18.10.2016 Views

Drupal 7 Module Development

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Building an Admin Interface<br />

Implementing hook_mail()<br />

In the last section, when we called drupal_mail(), we indicated that it should<br />

invoke hook_mail() in the user_warn module. This means that <strong>Drupal</strong> will be<br />

looking for a function named user_warn_mail(). This function is as follows:<br />

/**<br />

* Implement hook_mail().<br />

*/<br />

function user_warn_mail($key, &$message, $params) {<br />

switch ($key) {<br />

case 'warn':<br />

$account = $params['account'];<br />

$subject = variable_get('user_warn_e-mail_subject',<br />

'Administrative Warning');<br />

$body = variable_get('user_warn_e-mail_text',<br />

'You\'ve been warned!');<br />

if (variable_get('user_warn_bcc', FALSE)) {<br />

$admin_mail = variable_get('site_mail', NULL);<br />

$message['headers']['bcc'] = $admin_mail;<br />

}<br />

}<br />

}<br />

$message['to'] = $account->mail;<br />

$message['subject'] = $subject;<br />

$message['body'][] = $body;<br />

break;<br />

As you can see the preceding function receives three arguments:<br />

• The key we passed in parameter two of our call to drupal_mail(),<br />

indicating what message should be sent.<br />

• The structured array that <strong>Drupal</strong> creates to represent an e-mail message.<br />

At this point this array has already been populated with the mail's default<br />

header information.<br />

• The data we passed from drupal_mail() in the $params argument (in this<br />

case, a user's account object.)<br />

[ 144 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!