Drupal 6: Sending an email from a module using drupal_mail

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.

Updated: