Reputation: 64884
I need to send an e-mail from my custom php code using the Drupal service.
In other words, I've noticed Drupal can easily send emails so I would like to know how can I use drupal libraries to send the emails, instead of using external libraries.
Upvotes: 9
Views: 6794
Reputation: 29748
$message = array(
'to' => '[email protected]',
'subject' => t('Example subject'),
'body' => t('Example body'),
'headers' => array('From' => '[email protected]'),
);
drupal_mail_send($message);
Caveats:
drupal_mail()
isn't called, other modules will not be able to hook_mail_alter()
your output, which can cause unexpected results.drupal_mail_send()
is ignorant about which language to send the message in, so this needs to be figured out beforehand.drupal_mail()
.
In the case where your module sends several different types of emails, and you want those email templates to be editable (for example, user module's various registration notification/password reset/etc. e-mails), using hook_mail()
is still the best way to go.This is what reported in a comment in the documentation for drupal_mail(). If the caveats are not important in your case, then you can use the reported snippet.
Upvotes: 7
Reputation: 401182
You need to use drupal_mail(), and define hook_mail() in your module.
You'll then call the first one, and the e-mail information will be set by the second one.
(See the example on the documentation page for drupal_mail()
.)
Upvotes: 8
Reputation: 504
I would recommend to use mimemail. It's a contrib module that will allow you to send HTML mails (+ attachments) or plaintext-only messages with ease. Here is an excerpt from the readme file:
the mimemail() function:
This module creates a user preference for receiving plaintext-only messages. This preference will be honored by all calls to mimemail()
Link: http://drupal.org/project/mimemail
I hope this will help you!
Upvotes: 4