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.

Chapter 5<br />

• The second argument, warn, is a key which is passed to hook_mail(). Any<br />

hook_mail() implementation can define several e-mails, uniquely identified<br />

by a text key. (<strong>Drupal</strong>'s user module implements eighteen (!) for things like<br />

account confirmation and forgotten passwords). We specify which specific<br />

mail we want to send with this parameter.<br />

• The third argument contains the recipient's address. We pull this out of<br />

the user object for the user whose profile we visited, as passed by the<br />

confirmation form above.<br />

• The fourth argument specifies what language the mail should be sent in. This<br />

is important because individual users can specify a language preference that<br />

is different from the site's default language. We should honor this choice<br />

if possible when sending our e-mail to this user. The user_preferred_<br />

language() function makes this task easy by taking a user object and<br />

returning the user's language choice.<br />

• The fifth argument is an associative array of parameters to be passed to<br />

hook_mail(). Any custom information needed to build the e-mail should be<br />

put here. In our case, any custom information we need to build the e-mail is<br />

already in the data submitted from the confirmation form, so we will just use<br />

$form_state['values'] here.<br />

• The sixth argument contains the e-mail address from whom this mail should<br />

be sent. When you first installed <strong>Drupal</strong> you had to specify an administrative<br />

e-mail address. This address is already being used as the source for other<br />

system e-mails (like account verification) so it makes sense to use it as our<br />

sender e-mail as well. The e-mail is stored as a persistent variable with<br />

the key 'site_mail', so we can easily grab it using variable_get() as<br />

discussed earlier in the chapter.<br />

• Finally, the last variable indicates whether or not the mail should actually be<br />

sent. It will come as no surprise to learn that a mail message in <strong>Drupal</strong> is built<br />

in a specially structured associative array. At the end of the mail building<br />

process, this array is typically passed to the function drupal_mail_send()<br />

which handles the actual delivery of the mail. However, by setting this<br />

parameter to FALSE, drupal_mail() will bypass the delivery step, allowing<br />

you to take the structured array it returns and handle delivery yourself.<br />

[ 143 ]

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

Saved successfully!

Ooh no, something went wrong!