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.
We can make use of the Drupal theme layer to customize the e-mail headers of the e-mails sent from our forms.
sites/all/modules/webform/webform.module
file. 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. speakers_webform_mail_headers
.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).<[email protected]>
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; }
template.php
and click on the Save configuration button on the Appearance administrative page.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.