I have been fiddling with sending a mail in my custom module in Drupal 8. The mail gets sent but the subject and body are empty. It has been an issue for days and I can't get it to work. The code and var_dump are below. I just can't find the issue. Maybe someone can!

Code in hook_mail:

 * Implements hook_mail().
function document_mail($key, $message, $params)
    $options = array(
        'langcode' => $message['langcode'],
    switch ($key) {
       case 'send_file':
            $message['from'] = \Drupal::config('system.site')->get('mail');
            $message['subject'] = t('@title', array('@title' => $params['mail_title']), $options);
            $message['body'][] = $params['message'];

Code to send mail:

    $mailManager = \Drupal::service('plugin.manager.mail');
    $module = 'document';
    $key = 'send_file';
    $to = 'info@example.com';
    $params['mail_title'] = 'a title';
    $params['message'] = 'a message';
    $langcode = \Drupal::currentUser()->getPreferredLangcode();
    $send = true;
    $result = $mailManager->mail($module, $key, $to, $langcode, $params, null, $send);
    if ($result['result'] !== true) {
        drupal_set_message(t('There was a problem sending your message and it was not sent.'), 'error');
    } else {
        drupal_set_message(t('Your message has been sent.'));

var_dump of result from $result of mailManager

array(12) { 
["id"]=> string(18) "document_send_file" 
["module"]=> string(8) "document" 
["key"]=> string(9) "send_file" 
["to"]=> string(24) "info@example.com," 
["from"]=> string(22) "info@example.com" 
["reply-to"]=> NULL 
["langcode"]=> string(2) "en" 
["params"]=> array(2) 
    ["mail_title"]=> string(7) "a title" 
    ["message"]=> string(9) "a message" 
["send"]=> bool(true) 
["subject"]=> object(Drupal\Core\StringTranslation\TranslatableMarkup)#410 (5) 
    ["string":protected]=> string(6) "@title" 
        ["translatedMarkup":protected]=> NULL 
        ["options":protected]=> array(1) 
                ["langcode"]=> string(2) "en" 
        ["stringTranslation":protected]=> NULL 
        ["arguments":protected]=> array(1) 
            ["@title"]=> string(7) "a title" 
    ["body"]=> array(1) 
        [0]=> string(9) "a message" 
    ["headers"]=> array(7) 
        ["MIME-Version"]=> string(3) "1.0" 
        ["Content-Type"]=> string(51) "text/plain; charset=UTF-8; format=flowed; delsp=yes" 
        ["Content-Transfer-Encoding"]=> string(4) "8Bit" 
        ["X-Mailer"]=> string(6) "Drupal" 
        ["Return-Path"]=> string(22) "info@example.com" 
        ["Sender"]=> string(22) "info@example.com" 
        ["From"]=> string(44) "a string" 
    ["result"]=> bool(true)
  • @NoSssweat thank you for your comment! I have tried it and sadly the issue remains the same. – eeriek Dec 14 '17 at 11:11
  • Code is working fine. Please check disabling modules one by one. Other module might be causing the issue. – Vishal Patil Dec 14 '17 at 13:14

I believe you forgot to pass $message by reference (&), so please double check the implementation of your hook_mail as per details below:

hook_mail($key, &$message, $params)

Otherwise, any changes to $message won't take any effect.

  • 1
    Wow! That worked! just 1 character difference... Thanks a lot! – eeriek Dec 14 '17 at 15:17

Remove the empty array tag from body as below.

$message['body'] = $params['message'];
  • Hello! Thank you for your answer. I have tried it and sadly the issue remains. The empty array tag is actually necessary. At least according the hook_mail docs from Drupal. link – eeriek Dec 14 '17 at 12:38

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.