50

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

1

4 Answers 4

67

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'] = '[email protected]';
      $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
3
  • 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) Commented Jul 9, 2011 at 1:26
  • 9
    In this example $message['to'] is hard coded to [email protected]. Omit this and the message will be sent to the recipient that is specified when drupal_mail() is called.
    – pfrenssen
    Commented Dec 8, 2011 at 14:23
  • This solution is deprecated.
    – felipep
    Commented Nov 23, 2023 at 19:27
14

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);
1
  • 1
    ...and it works with Drupal 8, too, with the exact same API :) Commented Jul 20, 2017 at 20:30
2

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', '[email protected]');
$message = array(
  'id' => $my_module,
  'from' => $from,
  'to' => '[email protected]',
  '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.
}
?>
1
  • Works perfectly.
    – W.M.
    Commented Aug 17, 2017 at 17:08
1

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', '[email protected]');
            }
            $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 agree to our terms of service and acknowledge you have read our privacy policy.

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