3

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

function changeme_node_update($node) {
...
$to = "receiver@example.com";
$from = "sender@example.com";
$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!

  • Are you using htmlmail et mail_system module ? – pbonnefoi Nov 12 '14 at 11:01
  • no, just drupal_mail, no need for html, just plain text. – marcmueller Nov 12 '14 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 '14 at 9:06
  • Yes, tried this as well. Still no luck with drupal_mail. I think i'm gonna use php mail instead. – marcmueller Nov 14 '14 at 6:45
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'][]

  • 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.. – marcmueller Nov 12 '14 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. – marcmueller Nov 12 '14 at 12:03
  • Are you sure you have no email template declared ? It might be an encoding problem ? – pbonnefoi Nov 18 '14 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 '16 at 17:21
  • No, but I haven't tried any further, I used the php mail function instead. – marcmueller Aug 8 '16 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;
      }
    }
  • This should be the accepted answer. Works fine. Thanks. – penname Nov 27 '17 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.)

  • 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) – marcmueller Nov 12 '14 at 12:14

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.