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

PHP mail configuration<br />

In order for <strong>Drupal</strong> to send an e-mail, your server must be configured so<br />

that PHP's mail() function will work. Typically this involves installing<br />

and configuring a local mail server. Most shared hosting providers are<br />

already properly configured to do this, but if you are on a VPS or other<br />

custom-built server you may have to handle it yourself. This process<br />

varies wildly depending on your operating system and a variety of other<br />

factors. Searching for php mail setup on Google will most likely start you<br />

in the right direction.<br />

Calling drupal_mail()<br />

The following is the function that gets called when our confirmation form is<br />

submitted (indicating that we should in fact send an e-mail warning to the<br />

user in question).<br />

/**<br />

* Send a warning e-mail to the specified user.<br />

*/<br />

function user_warn_confirm_form_submit($form, &$form_state) {<br />

$account = $form_state['values']['account'];<br />

}<br />

drupal_mail(<br />

'user_warn',<br />

'warn',<br />

$account->mail,<br />

user_preferred_language($account),<br />

$form_state['values'],<br />

variable_get('site_mail', NULL),<br />

TRUE<br />

);<br />

As you can see, drupal_mail() requires that we pass it quite a bit of information.<br />

Let's look at each of these arguments in detail:<br />

• The first argument indicates what module should invoke hook_mail() to<br />

send this message. We are setting this to 'user_warn' since we are doing<br />

our own hook_mail() implementation. However, you can send a mail<br />

implemented by another module if you need to. We will look at the<br />

user_warn_mail() implementation in a bit.<br />

[ 142 ]

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

Saved successfully!

Ooh no, something went wrong!