Merge branch 'master' into revisionHandle3
[dokuwiki.git] / inc / Subscriptions / SubscriptionSender.php
blobafc05bfc06b28809eb1d06d6430027e50dd00eb5
1 <?php
3 namespace dokuwiki\Subscriptions;
5 use Mailer;
7 abstract class SubscriptionSender
9 protected $mailer;
11 public function __construct(Mailer $mailer = null)
13 if ($mailer === null) {
14 $mailer = new Mailer();
16 $this->mailer = $mailer;
19 /**
20 * Get a valid message id for a certain $id and revision (or the current revision)
22 * @param string $id The id of the page (or media file) the message id should be for
23 * @param string $rev The revision of the page, set to the current revision of the page $id if not set
25 * @return string
27 protected function getMessageID($id, $rev = null)
29 static $listid = null;
30 if (is_null($listid)) {
31 $server = parse_url(DOKU_URL, PHP_URL_HOST);
32 $listid = join('.', array_reverse(explode('/', DOKU_BASE))) . $server;
33 $listid = urlencode($listid);
34 $listid = strtolower(trim($listid, '.'));
37 if (is_null($rev)) {
38 $rev = @filemtime(wikiFN($id));
41 return "<$id?rev=$rev@$listid>";
44 /**
45 * Helper function for sending a mail
47 * @param string $subscriber_mail The target mail address
48 * @param string $subject The lang id of the mail subject (without the
49 * prefix “mail_”)
50 * @param string $context The context of this mail, eg. page or namespace id
51 * @param string $template The name of the mail template
52 * @param array $trep Predefined parameters used to parse the
53 * template (in text format)
54 * @param array $hrep Predefined parameters used to parse the
55 * template (in HTML format), null to default to $trep
56 * @param array $headers Additional mail headers in the form 'name' => 'value'
58 * @return bool
59 * @author Adrian Lang <lang@cosmocode.de>
62 protected function send($subscriber_mail, $subject, $context, $template, $trep, $hrep = null, $headers = [])
64 global $lang;
65 global $conf;
67 $text = rawLocale($template);
68 $subject = $lang['mail_' . $subject] . ' ' . $context;
69 $mail = $this->mailer;
70 $mail->bcc($subscriber_mail);
71 $mail->subject($subject);
72 $mail->setBody($text, $trep, $hrep);
73 if (in_array($template, ['subscr_list', 'subscr_digest'])) {
74 $mail->from($conf['mailfromnobody']);
76 if (isset($trep['SUBSCRIBE'])) {
77 $mail->setHeader('List-Unsubscribe', '<' . $trep['SUBSCRIBE'] . '>', false);
80 foreach ($headers as $header => $value) {
81 $mail->setHeader($header, $value);
84 return $mail->send();