/symphony/lib/toolkit/email-gateways/email.sendmail.php

https://github.com/builders/sym-bootstrap · PHP · 154 lines · 78 code · 24 blank · 52 comment · 4 complexity · b4d7d308a42fde2c709ba0c2ecbe6be3 MD5 · raw file

  1. <?php
  2. /**
  3. * @package email-gateways
  4. */
  5. require_once(TOOLKIT . '/class.emailgateway.php');
  6. require_once(TOOLKIT . '/class.emailhelper.php');
  7. /**
  8. * The basic gateway sending emails using Sendmail, php's mail function.
  9. *
  10. * @author Michael Eichelsdoerfer, Huib Keemink
  11. */
  12. Class SendmailGateway extends EmailGateway {
  13. /**
  14. * Returns the name, used in the dropdown menu in the preferences pane.
  15. *
  16. * @return array
  17. */
  18. public function about() {
  19. return array(
  20. 'name' => __('Sendmail (default)'),
  21. );
  22. }
  23. /**
  24. * Constructor. Sets basic default values based on preferences.
  25. *
  26. * @return void
  27. */
  28. public function __construct() {
  29. parent::__construct();
  30. $this->setSenderEmailAddress(Symphony::Configuration()->get('from_address', 'email_sendmail') ? Symphony::Configuration()->get('from_address', 'email_sendmail') : 'noreply@' . HTTP_HOST);
  31. $this->setSenderName(Symphony::Configuration()->get('from_name', 'email_sendmail') ? Symphony::Configuration()->get('from_name', 'email_sendmail') : 'Symphony');
  32. }
  33. /**
  34. * Send an email using the PHP mail() function
  35. *
  36. * Please note that 'encoded-words' should be used according to
  37. * RFC2047. Basically this means that the subject should be
  38. * encoded if necessary, as well as (real) names in 'From', 'To'
  39. * or 'Reply-To' header field bodies. For details see RFC2047.
  40. *
  41. * The parts of a message body should be encoded (quoted-printable
  42. * or base64) to make non-US-ASCII text work with the widest range
  43. * of email transports and clients.
  44. *
  45. * @return bool
  46. */
  47. public function send() {
  48. $this->validate();
  49. try {
  50. // Encode recipient names (but not any numeric array indexes)
  51. foreach($this->_recipients as $name => $email) {
  52. $name = is_numeric($name) ? $name : EmailHelper::qEncode($name);
  53. $recipients[$name] = $email;
  54. }
  55. // Combine keys and values into a recipient list (name <email>, name <email>).
  56. $recipient_list = EmailHelper::arrayToList($recipients);
  57. // Encode the subject
  58. $this->_subject = EmailHelper::qEncode($this->_subject);
  59. // Encode the sender name if it's not empty
  60. $this->_sender_name = empty($this->_sender_name) ? NULL : EmailHelper::qEncode($this->_sender_name);
  61. // Build the 'From' header field body
  62. $from = empty($this->_sender_name)
  63. ? $this->_sender_email_address
  64. : $this->_sender_name . ' <' . $this->_sender_email_address . '>';
  65. // Build the 'Reply-To' header field body
  66. if (!empty($this->_reply_to_email_address)) {
  67. if (!empty($this->_reply_to_name)) {
  68. $reply_to = EmailHelper::qEncode($this->_reply_to_name) . ' <'.$this->_reply_to_email_address.'>';
  69. }
  70. else {
  71. $reply_to = $this->_reply_to_email_address;
  72. }
  73. }
  74. if (!empty($reply_to)) {
  75. $this->_header_fields = array_merge(array(
  76. 'Reply-To' => $reply_to,
  77. ),$this->_header_fields);
  78. }
  79. // Build the message from the attachments, the html-text and the plain-text.
  80. $this->prepareMessageBody();
  81. // Build the header fields
  82. $this->_header_fields = array_merge(array(
  83. 'Message-ID' => sprintf('<%s@%s>', md5(uniqid()), HTTP_HOST),
  84. 'Date' => date('r'),
  85. 'From' => $from,
  86. 'X-Mailer' => 'Symphony Email Module',
  87. 'MIME-Version' => '1.0',
  88. ),$this->_header_fields);
  89. // Format header fields
  90. foreach ($this->_header_fields as $name => $body) {
  91. $header_fields[] = sprintf('%s: %s', $name, $body);
  92. }
  93. /**
  94. * Make things nice for mail().
  95. * - Replace CRLF in the message body by LF as required by mail().
  96. * - Implode the header fields as required by mail().
  97. */
  98. $this->_body = str_replace("\r\n", "\n", $this->_body);
  99. $header_fields = implode("\r\n", $header_fields);
  100. // Send the email
  101. mail($recipient_list, $this->_subject, $this->_body, $header_fields, "-f{$this->_sender_email_address}");
  102. }
  103. catch (Exception $e) {
  104. throw new EmailGatewayException($e->getMessage());
  105. }
  106. return true;
  107. }
  108. /**
  109. * Builds the preferences pane, shown in the symphony backend.
  110. *
  111. * @return XMLElement
  112. */
  113. public function getPreferencesPane() {
  114. parent::getPreferencesPane();
  115. $group = new XMLElement('fieldset');
  116. $group->setAttribute('class', 'settings pickable');
  117. $group->setAttribute('id', 'sendmail');
  118. $group->appendChild(new XMLElement('legend', __('Email: Sendmail')));
  119. $div = new XMLElement('div');
  120. $div->setAttribute('class', 'group');
  121. $label = Widget::Label(__('From Name'));
  122. $label->appendChild(Widget::Input('settings[email_sendmail][from_name]', $this->_sender_name));
  123. $div->appendChild($label);
  124. $label = Widget::Label(__('From Email Address'));
  125. $label->appendChild(Widget::Input('settings[email_sendmail][from_address]', $this->_sender_email_address));
  126. $div->appendChild($label);
  127. $group->appendChild($div);
  128. return $group;
  129. }
  130. }