Sending an e-mail

The final two functions in our custom action module will build and send the notification e-mail to those speakers whose presentation submissions have been declined.

How to do it...

Let's wrap up our Webform extension module by coding the functions that will send out the notification e-mail.

/**
* Build and send notification email.
*/
function webform_custom_action_speaker_status_email($submission) {
// Get some of the submitted data.
$speaker_name = $submission->data[1]['value'][0] . ' ' . $submission->data[2]['value'][0];
$to_email_address = $submission->data[3]['value'][0];
$presentation_title = $submission->data[4]['value'][0];
// Find out if html emails are possible.
$html_capable = webform_email_html_capable();
// Build email body as html.
$body = "<p>Dear $speaker_name,</p>";
$body .= "<p>We regret to inform you that your presentation entitled '$presentation_title' can not be included in the program of the Fictitious Conference this year.</p>";
$body .= "<p>Thank you once again for your interest and we look forward to considering your participation at our next conference.</p>";
$body .= "<p>Kind regards,</p>";
$body .= "<p>Fictitious Conference Organizers.</p>";
if ($html_capable) {
// Apply some css to the html.
$body = "<div style='font-family: Arial, Helvetica; font-size: 10pt;'>" . $body . "</div>";
}
else {
// Convert the html to plaintext.
$body = str_replace('<p>', '', $body);
$body = str_replace('</p>', "

", $body);
}
// Set email parameters.
$language = language_default();
$mail_params = array(
'message' => $body,
'subject' => t('Speaker application for the Fictitious Conference'),
'headers' => array(),
'plain' => !$html_capable,
'html' => $html_capable,
);
$mail_params['plaintext'] = $html_capable ? NULL : $body;
if ($html_capable) {
// MIME Mail requires this header or it will filter all text.
$mail_params['headers']['Content-Type'] = 'text/html; charset=UTF-8';
}
// Send the email.
$message = drupal_mail('webform_custom_action', 'declined', $to_email_address, $language, $mail_params);
// Notify user that mail was sent.
if ($message['result']) {
drupal_set_message(t('Speaker declined notification email sent.'));
}
else {
drupal_set_message(t('Unable to send speaker declined notification email. Contact the website administrator.'), 'error'),
}
}
/**
* Implements hook_mail()
*/
function webform_custom_action_mail($key, &$message, $mail_params) {
$message['headers'] = array_merge($message['headers'], $mail_params['headers']);
$message['subject'] = $mail_params['subject'];
$message['body'][] = $mail_params['message'];
}

One last reminder that we need to confirm the component identifiers in the code match the identifiers we noted down earlier from the component overview page.

How it works...

After extracting some data from the submission, we call a Webform function to determine whether HTML e-mails are possible. This answer will be stored as a Boolean TRUE or FALSE value in the $html_capable variable. The purpose of this variable is to help us determine whether the e-mail should be formatted as HTML or in plain text.

Next up we build the message body of the e-mail. By default this is built as HTML, but will be converted to plain text should our site not be HTML e-mail capable.

The final series of steps puts together the required e-mail message parameters which will inform the mail system how to construct the e-mail message, including such things as the message body, subject line, destination e-mail address, and so on.

The internal messaging process flow goes from our module, to Drupal's drupal_mail() function, back to our module's implementation of hook_mail(), and then on to the system that actually sends the mail (either Drupal itself or a contributed module such as MIMEmail).

We may be forgiven for thinking that the mail sending process seems a little convoluted. There are historic reasons for this. At the time of writing, there is some effort from Drupal core developers to simplify the process somewhat (see the discussions at http://drupal.org/node/1346036), so things may be simpler when Drupal 8 rolls around. Be that as it may, the fact remains that the mail system achieves what it needs to achieve.

Should the sending of the e-mail fail for any reason, Drupal will print an error message to that effect. Included in the error message is a request for the user to contact the website administrator to report the problem for further investigation in system log files and so on.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset