SubscriptionSender.php 2.8 KB

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