public function DrupalPHPMailer::mail in PHPMailer 8.3
Sends an e-mail message composed by drupal_mail().
Parameters
$message: A message array, as described in hook_mail_alter().
Return value
TRUE if the mail was successfully accepted, otherwise FALSE.
Overrides MailInterface::mail
See also
PHPMailer::Send()
File
- src/
Plugin/ Mail/ DrupalPHPMailer.php, line 273
Class
- DrupalPHPMailer
- Implements the base PHPMailer class for the Drupal MailInterface.
Namespace
Drupal\phpmailer\Plugin\MailCode
public function mail(array $message) {
try {
// Parse 'From' e-mail address.
$from = phpmailer_parse_address($message['from']);
$from = reset($from);
$this->From = $from['mail'];
if ($from['name'] != '') {
$this->FromName = $from['name'];
}
unset($message['headers']['From']);
// @todo This \still\ might not be the correct way to do this.
$phpmailer_debug_email = \Drupal::config('system.maintenance')
->get('phpmailer_debug_email');
if (empty($phpmailer_debug_email)) {
// Set recipients.
foreach (phpmailer_parse_address($message['to']) as $address) {
$this
->AddAddress($address['mail'], $address['name']);
}
// Extract CCs and BCCs from headers.
if (!empty($message['headers']['Cc'])) {
foreach (phpmailer_parse_address($message['headers']['Cc']) as $address) {
$this
->AddCC($address['mail'], $address['name']);
}
}
if (!empty($message['headers']['Bcc'])) {
foreach (phpmailer_parse_address($message['headers']['Bcc']) as $address) {
$this
->AddBCC($address['mail'], $address['name']);
}
}
}
else {
// Reroute to debug e-mail address.
// @todo This might not be the correct way to do this.
$this
->AddAddress($phpmailer_debug_email);
}
unset($message['headers']['Cc'], $message['headers']['Bcc']);
// Extract Reply-To from headers.
if (isset($message['headers']['Reply-To'])) {
foreach (phpmailer_parse_address($message['headers']['Reply-To']) as $address) {
$this
->AddReplyTo($address['mail'], $address['name']);
}
unset($message['headers']['Reply-To']);
}
elseif (\Drupal::config('phpmailer.settings')
->get('smtp_always_replyto')) {
// If no Reply-To header has been explicitly set, use the From address to
// be able to respond to e-mails sent via Google Mail.
$this
->AddReplyTo($from['mail'], $from['name']);
}
// Extract Content-Type and charset.
if (isset($message['headers']['Content-Type'])) {
$content_type = explode(';', $message['headers']['Content-Type']);
$this->ContentType = trim(array_shift($content_type));
foreach ($content_type as $param) {
$param = explode('=', $param, 2);
$key = trim($param[0]);
if ($key == 'charset') {
$this->CharSet = trim($param[1]);
}
else {
$this->ContentType .= '; ' . $key . '=' . trim($param[1]);
}
}
unset($message['headers']['Content-Type']);
}
// Set additional properties.
$properties = [
'X-Priority' => 'Priority',
'Content-Transfer-Encoding' => 'Encoding',
'Sender' => 'Sender',
'Message-ID' => 'MessageID',
'Return-Path' => 'ReturnPath',
];
foreach ($properties as $source => $property) {
if (isset($message['headers'][$source])) {
$this->{$property} = $message['headers'][$source];
unset($message['headers'][$source]);
}
}
// This one is always set by PHPMailer.
unset($message['headers']['MIME-Version']);
// Add remaining header lines.
// Note: Any header lines MUST already be checked by the caller for unwanted
// newline characters to avoid header injection.
// @see PHPMailer::SecureHeader()
foreach ($message['headers'] as $key => $value) {
$this
->AddCustomHeader("{$key}:{$value}");
}
$this->Subject = $message['subject'];
$this->Body = $message['body'];
return $this
->Send();
} catch (phpmailerException $e) {
// Log the error including verbose debug information.
// Since DBLog module is the most common case, we use HTML to format the
// message for visual inspection. For sites running with Syslog or other
// logging modules, we put the actual values on separate lines (\n), so the
// surrounding HTML markup doesn't get too disturbing.
// Message is a safe t() string from DrupalPHPMailer::SetLanguage().
$output = $e
->getMessage();
// Attempt to delimit summary from full message.
$output .= " \n";
$arguments = [];
// Append SMTP communication output.
if ($this->drupalDebugOutput) {
// PHPMailer debug output contains HTML linebreaks. PRE is more readable.
$this->drupalDebugOutput = str_replace('<br />', '', $this->drupalDebugOutput);
$output .= '<p><strong>Server response:</strong></p>';
$output .= "<pre>\n@smtp_output\n</pre>";
$arguments += [
'@smtp_output' => $this->drupalDebugOutput,
];
}
// We need to log the message in order to be able to debug why the server
// responded with an error. The mail body may contain passwords and other
// sensitive information, which should not be logged. Since all kind of
// mails are processed and Drupal provides no way to mark sensible data,
// it is technically impossible prevent logging in all cases.
// Remove $params; they've already been processed and may contain sensible
// data.
unset($message['params']);
// Subject.
$output .= "<p><strong>Subject:</strong> \n@subject\n</p>";
$arguments += [
'@subject' => $message['subject'],
];
unset($message['subject']);
// Body.
$output .= '<p><strong>Body:</strong></p>';
$output .= "<pre>\n@body\n</pre>";
$arguments += [
'@body' => $message['body'],
];
unset($message['body']);
// Rest of $message.
$output .= '<p><strong>Message:</strong></p>';
$output .= "<pre>\n@message\n</pre>";
$arguments += [
'@message' => var_export($message, TRUE),
];
\Drupal::logger('phpmailer')
->error($output, $arguments);
return FALSE;
}
}