How do I use the phpmailer module. I have it installed and my test email sends. However the php mailer tutorials show examples for using the module outside of Drupal. I want to place some code within a node to quickly fire off an email to myself when x happens.

or is it better to use the durpal mail function, I am using drupal 6, however this is not part of a module but instead will be placed in a node.

  • you can use rules to send email according to particular event.
    – Cool
    Dec 5, 2013 at 10:27

2 Answers 2


You should consider using drupal_mail for sending email from Drupal. That will make sure Drupal can control your mails. For example say, you are using some module like Re-Route Email or maillog.

  $params = array(
    'subject' => t('Client Requests Quote'),
    'body' => t("Body of the email goes here"),

  drupal_mail("samplemail", "samplemail_html_mail", "[email protected]", language_default(), $params, "[email protected]");

/*setting up our mail format, etc in hook mail*/
function hook_mail($key, &$message, $params)
    case 'samplemail_html_mail':
           * Emails with this key will be HTML emails,
           * we therefore cannot use drupal default headers, but set our own headers
           * $vars required even if not used to get $language in there since t takes in: t($string, $args = array(), $langcode = NULL) */
          $message['subject'] = t($params['subject'], $var, $language->language);
          /* the email body is here, inside the $message array */
          $body = "<html><body>
              <h2>HTML Email with Drupal</h2>
              <hr /><br /><br />
          $message['body'][] = $body;
          $message['headers']['Content-Type'] = 'text/html; charset=UTF-8; format=flowed';

You would invoke this in your code somewhere. Or else you can use the rules module to send email as per some events or actions in the site.

If you still want to use PHP Mailer, you have module to do that here.

you can see a screencast on using rules to send email here.

  • So I can place this in a node? and what module is it I need to make this code work? Thanks
    – Tom
    Dec 6, 2013 at 9:19
  • You should not place it in a node, even though it will work. Ideally, any logic or code should gointo template.php file. Or else to create a custom module to handle it. For example, if you want to fire an email each time a content is viewed, you can make this a function and call it in the page_preprocess_function or else if you can use any other hooks to write a condition to call the function when needed. When do you want the mail to be send?
    – esafwan
    Dec 7, 2013 at 4:05

I wrote this code to send email using drupal_mail() function, if it may help you.


                            function astha_menu() {
                                $items['custom-astha'] = array(
                                    'title' => t('Sending Mail!'),
                                    'page callback' => 'custom_astha',
                                    'access callback' => 'user_access',
                                    'access arguments' => array('Send Mail'),
                                return $items;

                            function custom_astha() {

                                $form['custom_form'] = drupal_get_form('custom_astha_form');
                                $output = drupal_render($form);
                                return $output;

                            function custom_astha_form() {
                                $form['email'] = array(
                                    '#type' => 'textfield',
                                    '#title' => 'UnRegister User Email',
                                    '#size' => 20,
                                    '#required' => TRUE,

                                $form['file'] = array(
                                    '#type' => 'file',
                                    '#name' => 'file',
                                    '#title' => 'Attach any file!!',

                                $form['submit'] = array
                                    '#type' => 'submit',
                                    '#value' => t('SEND'),

                                return $form;

                            // to send mail

                            function astha_send_mail($to, $from, $subject, $body, $file) {

                                // move_uploaded_file($_FILES["file"]["tmp_name"], 'upload/'. $_FILES["file"]["name"]);
                                $params = array(
                                    'subject' => $subject,
                                    'body' => array(token_replace($body, array('callback' => 'user_mail_tokens', 'sanitize' => FALSE)),),
                                    'attachment' => array(
                                        'filecontent' => file_get_contents($_FILES['file']['name']),
                                        'filename' => $_FILES['file']['tmp_name'])
                                drupal_mail('custom', 'information', $to, language_default(), $params, $from);

                            function custom_astha_form_submit($form, &$form_state) {
                                $ind_user = $form_state['values']['email'];
                                $to = (string) $ind_user; // to e-mail address
                                $from = (string) variable_get('site_mail', '[email protected]'); // from e-mail address
                                $subject = "abc"; // subject of e-mail
                                $body = "xyz </br>Faithfully yours,<br/> [site:name] team";
                                $file = $_FILES['file']['tmp_name'];
                                // here we call our mailing function
                                astha_send_mail($to, $from, $subject, $body, $file);

                                drupal_set_message('Mail to ' . $ind_user . ' has been sent.');

                            function custom_astha_form_validate($form, &$form_state) {
                                $mail = $form_state['values']['email'];
                                if (!valid_email_address($mail)) {
                                    form_set_error('submitted][email_address', t('The email address appears to be invalid.'));

                            function custom_mail($key, &$message, $params) {
                                switch ($key) {
                                    case 'information':
                                        $message['subject'] = $params['subject'];
                                        $message['body'] = $params['body'];
                                        $message['params']['attachments'][] = $params['attachment'];

  • This code has to go into a module right? I was looking to place my code in a node. Though would it be possible to call the module in the background from my node, so that when X occurs on my node, can I call the email module in the background, instead of just placing some code in that if statement to send mail.
    – Tom
    Dec 6, 2013 at 9:20
  • This is a custom module that I developed for creating a form on a menu callback that sends email by clicking on submit button. Yes you can call this function in a particular node and rest is same as above. Dec 6, 2013 at 9:35
  • @Tom can you let what is X? You can definetely call this function when something occur.
    – esafwan
    Dec 7, 2013 at 4:08

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.