function sendgrid_integration_drupal_mail_wrapper in SendGrid Integration 6
1 call to sendgrid_integration_drupal_mail_wrapper()
- drupal_mail_wrapper in ./sendgrid_integration.module
File
- ./sendgrid_integration.module, line 78
- Main module file for SendGrid Integration.
Code
function sendgrid_integration_drupal_mail_wrapper($message) {
if (variable_get('sendgrid_integration_ssl', TRUE) == TRUE) {
$protocol = 'https://';
}
else {
$protocol = 'http://';
}
$server = $protocol . 'api.sendgrid.com/api/mail.send.json';
$user = variable_get('sendgrid_integration_username', '');
$key = variable_get('sendgrid_integration_apikey', '');
$unique_args = array(
'id' => $message['id'],
);
if (!empty($message['params']['account']->uid)) {
$unique_args['uid'] = $message['params']['account']->uid;
}
if (!empty($message['module'])) {
$unique_args['module'] = $message['module'];
}
if (!empty($args)) {
$unique_args = $args;
}
$smtp_api = array(
'category' => variable_get('site_name', 'Drupal'),
'unique_args' => $unique_args,
);
$data = array(
'api_user' => $user,
'api_key' => $key,
'x-smtpapi' => sendgrid_json_encode($smtp_api),
'to' => $message['to'],
'subject' => $message['subject'],
);
if (isset($message['from'])) {
$data['from'] = $message['from'];
}
else {
$data['from'] = variable_get('site_mail');
$data['fromname'] = variable_get('site_name');
}
if (strpos($message['headers']['Content-Type'], 'text/plain') !== FALSE) {
$data['text'] = drupal_wrap_mail(drupal_html_to_text($message['body']));
}
else {
$data['html'] = $message['body'];
}
$headers = array(
'Content-Type' => 'application/x-www-form-urlencoded',
);
$result = drupal_http_request($server, $headers, 'POST', http_build_query($data), $retry = 3, $timeout = 20.0);
$result_data = array();
if (isset($result->data)) {
$result_data = json_decode($result->data, TRUE);
}
if (isset($result_data['message'])) {
if ($result_data['message'] == 'success') {
return TRUE;
}
}
$errors = '';
if (isset($result_data['errors'])) {
foreach ($result_data['errors'] as $error) {
$errors .= $error . ' ';
}
}
$variables = array(
'%code' => $result->code,
'%msg' => $result->error,
'%errors' => $errors,
);
watchdog('SendGrid Integration', 'Email sending failed with %code/%msg. %errors', $variables, WATCHDOG_ERROR, $link = NULL);
return FALSE;
}