public function HTMLMailSystem::mail in HTML Mail 8
Same name and namespace in other branches
- 8.3 src/Plugin/Mail/HtmlMailSystem.php \Drupal\htmlmail\Plugin\Mail\HtmlMailSystem::mail()
Send an email message.
Parameters
array $message: An associative array containing at least:
- headers: An associative array of (name => value) email headers.
- body: The text/plain or text/html message body.
- MailMIME: The message, parsed into a MailMIME object.
Return value
bool TRUE if the mail was successfully accepted or queued, FALSE otherwise.
Overrides MailInterface::mail
See also
drupal_mail()
https://documentation.mailgun.com/api-sending.html#sending
File
- src/
Plugin/ Mail/ HTMLMailSystem.php, line 319
Class
- HTMLMailSystem
- Modify the Drupal mail system to use HTMLMail when sending emails.
Namespace
Drupal\htmlmail\Plugin\MailCode
public function mail(array $message) {
$eol = $this->siteSettings
->get('mail_line_endings', PHP_EOL);
$params = [];
// Ensure that subject is non-null.
$message += [
'subject' => t('(No subject)'),
];
// Check for empty recipient.
if (empty($message['to'])) {
if (empty($message['headers']['To'])) {
$this
->getLogger()
->error('Cannot send email about %subject without a recipient.', [
'%subject' => $message['subject'],
]);
return FALSE;
}
$message['to'] = $message['headers']['To'];
}
if (class_exists('HTMLMailMime')) {
$mime = new HTMLMailMime($this->logger, $this->siteSettings, $this->mimeType, $this->fileSystem);
$to = $mime
->mimeEncodeHeader('to', $message['to']);
$subject = $mime
->mimeEncodeHeader('subject', $message['subject']);
$txt_headers = $mime
->mimeTxtHeaders($message['headers']);
}
else {
$to = Unicode::mimeHeaderEncode($message['to']);
$subject = Unicode::mimeHeaderEncode($message['subject']);
$txt_headers = $this
->txtHeaders($message['headers']);
}
$body = preg_replace('#(\\r\\n|\\r|\\n)#s', $eol, $message['body']);
// Check for empty body.
if (empty($body)) {
$this
->getLogger()
->warning('Refusing to send a blank email to %recipient about %subject.', [
'%recipient' => $message['to'],
'%subject' => $message['subject'],
]);
return FALSE;
}
if ($this->configVariables
->get('htmlmail_debug')) {
$params = [
$to,
$subject,
Unicode::substr($body, 0, min(80, strpos("\n", $body))) . '...',
$txt_headers,
];
}
if (isset($message['headers']['Return-Path'])) {
// A return-path was set.
if (isset($_SERVER['WINDIR']) || strpos($_SERVER['SERVER_SOFTWARE'], 'Win32') !== FALSE) {
// On Windows, PHP will use the value of sendmail_from for the
// Return-Path header.
$old_from = ini_get('sendmail_from');
ini_set('sendmail_from', $message['headers']['Return-Path']);
$result = @mail($to, $subject, $body, $txt_headers);
ini_set('sendmail_from', $old_from);
}
elseif (ini_get('safe_mode')) {
// If safe mode is in effect, passing the fifth parameter to @mail
// will cause it to return FALSE and generate a PHP warning, even
// if the parameter is NULL.
$result = @mail($to, $subject, $body, $txt_headers);
}
else {
// On most non-Windows systems, the "-f" option to the sendmail command
// is used to set the Return-Path.
$extra = '-f' . $message['headers']['Return-Path'];
$result = @mail($to, $subject, $body, $txt_headers, $extra);
if ($this->configVariables
->get('htmlmail_debug')) {
$params[] = $extra;
}
}
}
else {
// No return-path was set.
$result = @mail($to, $subject, $body, $txt_headers);
}
if (!$result && $this->configVariables
->get('htmlmail_debug')) {
$call = '@mail(' . implode(', ', $params) . ')';
foreach ($params as $i => $value) {
$params[$i] = var_export($value, 1);
}
if (defined('DEBUG_BACKTRACE_IGNORE_ARGS')) {
$trace = print_r(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), 1);
}
else {
$trace = debug_backtrace(0);
for ($i = count($trace) - 1; $i >= 0; $i--) {
unset($trace[$i]['args']);
}
$trace = print_r($trace);
}
$this
->getLogger()
->info('Mail sending failed because:<br /><pre>@call</pre><br />returned FALSE.<br /><pre>@trace</pre>', [
'@call' => $call,
'@trace' => $trace,
]);
}
return $result;
}