code blog

Send email with drupal_mail()

language: Drupal 6.x

Send email with drupal_mail()

Below is an example of how to send an email with the drupal_mail() function in Drupal 6.x with simple php.

How it will work:

The fundamental concept is that you are going to build a custom function that will build the email.

Your function must follow a naming convention that helps the drupal_mail() function find it and call it. Your function must be ready to accept 3 parameters.

You tell drupal_mail() what the name of your email-building function is, and then it prepares the email, and passes $key, $params, and an array called $message. $key and $params help you pass variables into your email-building function. $message is an array passed by reference. This is where you put the email body, as the example below demonstrates.

This is set up to allow you to use hook_mail() in a module, but this is a simplified example that you can stick anywhere.

// set up the to and from
$to = "recepient@host.com"
// multiple recipients:
// The formatting of this string must comply with RFC 2822
// $to = "recip@host.com, anotherrecip@host.com, again@agin.com";
 
//this has to be from your server, 
//or you could get blocked by spam blocker
$email_from = "email@host.com";
 
//build an array called $params to pass 
//whatever you want to the email
$params['var'] = $whatevs;
$params['more'] = $secrets;
 
 
$sent = drupal_mail('myMessageName', 'key', $to, language_default(), $params, $email_from, TRUE);
 
if(!$sent){
//handle send fail, $sent ===false when mail fails, but it won't always recognize a failure
}
 
/*
 *	drupal mail looks for a function that matches the first parameter _ mail to build the message
 */		
function myMessageName_mail($key, &$message, $params) {
	$language = $message['language'];
	switch($key) {
		//switching on $key lets you create variations of the email based on the $key parameter
		case 'key':
		$message['subject'] = t('Subject', $vars, $language->language); //$vars required even if not used to get $language in there
		//the email body is here, inside the $message array
		$message['body'][] = 'This is the body of the email, and might contain vars from $params.\n
				$params "var":'.$params['var'].'\n
				$params "more":'.$params['more'].'\n';
		break;
	}
}

Note on multiple recipients:

The $to parameter can take a comma-separated list of email addresses, which, according to Drupal API must comply with RFC 2822.

Some examples are:

  • user@example.com user@example.com, anotheruser@example.com
  • User <user@example.com>, Another User <anotheruser@example.com>

The $params array:

You can pass whatever you want into this array. Once you've got it inside your custom mail-builder function, you can then unpack it and insert variables into the message body or have conditional snippets fire... Whatever your heart (or your client's heart) desires.

The 'key' argument

This comes into play when you are using a module that may be sending different email bodies based on a variable. In this example the $key argument is the string 'key', which is passed into a switch that determines what the subject and body of the email should be.

Comments

Why use a second function?

Hi Trevor,
Thanks for this tutorial...
I was looking for examples of how to send a simple e-mail using Drupal. I understand the function drupal_mail() serves that purpose. After reading this page I must admit I remain bit confused.
I don't really get the point of using a second custom function; can't drupal_mail() send an e-mail on its own?
I don't get the point of the $params argument either; it's not used in the function. I suppose you can do all sorts of clever things with that, but before I decide to do the clever stuff, I want to be able to just send an e-mail with a title and a body.
Francis

no -- second function is required

Hi Francis,

drupal_mail() is designed to send an email, but it requires this second function to define the body of the email. drupal_mail() only tells the system to prepare a message to be sent, but does not take in a message parameter. If you want to define the body of the email, you must have the 2nd function. I don't know why it was designed that way, but it is what it is.

drupal_mail() passes its parameters to the function defined by the first parameter in drupal_mail() -- in this example, the parameter of drupal_mail(), "myMessageName" points to the function "myMessageName_mail()" and passes in $key and $params, and a $message array by reference. $message['body'] is where you put your email body. $params exists to allow you to pass extra variables into your second function from drupal_mail()

I think it's done this way so Drupal can make sure that the $message is properly constructed. Your function, myMessageName_mail, is required to alter the $message so you can insert your email body.

Cheers!

Thanks for this Trevor. Needed a quick prototype for Drupal 7's drupal_mail().

What about multiple email

What about multiple email addresses in the $to variable? I've tried spaces, \n, etc... but it always fails! The rfc2822 is verbose and never really talks about this other than a CLRF. Umm.. oK?

User login