xref: /dokuwiki/inc/Subscriptions/SubscriptionSender.php (revision 704a815fe827ce918bab0973cde563705c65c698)
1479c05b1SMichael Große<?php
2479c05b1SMichael Große
3479c05b1SMichael Großenamespace dokuwiki\Subscriptions;
4479c05b1SMichael Große
5479c05b1SMichael Großeuse Mailer;
6479c05b1SMichael Große
7479c05b1SMichael Großeabstract class SubscriptionSender
8479c05b1SMichael Große{
9*704a815fSMichael Große    protected $mailer;
1047de339bSMichael Große
1147de339bSMichael Große    public function __construct(Mailer $mailer = null)
1247de339bSMichael Große    {
1347de339bSMichael Große        if ($mailer === null) {
1447de339bSMichael Große            $mailer = new Mailer();
1547de339bSMichael Große        }
1647de339bSMichael Große        $this->mailer = $mailer;
1747de339bSMichael Große    }
18479c05b1SMichael Große
19*704a815fSMichael Große    /**
20*704a815fSMichael Große     * Get a valid message id for a certain $id and revision (or the current revision)
21*704a815fSMichael Große     *
22*704a815fSMichael Große     * @param string $id  The id of the page (or media file) the message id should be for
23*704a815fSMichael Große     * @param string $rev The revision of the page, set to the current revision of the page $id if not set
24*704a815fSMichael Große     *
25*704a815fSMichael Große     * @return string
26*704a815fSMichael Große     */
27*704a815fSMichael Große    protected function getMessageID($id, $rev = null)
28*704a815fSMichael Große    {
29*704a815fSMichael Große        static $listid = null;
30*704a815fSMichael Große        if (is_null($listid)) {
31*704a815fSMichael Große            $server = parse_url(DOKU_URL, PHP_URL_HOST);
32*704a815fSMichael Große            $listid = join('.', array_reverse(explode('/', DOKU_BASE))) . $server;
33*704a815fSMichael Große            $listid = urlencode($listid);
34*704a815fSMichael Große            $listid = strtolower(trim($listid, '.'));
35*704a815fSMichael Große        }
36*704a815fSMichael Große
37*704a815fSMichael Große        if (is_null($rev)) {
38*704a815fSMichael Große            $rev = @filemtime(wikiFN($id));
39*704a815fSMichael Große        }
40*704a815fSMichael Große
41*704a815fSMichael Große        return "<$id?rev=$rev@$listid>";
42*704a815fSMichael Große    }
43479c05b1SMichael Große
44479c05b1SMichael Große    /**
45479c05b1SMichael Große     * Helper function for sending a mail
46479c05b1SMichael Große     *
47479c05b1SMichael Große     * @param string $subscriber_mail The target mail address
48479c05b1SMichael Große     * @param string $subject         The lang id of the mail subject (without the
49479c05b1SMichael Große     *                                prefix “mail_”)
50479c05b1SMichael Große     * @param string $context         The context of this mail, eg. page or namespace id
51479c05b1SMichael Große     * @param string $template        The name of the mail template
52479c05b1SMichael Große     * @param array  $trep            Predefined parameters used to parse the
53479c05b1SMichael Große     *                                template (in text format)
54479c05b1SMichael Große     * @param array  $hrep            Predefined parameters used to parse the
55479c05b1SMichael Große     *                                template (in HTML format), null to default to $trep
56479c05b1SMichael Große     * @param array  $headers         Additional mail headers in the form 'name' => 'value'
579c22b77cSMichael Große     *
58479c05b1SMichael Große     * @return bool
599c22b77cSMichael Große     * @author Adrian Lang <lang@cosmocode.de>
609c22b77cSMichael Große     *
61479c05b1SMichael Große     */
629c22b77cSMichael Große    protected function send($subscriber_mail, $subject, $context, $template, $trep, $hrep = null, $headers = [])
639c22b77cSMichael Große    {
64479c05b1SMichael Große        global $lang;
65479c05b1SMichael Große        global $conf;
66479c05b1SMichael Große
67479c05b1SMichael Große        $text = rawLocale($template);
68479c05b1SMichael Große        $subject = $lang['mail_' . $subject] . ' ' . $context;
6947de339bSMichael Große        $mail = $this->mailer;
70479c05b1SMichael Große        $mail->bcc($subscriber_mail);
71479c05b1SMichael Große        $mail->subject($subject);
72479c05b1SMichael Große        $mail->setBody($text, $trep, $hrep);
739c22b77cSMichael Große        if (in_array($template, ['subscr_list', 'subscr_digest'])) {
74479c05b1SMichael Große            $mail->from($conf['mailfromnobody']);
75479c05b1SMichael Große        }
76479c05b1SMichael Große        if (isset($trep['SUBSCRIBE'])) {
77479c05b1SMichael Große            $mail->setHeader('List-Unsubscribe', '<' . $trep['SUBSCRIBE'] . '>', false);
78479c05b1SMichael Große        }
79479c05b1SMichael Große
80479c05b1SMichael Große        foreach ($headers as $header => $value) {
81479c05b1SMichael Große            $mail->setHeader($header, $value);
82479c05b1SMichael Große        }
83479c05b1SMichael Große
84479c05b1SMichael Große        return $mail->send();
85479c05b1SMichael Große    }
86479c05b1SMichael Große}
87