public function SWIFTMailSystem::mail in Swift Mailer 7
Sends a message composed by drupal_mail().
Parameters
array $message: A message array holding all relevant details for the message.
Return value
boolean TRUE if the message was successfully sent, and otherwise FALSE.
Overrides MailSystemInterface::mail
See also
http://api.drupal.org/api/drupal/includes--mail.inc/interface/MailSystem...
File
- includes/
classes/ SWIFTMailSystem.inc, line 105 - The implementation of MailSystemInterface which delegates handling of e-mails to the Swift Mailer library.
Class
- SWIFTMailSystem
- @file The implementation of MailSystemInterface which delegates handling of e-mails to the Swift Mailer library.
Code
public function mail(array $message) {
// Include required files.
require_once dirname(dirname(__FILE__)) . '/helpers/conversion.inc';
require_once dirname(dirname(__FILE__)) . '/helpers/utilities.inc';
// Validate whether the Swift Mailer module has been configured.
$library_path = variable_get('swiftmailer_path', SWIFTMAILER_VARIABLE_PATH_DEFAULT);
if (empty($library_path)) {
watchdog('swiftmailer', 'An attempt to send an e-mail failed. The Swift Mailer library could not be found by the Swift Mailer module.', array(), WATCHDOG_ERROR);
drupal_set_message(t('An attempt to send the e-mail failed. The e-mail has not been sent.'), 'error');
return;
}
// Include the Swift Mailer library.
require_once DRUPAL_ROOT . '/' . $library_path . '/lib/swift_required.php';
try {
// Create a new message.
$m = Swift_Message::newInstance();
// Not all Drupal headers should be added to the e-mail message.
// Some headers must be suppressed in order for Swift Mailer to
// do its work properly.
$suppressable_headers = swiftmailer_get_supressable_headers();
// Keep track of whether we need to respect the provided e-mail
// format or not
$respect_format = variable_get('swiftmailer_respect_format', SWIFTMAILER_VARIABLE_RESPECT_FORMAT_DEFAULT);
// Process headers provided by Drupal. We want to add all headers which
// are provided by Drupal to be added to the message. For each header we
// first have to find out what type of header it is, and then add it to
// the message as the particular header type.
if (!empty($message['headers']) && is_array($message['headers'])) {
foreach ($message['headers'] as $header_key => $header_value) {
// Check wether the current header key is empty or represents
// a header that should be suppressed. If yes, then skip header.
if (empty($header_key) || in_array($header_key, $suppressable_headers)) {
continue;
}
// Skip 'Content-Type' header if the message to be sent will be a
// multipart message or the provided format is not to be respected.
if ($header_key == 'Content-Type' && (!$respect_format || swiftmailer_is_multipart($message))) {
continue;
}
// Get header type.
$header_type = swiftmailer_get_headertype($header_key, $header_value);
// Add the current header to the e-mail message.
switch ($header_type) {
case SWIFTMAILER_HEADER_ID:
swiftmailer_add_id_header($m, $header_key, $header_value);
break;
case SWIFTMAILER_HEADER_PATH:
swiftmailer_add_path_header($m, $header_key, $header_value);
break;
case SWIFTMAILER_HEADER_MAILBOX:
swiftmailer_add_mailbox_header($m, $header_key, $header_value);
break;
case SWIFTMAILER_HEADER_DATE:
swiftmailer_add_date_header($m, $header_key, $header_value);
break;
case SWIFTMAILER_HEADER_PARAMETERIZED:
swiftmailer_add_parameterized_header($m, $header_key, $header_value);
break;
default:
swiftmailer_add_text_header($m, $header_key, $header_value);
break;
}
}
}
// Set basic message details.
swiftmailer_remove_header($m, 'From');
swiftmailer_remove_header($m, 'To');
swiftmailer_remove_header($m, 'Subject');
if ($message['from'] == variable_get('site_mail', '')) {
$sender_name = variable_get('swiftmailer_sender_name', '');
if (empty($sender_name)) {
$sender_name = variable_get('site_name', 'Drupal');
}
$sender_email = variable_get('swiftmailer_sender_email', '');
if (empty($sender_email)) {
$sender_email = variable_get('site_mail', '');
}
$message['from'] = '"' . $sender_name . '" <' . $sender_email . '>';
}
// Parse 'from' and 'to' mailboxes.
$from = swiftmailer_parse_mailboxes($message['from']);
$to = swiftmailer_parse_mailboxes($message['to']);
// Set 'from', 'to' and 'subject' headers.
$m
->setFrom($from);
$m
->setTo($to);
$m
->setSubject($message['subject']);
// Get applicable format.
$applicable_format = $this
->getApplicableFormat($message);
// Get applicable character set.
$applicable_charset = $this
->getApplicableCharset($message);
// Set body.
$m
->setBody($message['body'], $applicable_format, $applicable_charset);
// Add alternative plain text version if format is HTML and plain text
// version is available.
if ($applicable_format == SWIFTMAILER_FORMAT_HTML && !empty($message['plain'])) {
$m
->addPart($message['plain'], SWIFTMAILER_FORMAT_PLAIN, $applicable_charset);
}
// Validate that $message['params']['files'] is an array.
if (empty($message['params']['files']) || !is_array($message['params']['files'])) {
$message['params']['files'] = array();
}
// Let other modules get the chance to add attachable files.
$files = module_invoke_all('swiftmailer_attach', $message['key']);
if (!empty($files) && is_array($files)) {
$message['params']['files'] = array_merge(array_values($message['params']['files']), array_values($files));
}
// Attach files.
if (!empty($message['params']['files']) && is_array($message['params']['files'])) {
$this
->attach($m, $message['params']['files']);
}
// Attach files (provide compatibility with mimemail)
if (!empty($message['params']['attachments']) && is_array($message['params']['attachments'])) {
$this
->attachAsMimeMail($m, $message['params']['attachments']);
}
// Embed images.
if (!empty($message['params']['images']) && is_array($message['params']['images'])) {
$this
->embed($m, $message['params']['images']);
}
static $mailer;
// If required, create a mailer which will be used to send the message.
if (empty($mailer)) {
// Get the configured transport type.
$transport_type = variable_get('swiftmailer_transport', SWIFTMAILER_VARIABLE_TRANSPORT_DEFAULT);
// Configure the mailer based on the configured transport type.
switch ($transport_type) {
case SWIFTMAILER_TRANSPORT_SMTP:
// Get transport configuration.
$host = variable_get('swiftmailer_smtp_host', SWIFTMAILER_VARIABLE_SMTP_HOST_DEFAULT);
$port = variable_get('swiftmailer_smtp_port', SWIFTMAILER_VARIABLE_SMTP_PORT_DEFAULT);
$encryption = variable_get('swiftmailer_smtp_encryption', SWIFTMAILER_VARIABLE_SMTP_ENCRYPTION_DEFAULT);
$username = variable_get('swiftmailer_smtp_username', SWIFTMAILER_VARIABLE_SMTP_USERNAME_DEFAULT);
$password = variable_get('swiftmailer_smtp_password', SWIFTMAILER_VARIABLE_SMTP_PASSWORD_DEFAULT);
// Instantiate transport.
$transport = Swift_SmtpTransport::newInstance($host, $port);
$transport
->setLocalDomain('[127.0.0.1]');
// Set encryption (if any).
if (!empty($encryption)) {
$transport
->setEncryption($encryption);
}
// Set username (if any).
if (!empty($username)) {
$transport
->setUsername($username);
}
// Set password (if any).
if (!empty($password)) {
$transport
->setPassword($password);
}
$mailer = Swift_Mailer::newInstance($transport);
break;
case SWIFTMAILER_TRANSPORT_SENDMAIL:
// Get transport configuration.
$path = variable_get('swiftmailer_sendmail_path', SWIFTMAILER_VARIABLE_SENDMAIL_PATH_DEFAULT);
$mode = variable_get('swiftmailer_sendmail_mode', SWIFTMAILER_VARIABLE_SENDMAIL_MODE_DEFAULT);
// Instantiate transport.
$transport = Swift_SendmailTransport::newInstance($path . ' -' . $mode);
$mailer = Swift_Mailer::newInstance($transport);
break;
case SWIFTMAILER_TRANSPORT_NATIVE:
// Instantiate transport.
$transport = Swift_MailTransport::newInstance();
$mailer = Swift_Mailer::newInstance($transport);
break;
case SWIFTMAILER_TRANSPORT_SPOOL:
// Instantiate transport.
$spooldir = variable_get('swiftmailer_spool_directory', SWIFTMAILER_VARIABLE_SPOOL_DIRECTORY_DEFAULT);
$spool = new Swift_FileSpool($spooldir);
$transport = Swift_SpoolTransport::newInstance($spool);
$mailer = Swift_Mailer::newInstance($transport);
break;
}
}
drupal_alter('swiftmailer', $mailer, $m);
swiftmailer_filter_message($m);
// Send the message.
return $mailer
->send($m);
} catch (Exception $e) {
$headers = !empty($m) ? $m
->getHeaders() : '';
$headers = !empty($headers) ? nl2br($headers
->toString()) : 'No headers were found.';
watchdog('swiftmailer', 'An attempt to send an e-mail message failed, and the following error
message was returned : @exception_message<br /><br />The e-mail carried
the following headers:<br /><br />!headers', array(
'@exception_message' => $e
->getMessage(),
'!headers' => $headers,
), WATCHDOG_ERROR);
drupal_set_message(t('An attempt to send an e-mail message failed.'), 'error');
}
}