• Avail guidance to develop Drupal modules & Drupal themes
  • Share and get review of your code
  • Get Access to free CodeBooks and ThemeBooks
  • Drupal 5, 6 and 7 covered

Drupal hook: hook_mail

Printer-friendly versionSend to a DeveloperPDF version

hook_mail is a way to re-define the content of the mail being sent from you Drupal site.

With hook_mail, you can define and accommodate more information in your mail and can alter already provided/defined mail content in certain way. You can even use a separate template to render the mail and send it.
The syntax of hook_mail is:
Drupal 6.x, 7.x: hook_mail($key, &$message, $parmas)

Now, let's take a look at parameters.

  • $key: This is the 'key' parameter of hook_mail, since it defines which mail to alter/enhance/prepare. If you overlook this parameter in you implementation of hook_mail, all the mails will be prepared/enhanced/alter with the function. This is a very important parameter. A correct key will prepare a correct mail. See http://ly2.in/[CR?TV which makes use of hook_mail
  • &$message: This parameter contains key-pair value defining different parts of the mail being sent. &$message contains
    • id: An identifier of the mail being sent.
    • from: This value will define from where the email originated
    • to: The email address to send the mail to
    • subject: This value defines the subject of the email. Make sure you set a proper subject otherwise it can be marked as a spam or junk. The usual practice is to include the site name from where the mail is being sent in the subject.
    • body: This is the actual message of the mail. You can add header/footer or any other text like company information by concatenating to existing content of the message. The message is structured as array of lines and the line delimiters are formatted and added by Drupal itself.
    • headers: This is an associative array which contains mails headers like FROM, MIME version, content-type, x-mailer and other header information
  • $paramas: This is an array of information specific to mail identified the $key parameter
5
Your rating: None Average: 5 (3 votes)
Syndicate content

Recent comments