Pages

Tuesday, October 05, 2010

Drupal: Sending an Email using the drupal_mail function

Here's a code snippet that shows you how to use the drupal_mail function to send an email. First, define a callback function that will handle the creation of the message. NOTE: this function must end with "_mail"

<?php
function MYMODULE_mail($key, &$message, $params) {
switch ($key) {
case 'invitation':
// note: data can be passed to this function in the $params array
$message['subject'] = t('EMAIL SUBJECT');
$message['body'] = t('MESSAGE BODY');
break;
}
}
?>

Now, we can use this callback function in our code:
<?php

$params = array(
'myVar' => 'data you would like in your message and/or subject',
);
drupal_mail('MYMODULE', 'invitation', $emailTo, language_default(), $params);
?>

More information about drupal_mail can be found here. NOTE: if you're going to send an email to a registered user, you should use the data found in $user for the language and email address.

Source: http://thedrupalblog.com/sending-email-module-using-drupal-mail

1 comment: