user742736
user742736

Reputation: 2729

Drupal_mail() empty body and no subject

I'm having issues with drupal_mail(). I receive the email but the subject and body are empty.

Drupal version 7

code below

$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]", TRUE);

I have even tried it with the hook below and I get the same result.

function hook_mail($key, &$message, $params) {
switch ($key) {
    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 Sample with Drupal</h2>
          <hr /><br /><br />
          {$params['body']}
          </body></html>";
        $message['body'][] = $body;
        $message['headers']['Content-Type'] = 'text/html; charset=UTF-8; format=flowed';
        break;
}
}

drupal_mail_system() works but it comes in plain text.

Upvotes: 2

Views: 5839

Answers (3)

Shankar Ganesh Jayaraman
Shankar Ganesh Jayaraman

Reputation: 1491

in your module you shouldn't use hook_mail it should be <MODULE_NAME>_hook().

change

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

With

function samplemail_mail($key, &$message, $params) {

Upvotes: 3

Shankar Ganesh Jayaraman
Shankar Ganesh Jayaraman

Reputation: 1491

Solution !

Try with below code in your "registration_form" module. This code will send mail often on page refresh since mail trigger is done in hook_init. Use where ever you want.



    /**
     * Implements hook_init();
     */
    function registration_form_init() {
    $first_name = 'Shankar';
    $params = array(
                    'subject' => 'Signup Invitation',
                    'body' => '<p>Dear ' . $first_name . ',</p>
                               <p>Congratulations! Your account <b> ' . $first_name . ' </b>has been successfully created with SOME portal.</p>
                                                <p>Thanks,SOME portal POSTMASTER</p>',
                    'first_name' => $first_name
                );
                drupal_mail('registration_form', 'invitation', $email, language_default(), $params);    
    }
    /**
     * Implements hook_mail();
     */
    function registration_form_mail($key, &$message, $params) {
        switch ($key) {
            case 'invitation':      
                $message['subject'] = $params['subject'];
                $message['body'][] = $params['body'];
                break;
        }
    }

Upvotes: 1

VeeraPrasad
VeeraPrasad

Reputation: 47

Try using the following snippet.

// Use these two lines when you want to send a mail.
global $user;
drupal_mail('test', 'test_mail', 'your_mail_id', user_preferred_language($user), $params, $from_mail_id, TRUE);

/**
 * Implements hook_mail().
 */
function test_mail($key, &$message, $params) {
  switch ($key) {
    case 'test_mail':
      $params['subject'] = t('Subject is here');
      $params['message'] = 'message is here';
      $message['subject'] = $params['subject'];
      $message['body'][] = $params['message'];
      break;
  }
}

Note: 'test' is module name.

Upvotes: 1

Related Questions