View source
<?php
define('POSTMARKAPP_API_KEY', variable_get('postmark_api_key', ''));
if (($library = libraries_load('postmark-php')) && !empty($library['loaded'])) {
variable_set('postmark_library_version', $library['version']);
$debug_mode = variable_get('postmark_debug_mode', 0);
if ($debug_mode) {
watchdog('postmark', '<pre>Library loaded successfully: ' . print_r($library, 1) . '</pre>');
}
}
else {
variable_del('postmark_library_version');
$debug_mode = variable_get('postmark_debug_mode', 0);
if ($debug_mode) {
watchdog('postmark', '<pre>Unable to load library: ' . print_r($library, 1) . '</pre>', NULL, WATCHDOG_ERROR);
}
if (arg(0) == 'admin' && arg(1) != 'config' && arg(1) != 'reports') {
drupal_set_message(t('The Postmark module is enabled but the Postmark PHP library has not been installed. See module README file for instructions.'), 'error');
}
}
class PostmarkMailSystem implements MailSystemInterface {
protected $AllowHtml;
public function format(array $message) {
$this->AllowHtml = TRUE;
$message['body'] = implode("\n\n", $message['body']);
return $message;
}
public function mail(array $message) {
$postmarkApiKey = variable_get('postmark_api_key', '');
$postmark_mail = variable_get('postmark_library_version') == '0.4.5' ? new Mail_Postmark() : new Postmark\Mail($postmarkApiKey);
$address = postmark_parse_address($message['from']);
$sender = $address[0]['mail'];
$name = $address[0]['name'];
$from = variable_get('site_mail', ini_get('sendmail_from'));
$postmark_mail
->from($from);
$postmark_mail
->subject($message['subject']);
if ($sender != $from) {
$postmark_mail
->replyTo($sender, $name == '' ? NULL : $name);
unset($message['headers']['Reply-To']);
}
else {
if ($name != '') {
$postmark_mail
->fromName($name);
}
}
$fullsender = $sender;
if ($name != '') {
$fullsender = $name . ' [' . $sender . ']';
}
foreach ($message['headers'] as $name => $value) {
if ($name == 'Reply-To') {
$reply_to = postmark_parse_address($message['headers']['Reply-To']);
$postmark_mail
->replyTo($reply_to[0]['mail'], $reply_to[0]['name']);
}
}
$debug_mode = variable_get('postmark_debug_mode', 0);
$debug_email = variable_get('postmark_debug_email', '');
if (!$debug_mode) {
foreach (postmark_parse_address($message['to']) as $id => $address) {
if ($id == 0) {
$postmark_mail
->to($address['mail'], $address['name']);
}
else {
$postmark_mail
->addTo($address['mail'], $address['name']);
}
}
}
else {
if ($debug_email != '') {
drupal_set_message(t('Debugging email used @email', array(
'@email' => $debug_email,
)), 'warning');
$postmark_mail
->to($debug_email);
}
}
if (strpos($message['headers']['Content-Type'], 'text/plain') !== FALSE) {
if ($sender != $from) {
$message['body'] .= "\r\n\r\n";
$message['body'] .= t('Message sent by') . ":\r\n";
$message['body'] .= $fullsender;
}
$postmark_mail
->messagePlain($message['body']);
}
else {
if ($sender != $from) {
$message['body'] .= "<p>" . t('Message sent by') . ":<br />" . $fullsender . "</p>";
}
$postmark_mail
->messageHtml($message['body']);
}
if ($debug_mode) {
drupal_set_message('Message array: <pre>' . print_r($message, TRUE) . '</pre>', 'warning');
}
if (variable_get('postmark_debug_no_send', 0)) {
drupal_set_message('Email successfully tested, no email has been sent (no credits used)', 'warning');
return TRUE;
}
else {
try {
if (!($result = $postmark_mail
->send())) {
watchdog('postmark', "Mail sending error: {$postmark_mail->ErrorInfo}", NULL, WATCHDOG_ERROR);
}
} catch (Exception $e) {
watchdog('postmark', 'Exception message: ' . $e
->getMessage(), NULL, WATCHDOG_ERROR);
drupal_set_message('Mail sending error: ' . $e
->getMessage(), 'error');
if ($debug_mode) {
watchdog('postmark', 'Exception caught: <pre>' . print_r($e, TRUE) . '</pre>', NULL, WATCHDOG_ERROR);
}
}
}
return $result;
}
}