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 />

Forms generated by confirm_form() only call their submit functions if the form<br />

is actually confirmed, so developers don't need to check for this themselves. If the<br />

form is canceled, then the user is simply redirected to the URL specified in the<br />

function call.<br />

We have now gotten a pretty thorough introduction to <strong>Drupal</strong>'s Form API. We<br />

can create forms from scratch, write, validate, and submit handlers, and use some<br />

of <strong>Drupal</strong>'s internal functions to create common form types. We're two-thirds of<br />

the way into this chapter and we still haven't touched on the module's central<br />

purpose—sending an e-mail to a user!<br />

Sending mail with drupal_mail() and<br />

hook_mail()<br />

<strong>Drupal</strong> implements a custom e-mail templating system. Initially it may appear that<br />

this system is overly complicated, but it allows an enormous amount of flexibility<br />

for module developers.<br />

Sending e-mail in <strong>Drupal</strong> is a multi-step process:<br />

1. drupal_mail() is called, specifying what mail is being sent and what<br />

options are unique to this specific message (the recipient's e-mail address, the<br />

language the mail should be sent in, and so on).<br />

2. <strong>Drupal</strong> then builds an e-mail message with standard headers combined with<br />

the information submitted to drupal_mail().<br />

3. The hook_mail() implementation specified in drupal_mail() is called. This<br />

is where the subject and body of the mail are added.<br />

4. The fully composed mail array is then passed to hook_mail_alter(),<br />

allowing other modules to modify it (for instance, to add a common<br />

signature to all outgoing e-mails.).<br />

5. The mail is passed to drupal_send_mail() for delivery.<br />

That is a pretty long process just for sending a simple e-mail! However, in most<br />

cases developers will only have to worry about two of the above steps—calling<br />

drupal_mail() and implementing hook_mail().<br />

[ 141 ]

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

Saved successfully!

Ooh no, something went wrong!