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.

84 lines
2.9KB

  1. <?php
  2. require_once('include/email.php');
  3. class Emailer {
  4. /**
  5. * Send a multipart/alternative message with Text and HTML versions
  6. *
  7. * @param fromName name of the sender
  8. * @param fromEmail email fo the sender
  9. * @param replyTo replyTo address to direct responses
  10. * @param toEmail destination email address
  11. * @param messageSubject subject of the message
  12. * @param htmlVersion html version of the message
  13. * @param textVersion text only version of the message
  14. * @param additionalMailHeader additions to the smtp mail header
  15. * @param optional uid user id of the destination user
  16. */
  17. static public function send($params) {
  18. call_hooks('emailer_send_prepare', $params);
  19. $email_textonly = False;
  20. if (x($params,"uid")) {
  21. $email_textonly = get_pconfig($params['uid'], "system", "email_textonly");
  22. }
  23. $fromName = email_header_encode(html_entity_decode($params['fromName'],ENT_QUOTES,'UTF-8'),'UTF-8');
  24. $messageSubject = email_header_encode(html_entity_decode($params['messageSubject'],ENT_QUOTES,'UTF-8'),'UTF-8');
  25. // generate a mime boundary
  26. $mimeBoundary =rand(0,9)."-"
  27. .rand(100000000,999999999)."-"
  28. .rand(100000000,999999999)."=:"
  29. .rand(10000,99999);
  30. // generate a multipart/alternative message header
  31. $messageHeader =
  32. $params['additionalMailHeader'] .
  33. "From: $fromName <{$params['fromEmail']}>\n" .
  34. "Reply-To: $fromName <{$params['replyTo']}>\n" .
  35. "MIME-Version: 1.0\n" .
  36. "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
  37. // assemble the final multipart message body with the text and html types included
  38. $textBody = chunk_split(base64_encode($params['textVersion']));
  39. $htmlBody = chunk_split(base64_encode($params['htmlVersion']));
  40. $multipartMessageBody =
  41. "--" . $mimeBoundary . "\n" . // plain text section
  42. "Content-Type: text/plain; charset=UTF-8\n" .
  43. "Content-Transfer-Encoding: base64\n\n" .
  44. $textBody . "\n";
  45. if (!$email_textonly && !is_null($params['htmlVersion'])){
  46. $multipartMessageBody .=
  47. "--" . $mimeBoundary . "\n" . // text/html section
  48. "Content-Type: text/html; charset=UTF-8\n" .
  49. "Content-Transfer-Encoding: base64\n\n" .
  50. $htmlBody . "\n";
  51. }
  52. $multipartMessageBody .=
  53. "--" . $mimeBoundary . "--\n"; // message ending
  54. // send the message
  55. $hookdata = array(
  56. 'to' => $params['toEmail'],
  57. 'subject' => $messageSubject,
  58. 'body' => $multipartMessageBody,
  59. 'headers' => $messageHeader
  60. );
  61. //echo "<pre>"; var_dump($hookdata); killme();
  62. call_hooks("emailer_send", $hookdata);
  63. $res = mail(
  64. $hookdata['to'], // send to address
  65. $hookdata['subject'], // subject
  66. $hookdata['body'], // message body
  67. $hookdata['headers'] // message headers
  68. );
  69. logger("header " . 'To: ' . $params['toEmail'] . "\n" . $messageHeader, LOGGER_DEBUG);
  70. logger("return value " . (($res)?"true":"false"), LOGGER_DEBUG);
  71. return $res;
  72. }
  73. }