public function PostmarkHandler::sendMail in Postmark 8
Connects to Postmark API and sends out the email.
Parameters
array $params: A message array.
Return value
bool TRUE if the mail was successfully accepted by the API, FALSE otherwise.
File
- src/
PostmarkHandler.php, line 59
Class
- PostmarkHandler
- Mail handler to send out an email message array to the Postmark API.
Namespace
Drupal\postmarkCode
public function sendMail(array $params) {
$api_key = $this->config
->get('postmark_api_key');
$debug_mode = $this->config
->get('postmark_debug_mode');
$debug_email = $debug_mode ? $this->config
->get('postmark_debug_email') : FALSE;
$sender_signature = $this->config
->get('postmark_sender_signature');
try {
if (self::checkApiSettings($api_key, $sender_signature) === FALSE) {
$this->logger
->error('Failed to send message from %from to %to. Please check the Postmark settings.', [
'%from' => $sender_signature,
'%to' => $params['to'],
]);
return FALSE;
}
if ($this->config
->get('postmark_debug_no_send')) {
\Drupal::messenger()
->addWarning('Email successfully tested, no email has been sent (no credits used).');
return TRUE;
}
$html = !empty($params['html']) && empty($params['text']) ? $params['html'] : NULL;
$text = !empty($params['text']) ? $params['text'] : NULL;
$tag = NULL;
$track_opens = FALSE;
$reply_to = !empty($params['reply-to']) ? $params['reply-to'] : NULL;
$cc = !empty($params['cc']) ? $params['cc'] : NULL;
$bcc = !empty($params['bcc']) ? $params['bcc'] : NULL;
$response = $this->postmarkClient
->sendEmail($sender_signature, $debug_email ? $debug_email : $params['to'], $params['subject'], $html, $text, $tag, $track_opens, $reply_to, $cc, $bcc);
// Debug mode: log all messages.
if ($debug_mode) {
$this->logger
->notice('Successfully sent message from %from to %to. Response data was %response.', [
'%from' => $sender_signature,
'%to' => $params['to'],
'%response' => '<pre>' . print_r($response->dumpAvailable, 1) . '</pre>',
]);
}
} catch (PostmarkException $e) {
// If client is able to communicate with the API in a timely fashion,
// but the message data is invalid, or there's a server error,
// a PostmarkException can be thrown.
$this->logger
->error('Postmark exception occurred while trying to send email from %from to %to. @code: @message', [
'%from' => $sender_signature,
'%to' => $params['to'],
'@code' => $e->postmarkApiErrorCode,
'@message' => $e->message,
]);
return FALSE;
} catch (Exception $e) {
// A general exception is thrown if the API
// was unreachable or times out.
$this->logger
->error('General exception occurred while trying to send Postmark email from %from to %to. @code: @message', [
'%from' => $sender_signature,
'%to' => $params['to'],
'@code' => $e
->getCode(),
'@message' => $e
->getMessage(),
]);
return FALSE;
}
return TRUE;
}