5

I'm using drupal_mail for sending eMails from a custom module. This is the code:

function changeme_node_update($node) {
...
$to = "[email protected]";
$from = "[email protected]";
$subject = 'sampletext ' . $nodedate;
$body = "Dear " . $user_name . "! \r\nSome more text\r\n\r\n" . $nodetitle . "\r\nDate:" . $nodedate . "\r\nTime:" . $nodetime . " h";
$body .= "\r\n\r\nSome more text here";

//call mail function
$params = array(
    'subject' => $subject,
    'body' => $body,
);
drupal_mail('changeme', 'information', $to, "de", $params, $from);
...
}



function changeme_mail($key, &$message, $params) {
  switch ($key) {
    case 'information':
      $message['subject'] = $params['subject'];
      $message['body'] = $params['body'];
    break;
    }
}

I'm receiving emails with the subject but the message is missing. Also, where to change the encoding?

Thanks!

4
  • Are you using htmlmail et mail_system module ?
    – pbonnefoi
    Nov 12, 2014 at 11:01
  • no, just drupal_mail, no need for html, just plain text. Nov 12, 2014 at 11:02
  • You have used two $body's. Did you try using just one? Try including all your email contents into $body. <br> tag works, if you want to print the contents in the new line.
    – Aparna
    Nov 13, 2014 at 9:06
  • Yes, tried this as well. Still no luck with drupal_mail. I think i'm gonna use php mail instead. Nov 14, 2014 at 6:45

3 Answers 3

3

Try this :

function changeme_mail($key, &$message, $params) {
  switch ($key) {
    case 'information':
      $message['subject'] = $params['subject'];
      $message['body'][] = $params['body'];
    break;
    }
}

$message['body'] is supposed to be an array as defined here so I changed $message['body'] to $message['body'][]

5
  • Thanks for pointing this out. I've tried but didn't make a difference. I'll try to dpm() my $body to see what is happening.. Nov 12, 2014 at 11:40
  • I've tried two things: 1) use $message['body'][] in the changeme_mail function: still empty message body in email. 2) i made $body itself an array and left changeme_mail function unchanged: still empty message body in email. Nov 12, 2014 at 12:03
  • Are you sure you have no email template declared ? It might be an encoding problem ?
    – pbonnefoi
    Nov 18, 2014 at 17:32
  • @marcmueller was this issue fixed? I am facing the same issue, I would like to know the solution to this problem.
    – harshal
    Aug 3, 2016 at 17:21
  • No, but I haven't tried any further, I used the php mail function instead. Aug 8, 2016 at 19:46
3

I was having the same issue. The following worked for me. The code below should replace what you have set in the case statement
$message['body'][] = check_plain(t($params['body']));

    function changeme_mail($key, &$message, $params) {
     switch ($key) {
       case 'information':
         $message['subject'] = $params['subject'];
         $message['body'][] = check_plain(t($params['body']));
       break;
      }
    }
1
  • This should be the accepted answer. Works fine. Thanks.
    – penname
    Nov 27, 2017 at 14:33
0

The reason you are getting an empty string for body is that Drupal is expecting an array, not a string. In fact, the function that formats the message, DefaultMailSystem::format(), contains the following code.

  // Join the body array into one string.
  $message['body'] = implode("\n\n", $message['body']);
  // Convert any HTML to plain-text.
  $message['body'] = drupal_html_to_text($message['body']);
  // Wrap the mail body for sending.
  $message['body'] = drupal_wrap_mail($message['body']);

If you pass two strings to implode(), the function will return NULL, which (in the context of strings) is then converted to an empty string. (See one of the comments in the PHP documentation page.)

1
  • Thanks. I've made $body an array now. to make sure i used dpm() to show each of my $body variables. Still, there is no email body.. can drupal_mail be called from an other hook than xxx_form_submit? (i've only found expamples using this) Nov 12, 2014 at 12:14

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.