I'd like to use Drupal's email system to programmatically send an email from my custom module. Is that possible?
4 Answers
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:
- drupal_mail is called
- Drupal builds the e-mail
- hook_mail is called for the specifics (implementation)
- hook_mail_alter is called so other modules can modify it
- drupal_send_mail is called
-
5He'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
-
9In this example
$message['to']
is hard coded to[email protected]
. Omit this and the message will be sent to the recipient that is specified whendrupal_mail()
is called. Commented Dec 8, 2011 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);
-
1...and it works with Drupal 8, too, with the exact same API :) Commented Jul 20, 2017 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', '[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.
}
?>
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');