PageRenderTime 26ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/smartpan_notification/lib/Drupal/smartpan_notification/Mail/SmartPanMail.php

https://bitbucket.org/aswinvk28/smartpan-stock-drupal
PHP | 142 lines | 52 code | 18 blank | 72 comment | 9 complexity | c090355476a3212703076a2158e39ae5 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /*
  3. * To change this template, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. namespace Drupal\smartpan\Mail;
  7. use Drupal\Core\Mail\PhpMail;
  8. /**
  9. * Description of SmartPanMail
  10. *
  11. * @author aswinvk28
  12. */
  13. class SmartPanMail extends PhpMail
  14. {
  15. public function __construct()
  16. {
  17. $this->settings = \Drupal::getContainer()->get('settings');
  18. }
  19. /**
  20. * Sends an e-mail message.
  21. *
  22. * @param array $message
  23. * A message array, as described in hook_mail_alter().
  24. *
  25. * @return bool
  26. * TRUE if the mail was successfully accepted, otherwise FALSE.
  27. *
  28. * @see http://php.net/manual/en/function.mail.php
  29. * @see drupal_mail()
  30. */
  31. public function mail(array $message) {
  32. // If 'Return-Path' isn't already set in php.ini, we pass it separately
  33. // as an additional parameter instead of in the header.
  34. // However, if PHP's 'safe_mode' is on, this is not allowed.
  35. if (isset($message['headers']['Return-Path']) && !ini_get('safe_mode')) {
  36. $return_path_set = strpos(ini_get('sendmail_path'), ' -f');
  37. if (!$return_path_set) {
  38. $message['Return-Path'] = $message['headers']['Return-Path'];
  39. unset($message['headers']['Return-Path']);
  40. }
  41. }
  42. $mimeheaders = array();
  43. foreach ($message['headers'] as $name => $value) {
  44. $mimeheaders[] = $name . ': ' . mime_header_encode($value);
  45. }
  46. $line_endings = settings()->get('mail_line_endings', PHP_EOL);
  47. // Prepare mail commands.
  48. $mail_subject = mime_header_encode($message['subject']);
  49. // Note: e-mail uses CRLF for line-endings. PHP's API requires LF
  50. // on Unix and CRLF on Windows. Drupal automatically guesses the
  51. // line-ending format appropriate for your system. If you need to
  52. // override this, adjust $settings['mail_line_endings'] in settings.php.
  53. $mail_body = preg_replace('@\r?\n@', $line_endings, $message['body']);
  54. // For headers, PHP's API suggests that we use CRLF normally,
  55. // but some MTAs incorrectly replace LF with CRLF. See #234403.
  56. $mail_headers = join("\n", $mimeheaders);
  57. $request = \Drupal::request();
  58. $smtp_settings = $this->settings->get('mail_settings');
  59. $pear = new \PEAR();
  60. if (! ($smtp = new \Net_SMTP($smtp_settings['host'], $smtp_settings['port'], $smtp_settings['localhost']))) {
  61. die("Unable to instantiate Net_SMTP object\n");
  62. }
  63. if ($pear->isError($e = $smtp->connect())) {
  64. die($e->getMessage() . "\n");
  65. }
  66. if ($pear->isError($e = $smtp->auth($smtp_settings['username'], $smtp_settings['password']))) {
  67. die('Could not authenticate');
  68. }
  69. if ($pear->isError($smtp->mailFrom($smtp_settings['sendmail_from']))) {
  70. die('Unable to set sender to <' . TEST_FROM . ">\n");
  71. }
  72. if ($pear->isError($res = $smtp->rcptTo($message['to']))) {
  73. die('Unable to add recipient <' . TEST_TO . '>: ' .
  74. $res->getMessage() . "\n");
  75. }
  76. if ($pear->isError($smtp->data($mail_subject . "\r\n" . $mail_body, $mail_headers))) {
  77. die("Unable to send data\n");
  78. }
  79. $mail_result = $smtp->disconnect();
  80. // We suppress warnings and notices from mail() because of issues on some
  81. // hosts. The return value of this method will still indicate whether mail
  82. // was sent successfully.
  83. // if (!$request->server->has('WINDIR') && strpos($request->server->get('SERVER_SOFTWARE'), 'Win32') === FALSE) {
  84. // if (isset($message['Return-Path']) && !ini_get('safe_mode')) {
  85. // // On most non-Windows systems, the "-f" option to the sendmail command
  86. // // is used to set the Return-Path. There is no space between -f and
  87. // // the value of the return path.
  88. // $mail_result = @mail(
  89. // $message['to'],
  90. // $mail_subject,
  91. // $mail_body,
  92. // $mail_headers,
  93. // '-f' . $message['Return-Path']
  94. // );
  95. // }
  96. // else {
  97. // // The optional $additional_parameters argument to mail() is not
  98. // // allowed if safe_mode is enabled. Passing any value throws a PHP
  99. // // warning and makes mail() return FALSE.
  100. // $mail_result = @mail(
  101. // $message['to'],
  102. // $mail_subject,
  103. // $mail_body,
  104. // $mail_headers
  105. // );
  106. // }
  107. // }
  108. // else {
  109. // // On Windows, PHP will use the value of sendmail_from for the
  110. // // Return-Path header.
  111. // $old_from = ini_get('sendmail_from');
  112. // ini_set('sendmail_from', $message['Return-Path']);
  113. // $mail_result = @mail(
  114. // $message['to'],
  115. // $mail_subject,
  116. // $mail_body,
  117. // $mail_headers
  118. // );
  119. // ini_set('sendmail_from', $old_from);
  120. // }
  121. return $mail_result;
  122. }
  123. }
  124. ?>