xref: /dokuwiki/inc/Subscriptions/SubscriptionSender.php (revision 9c22b77ce53fe9d4577eb121afa86dda93c3a0e4)
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{
947de339bSMichael Große    private $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
19479c05b1SMichael Große
20479c05b1SMichael Große    /**
21479c05b1SMichael Große     * Helper function for sending a mail
22479c05b1SMichael Große     *
23479c05b1SMichael Große     * @param string $subscriber_mail The target mail address
24479c05b1SMichael Große     * @param string $subject         The lang id of the mail subject (without the
25479c05b1SMichael Große     *                                prefix “mail_”)
26479c05b1SMichael Große     * @param string $context         The context of this mail, eg. page or namespace id
27479c05b1SMichael Große     * @param string $template        The name of the mail template
28479c05b1SMichael Große     * @param array  $trep            Predefined parameters used to parse the
29479c05b1SMichael Große     *                                template (in text format)
30479c05b1SMichael Große     * @param array  $hrep            Predefined parameters used to parse the
31479c05b1SMichael Große     *                                template (in HTML format), null to default to $trep
32479c05b1SMichael Große     * @param array  $headers         Additional mail headers in the form 'name' => 'value'
33*9c22b77cSMichael Große     *
34479c05b1SMichael Große     * @return bool
35*9c22b77cSMichael Große     * @author Adrian Lang <lang@cosmocode.de>
36*9c22b77cSMichael Große     *
37479c05b1SMichael Große     */
38*9c22b77cSMichael Große    protected function send($subscriber_mail, $subject, $context, $template, $trep, $hrep = null, $headers = [])
39*9c22b77cSMichael Große    {
40479c05b1SMichael Große        global $lang;
41479c05b1SMichael Große        global $conf;
42479c05b1SMichael Große
43479c05b1SMichael Große        $text = rawLocale($template);
44479c05b1SMichael Große        $subject = $lang['mail_' . $subject] . ' ' . $context;
4547de339bSMichael Große        $mail = $this->mailer;
46479c05b1SMichael Große        $mail->bcc($subscriber_mail);
47479c05b1SMichael Große        $mail->subject($subject);
48479c05b1SMichael Große        $mail->setBody($text, $trep, $hrep);
49*9c22b77cSMichael Große        if (in_array($template, ['subscr_list', 'subscr_digest'])) {
50479c05b1SMichael Große            $mail->from($conf['mailfromnobody']);
51479c05b1SMichael Große        }
52479c05b1SMichael Große        if (isset($trep['SUBSCRIBE'])) {
53479c05b1SMichael Große            $mail->setHeader('List-Unsubscribe', '<' . $trep['SUBSCRIBE'] . '>', false);
54479c05b1SMichael Große        }
55479c05b1SMichael Große
56479c05b1SMichael Große        foreach ($headers as $header => $value) {
57479c05b1SMichael Große            $mail->setHeader($header, $value);
58479c05b1SMichael Große        }
59479c05b1SMichael Große
60479c05b1SMichael Große        return $mail->send();
61479c05b1SMichael Große    }
62479c05b1SMichael Große}
63