I'd like to use Drupal's email system to programmatically send an email from my custom module. Is that possible?

up vote 62 down vote accepted

Using hook_mail and drupal_mail you can create and send an e-mail.

Implement an e-mail use hook_mail:

function MODULENAME_mail ($key, &$message, $params) {
  switch ($key) {
    case 'mymail':
      // Set headers etc
      $message['to'] = 'foo@bar.com';
      $message['subject'] = t('Hello');
      $message['body'][] = t('Hello @username,', array('@username' => $params['username']));
      $message['body'][] = t('The main part of the message.');
      break;
  }
}

To send a mail use drupal_mail:

drupal_mail($module, $key, $to, $language, $params = array('username' => 'John Potato'), $from = NULL, $send = TRUE)

Obviously replace the parameters: $key should equal 'mymail'

An e-mail is sent in a few steps:

  1. drupal_mail is called
  2. Drupal builds the e-mail
  3. hook_mail is called for the specifics (implementation)
  4. hook_mail_alter is called so other modules can modify it
  5. drupal_send_mail is called
  • 5
    He's right, but to clarify a bit hook_mail provides you with a way to structure and theme an email based on an arbitrary key that you define. drupal_mail() is what you call to send an email. Pass the key for the structure you'd like to use. (and the module that responds to that key) – Jason Smith Jul 9 '11 at 1:26
  • 8
    In this example $message['to'] is hard coded to foo@bar.com. Omit this and the message will be sent to the recipient that is specified when drupal_mail() is called. – pfrenssen Dec 8 '11 at 14:23

If you'd like a simpler way of sending emails, check out Simple Mail; it's a module I'm working on to make sending emails with Drupal 7+ much easier, and it doesn't require any extra hook implementations or MailSystem knowledge. Sending an email is as simple as:

simple_mail_send($from, $to, $subject, $message);
  • ...and it works with Drupal 8, too, with the exact same API :) – geerlingguy Jul 20 '17 at 20:30

You can use a simpler way of sending emails, check out mailsystem ; it's a module.

<?php
$my_module = 'foo';
$from = variable_get('system_mail', 'organization@example.com');
$message = array(
  'id' => $my_module,
  'from' => $from,
  'to' => 'test@example.com',
  'subject' => 'test',
  'body' => 'test',
  'headers' => array(
    'From' => $from, 
    'Sender' => $from, 
    'Return-Path' => $from,
  ),
);

$system = drupal_mail_system($my_module, $my_mail_token);
if ($system->mail($message)) {
  // Success.
}
else {
  // Failure.
}
?>
  • Works perfectly. – W.M. Aug 17 '17 at 17:08

You can use this code in a hook of your own choice within your custom module:

 function yourmodulename_mail($from = 'default_from', $to, $subject, $message) {
            $my_module = 'yourmodulename';
            $my_mail_token = microtime();
            if ($from == 'default_from') {
                // Change this to your own default 'from' email address.
                $from = variable_get('system_mail', 'admin@yoursite.com');
            }
            $message = array(
                'id' => $my_module . '_' . $my_mail_token,
                'to' => $to,
                'subject' => $subject,
                'body' => array($message),
                'headers' => array(
                    'From' => $from,
                    'Sender' => $from,
                    'Return-Path' => $from,
                ),
            );
            $system = drupal_mail_system($my_module, $my_mail_token);
            $message = $system->format($message);
            if ($system->mail($message)) {
                return TRUE;
            } else {
                return FALSE;
            }
        }

Then you can use the above function like this:

        $user = user_load($userid); // load a user using its uid
        $usermail = (string) $user->mail; // load user email to send a mail to it OR you can specify an email here to which the email will be sent 
        customdraw_mail('default_from', $usermail, 'You Have Won a Draw -- this is the subject',  'Congrats! You have won a draw --this is the body');

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.