Tuesday, December 30, 2008

Sending Mail with the hook_mail in Drupal 6

The Drupal 6 Mail API is used to provide mail-sending services to Drupal modules.
In most cases, using the Mail API is a two-step process:
1. Implement hook_mail() in your module.
2. Elsewhere in your module, use the drupal_mail() function to invoke
your hook_mail() implementation and also do additional formatting
and sending.
In the previous section, we briefly glanced at the drupal_mail() function. Here, we
will start by looking at the function in more detail. Inside emailusers_compose_
form_submit(), we called drupal_mail() with the following parameters:
drupal_mail(
'emailusers',
'composemessage',
$account->mail,
user_preferred_language($account),
$form_values,
variable_get('site_mail', null),
true // Automatically send
);
Seven parameters! To get an idea as to what is going on here, let's look at each
in turn.
The first parameter (emailusers) is the name of the module that contains an
implementation of hook_mail(). Later, we will look at the emailusers_mail()
hook that will be called when this drupal_mail() function is executed.
The second parameter, composemessage, is used as a key and passed on to the
hook_mail() implementation. As we will see shortly, the mail hook can then
determine how to treat the message based on the key. In other words, you can
use one mail hook to handle various different mail-sending tasks simply by using
different keys.
The third parameter should contain the destination address. In this case, an
administrator will be sending the message to the email address for the account he or
she is examining. This is stored in $account->mail.
The fourth parameter is the language that should be used by t() and other
translation facilities when translating the message. Why is it necessary to specify
this? Since the user receiving the message may prefer a different language than that
of the system administrator who is sending the message.Fortunately, the user_preferred_language() function, which takes an account
object (like the one returned from load_user()), can return the appropriate
locale information.
The fifth parameter holds an associative array of data that might be used when
generating the message. This data is passed on to the hook_mail() implementation,
and we will make use of it in a few moments. In this case, though, the data we want
happens to be the values submitted through our form. So we pass $form_values
here.
Moving to the sixth parameter, we need to specify a delivery address. Who is
this message from? One of the values in Drupal's site-wide configuration is the
administration email address. We can use this address by retrieving the setting:
variable_get('site_mail', null). This will attempt to get the 'site_mail'
setting. If no such setting is found, this will return the default value null (in which
case the mailing library will attempt to assign an appropriate from address).
The last of the seven parameters is a Boolean flag to indicate whether or not the
message should be sent. When drupal_mail() is executed, it will return a specially
structured array, which can be passed to drupal_mail_send(). However, if this last
parameter is set to true, then the drupal_mail() function will send the mail before
returning. In that case, there is no need to call the drupal_mail_send() function or
even capture the data returned from drupal_mail().When drupal_mail() is called, it goes through a series of steps to take the data
passed in the seven parameters and create a suitable mail message. For example, it
sets default RFC 2822 mail headers and makes sure that certain values (like a from
address) are set.
Then it executes the hook_mail() implementation (if found).
After that, it proceeds through a few other steps, like executing any hook_mail_
alter() implementations before it (optionally) sends the email and returns a
formatted message.So the mail hook is executed right in the middle of this process. What does it do? In
a nutshell, it is responsible for setting appropriate fields (like the subject, CC, or BCC
fields) as well as creating a formatted body for the message.

1 comment:

Anonymous said...

Stolen from Learning Drupal 6 module development, Packt Publishing.