Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

117 lines
3.7 KiB

  1. <?php
  2. /**
  3. * @file src/Util/Emailer.php
  4. */
  5. namespace Friendica\Util;
  6. use Friendica\Core\Config;
  7. use Friendica\Core\Hook;
  8. use Friendica\Core\Logger;
  9. use Friendica\Protocol\Email;
  10. /**
  11. * @brief class to handle emailing
  12. */
  13. class Emailer
  14. {
  15. /**
  16. * Send a multipart/alternative message with Text and HTML versions
  17. *
  18. * @param array $params parameters
  19. * fromName name of the sender
  20. * fromEmail email of the sender
  21. * replyTo address to direct responses
  22. * toEmail destination email address
  23. * messageSubject subject of the message
  24. * htmlVersion html version of the message
  25. * textVersion text only version of the message
  26. * additionalMailHeader additions to the SMTP mail header
  27. * optional uid user id of the destination user
  28. *
  29. * @return bool
  30. * @throws \Friendica\Network\HTTPException\InternalServerErrorException
  31. */
  32. public static function send(array $params)
  33. {
  34. $params['sent'] = false;
  35. Hook::callAll('emailer_send_prepare', $params);
  36. if ($params['sent']) {
  37. return true;
  38. }
  39. $email_textonly = false;
  40. if (!empty($params['uid'])) {
  41. $email_textonly = DI::pConfig()->get($params['uid'], "system", "email_textonly");
  42. }
  43. $fromName = Email::encodeHeader(html_entity_decode($params['fromName'], ENT_QUOTES, 'UTF-8'), 'UTF-8');
  44. $messageSubject = Email::encodeHeader(html_entity_decode($params['messageSubject'], ENT_QUOTES, 'UTF-8'), 'UTF-8');
  45. // generate a mime boundary
  46. $mimeBoundary =rand(0, 9)."-"
  47. .rand(100000000, 999999999)."-"
  48. .rand(100000000, 999999999)."=:"
  49. .rand(10000, 99999);
  50. // generate a multipart/alternative message header
  51. $messageHeader = ($params['additionalMailHeader'] ?? '') .
  52. "From: $fromName <{$params['fromEmail']}>\n" .
  53. "Reply-To: $fromName <{$params['replyTo']}>\n" .
  54. "MIME-Version: 1.0\n" .
  55. "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
  56. // assemble the final multipart message body with the text and html types included
  57. $textBody = chunk_split(base64_encode($params['textVersion']));
  58. $htmlBody = chunk_split(base64_encode($params['htmlVersion']));
  59. $multipartMessageBody = "--" . $mimeBoundary . "\n" . // plain text section
  60. "Content-Type: text/plain; charset=UTF-8\n" .
  61. "Content-Transfer-Encoding: base64\n\n" .
  62. $textBody . "\n";
  63. if (!$email_textonly && !is_null($params['htmlVersion'])) {
  64. $multipartMessageBody .=
  65. "--" . $mimeBoundary . "\n" . // text/html section
  66. "Content-Type: text/html; charset=UTF-8\n" .
  67. "Content-Transfer-Encoding: base64\n\n" .
  68. $htmlBody . "\n";
  69. }
  70. $multipartMessageBody .=
  71. "--" . $mimeBoundary . "--\n"; // message ending
  72. if (Config::get("system", "sendmail_params", true)) {
  73. $sendmail_params = '-f ' . $params['fromEmail'];
  74. } else {
  75. $sendmail_params = null;
  76. }
  77. // send the message
  78. $hookdata = [
  79. 'to' => $params['toEmail'],
  80. 'subject' => $messageSubject,
  81. 'body' => $multipartMessageBody,
  82. 'headers' => $messageHeader,
  83. 'parameters' => $sendmail_params,
  84. 'sent' => false,
  85. ];
  86. Hook::callAll("emailer_send", $hookdata);
  87. if ($hookdata['sent']) {
  88. return true;
  89. }
  90. $res = mail(
  91. $hookdata['to'],
  92. $hookdata['subject'],
  93. $hookdata['body'],
  94. $hookdata['headers'],
  95. $hookdata['parameters']
  96. );
  97. Logger::log("header " . 'To: ' . $params['toEmail'] . "\n" . $messageHeader, Logger::DEBUG);
  98. Logger::log("return value " . (($res)?"true":"false"), Logger::DEBUG);
  99. return $res;
  100. }
  101. }