Theming Webform e-mail headers

In Chapter 2, Trying Out Webform, we configured our Speaker Registration form to automatically send a confirmation e-mail to the person registering. We also saw that it was possible to configure a second e-mail to be sent to a website administrator.

In this exercise we will, instead, send a copy of the confirmation e-mail to our website administrator using the BCC (Blind Carbon Copy) e-mail header. The standard CC (Carbon Copy) header will show the additional recipients of an e-mail in the main recipients e-mail client. Using BCC enables us to hide the fact that a third party has also received a copy of an e-mail.

How to do it...

We can make use of the Drupal theme layer to customize the e-mail headers of the e-mails sent from our forms.

  1. The theme function we need to override is found within the sites/all/modules/webform/webform.module file.
  2. We need to copy the entire function called theme_webform_mail_headers from the module file and paste it into the sites/all/themes/speakers/template.php file that we created in the previous exercise.
  3. The function needs to be renamed to include our theme name, so we edit the function name to read speakers_webform_mail_headers.
  4. Our next step is to add the BCC header to the required e-mail.
  5. To achieve this we first need to know the relevant e-mail identifier (eid), which can be obtained by clicking on the edit link for the e-mail on our Webform E-mails page (the eid is the number at the end of the URL in our browser address bar).
  6. Armed with the e-mail identifier, we can add the code to send a BCC e-mail to a specified e-mail address underneath the existing function code.
  7. Our completed e-mail headers override function looks similar to the following code (obviously we would substitute the address and the eid value in the condition check as per our individual site requirements):
    function speakers_webform_mail_headers($variables) {
    $headers = array(
    'X-Mailer' => 'Drupal Webform (PHP/' . phpversion() . ')',
    );
    // Add BCC on registrant email.
    if ($variables['email']['eid'] == '1') {
    $headers['bcc'] = '[email protected]';
    }
    return $headers;
    }
    
  8. Again, to have our theming changes made effective we need to save our changes to template.php and click on the Save configuration button on the Appearance administrative page.
  9. Let's return to our Speaker Registration page and complete a new submission to verify that the BCC e-mail arrives at the specified mailbox.

How it works...

E-mail headers are used by the mail transfer agents (MTA) to determine where each e-mail must be sent, who it comes from, and so forth. MTAs also put in additional headers showing, in the received e-mail, the routing used to successfully deliver the e-mail. Most e-mail clients have a View Source or Show Headers option that will let us have a look at the various headers in our received e-mails.

To send the BCC e-mail for all Webform e-mails on our site, we merely amend the function as follows:

function speakers_webform_mail_headers($variables) {
$headers = array(
'X-Mailer' => 'Drupal Webform (PHP/' . phpversion() . ')',
'bcc' => '[email protected]',
);
return $headers;
}

In this exercise, we have added a BCC e-mail recipient into the e-mail headers for a specific e-mail as per its e-mail identifier. Of course, we are not limited to the BCC header, as we could just as easily override the from header or CC header.

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

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