I've written the following testcase to send an email. In test results output it says

"1 e-mail was sent during this test."

but acutally it's not.

I tried another way, I put the function in menu callback (test-mail) it's sending mail when I access the URL. But if I access the URL in simple testcase like $this->drupalGet('test-mail'); the result still says the same "1 e-mail was sent during this test." but was not.

enter image description here

So is it by design? Is there any other way to send mail?

 public function testSendMail() {

function sendmail() {
  $from = 'fromaddress@gmail.com';
  $to = 'toaddress@gmail.com';
  $message = 'testcase mail';
  $language = 'en';
  $module = "test_mails";
  $key = "notification_mail";
  $send = TRUE;
  $subject = t("subject line goes here", array(NULL),array('langcode'=>'en'));
  $params = array(
    'body' => 'hello from krishna',
    'subject' => $subject
  $result = drupal_mail($module, $key, $to, $language, $params, $from, $send);
  return 'Mail sent succesfully';


 function test_mails_mail($key, &$message, $params) {
 switch ($key) {
   case 'notification_mail' :
     $message['subject'] = $params['subject'];
     //the email body is here, inside the $message array
     $message['body'][] = $params['body'];

You need three functions.

1) module_submit. 2) Create a function to send the mail. 3) create hook_mail.

a complete example: file sendmailtest.info

name = sendmailtest
description = sendmailtest
core = 7.x

file sendmailtest.module


function sendmailtest_menu() {
 $items['sendmailtest'] = array(
   'title' => 'send mail test',
   'page callback' => 'drupal_get_form',
   'page arguments' => array('sendmailtest'),
   'access callback' => TRUE
 return $items;

function sendmailtest($form, &$form_state){

 $form['submit'] = array(
  '#value' => 'Send Mail Test',
  '#type' => 'submit',
 //Fundamental devolver el objeto-array form
 return $form;

function sendmailtest_submit($form, &$form_state){

 drupal_set_message('Test ok');



function sendmailtest_send($form_values) {
  // All system mails need to specify the module and template key (mirrored from
  // hook_mail()) that the message they want to send comes from.
  $module = 'sendmailtest';
  $key = 'sendmailtest';

  // Specify 'to' and 'from' addresses.
  //$to = $form_values['email'];
  $to = 'jeremiselxi@gmail.com';
  $from = variable_get('site_mail', 'admin@example.com');

  // "params" loads in additional context for email content completion in
  // hook_mail(). In this case, we want to pass in the values the user entered
  // into the form, which include the message body in $form_values['message'].
  $params = $form_values;

  // The language of the e-mail. This will one of three values:
  // - user_preferred_language(): Used for sending mail to a particular website
  //   user, so that the mail appears in their preferred language.
  // - global $language: Used when sending a mail back to the user currently
  //   viewing the site. This will send it in the language they're currently
  //   using.
  // - language_default(): Used when sending mail to a pre-existing, 'neutral'
  //   address, such as the system e-mail address, or when you're unsure of the
  //   language preferences of the intended recipient.
  // Since in our case, we are sending a message to a random e-mail address that
  // is not necessarily tied to a user account, we will use the site's default
  // language.
  $language = language_default();

  // Whether or not to automatically send the mail when drupal_mail() is
  // called. This defaults to TRUE, and is normally what you want unless you
  // need to do additional processing before drupal_mail_send() is called.
  $send = TRUE;
  // Send the mail, and check for success. Note that this does not guarantee
  // message delivery; only that there were no PHP-related issues encountered
  // while sending.
  $result = drupal_mail($module, $key, $to, $language, $params, $from, $send);
  if ($result['result'] == TRUE) {
    drupal_set_message(t('Your message has been sent.'));
  else {
    drupal_set_message(t('There was a problem sending your message and it was not sent.'), 'error');


function sendmailtest_mail($key, &$message, $params) {
  switch ($key) {
    // Send a simple message from the contact form.
    case 'sendmailtest':

      $message['subject'] = 'Test subject';

      $body= 'Test Body';
      $message['body'][] = check_plain($body);
| improve this answer | |
  • My module works well by clicking on the "Send mail test" button. Automatically, send an email to my email address: "jeremiselxi@gmail.com". Change this value by your mail and it will work correctly. Please, you can put your complete code to review it and in this way we can help you. Is your version of drupal 7? – JHONATAN DAVID FERNANDEZ ROSA Nov 7 '17 at 13:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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