aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/inc/Subscriptions/SubscriptionSender.php
blob: 4a4e31ea2711e453140fd755c17f75e13de90e17 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php

namespace dokuwiki\Subscriptions;

use Mailer;

abstract class SubscriptionSender
{
    protected $mailer;

    public function __construct(Mailer $mailer = null)
    {
        if (!$mailer instanceof \Mailer) {
            $mailer = new Mailer();
        }
        $this->mailer = $mailer;
    }

    /**
     * Get a valid message id for a certain $id and revision (or the current revision)
     *
     * @param string $id  The id of the page (or media file) the message id should be for
     * @param string $rev The revision of the page, set to the current revision of the page $id if not set
     *
     * @return string
     */
    protected function getMessageID($id, $rev = null)
    {
        static $listid = null;
        if (is_null($listid)) {
            $server = parse_url(DOKU_URL, PHP_URL_HOST);
            $listid = implode('.', array_reverse(explode('/', DOKU_BASE))) . $server;
            $listid = urlencode($listid);
            $listid = strtolower(trim($listid, '.'));
        }

        if (is_null($rev)) {
            $rev = @filemtime(wikiFN($id));
        }

        return "<$id?rev=$rev@$listid>";
    }

    /**
     * Helper function for sending a mail
     *
     * @param string $subscriber_mail The target mail address
     * @param string $subject         The lang id of the mail subject (without the
     *                                prefix “mail_”)
     * @param string $context         The context of this mail, eg. page or namespace id
     * @param string $template        The name of the mail template
     * @param array  $trep            Predefined parameters used to parse the
     *                                template (in text format)
     * @param array  $hrep            Predefined parameters used to parse the
     *                                template (in HTML format), null to default to $trep
     * @param array  $headers         Additional mail headers in the form 'name' => 'value'
     *
     * @return bool
     * @author Adrian Lang <lang@cosmocode.de>
     *
     */
    protected function send($subscriber_mail, $subject, $context, $template, $trep, $hrep = null, $headers = [])
    {
        global $lang;
        global $conf;

        $text = rawLocale($template);
        $subject = $lang['mail_' . $subject] . ' ' . $context;
        $mail = $this->mailer;
        $mail->bcc($subscriber_mail);
        $mail->subject($subject);
        $mail->setBody($text, $trep, $hrep);
        if (in_array($template, ['subscr_list', 'subscr_digest'])) {
            $mail->from($conf['mailfromnobody']);
        }
        if (isset($trep['SUBSCRIBE'])) {
            $mail->setHeader('List-Unsubscribe', '<' . $trep['SUBSCRIBE'] . '>', false);
        }

        foreach ($headers as $header => $value) {
            $mail->setHeader($header, $value);
        }

        return $mail->send();
    }
}