Drupal_mail()空身并没有主题

时间:2013-08-19 05:21:09

标签: drupal

我遇到drupal_mail()问题。我收到了电子邮件,但主题和正文都是空的。

Drupal版本7

下面的代码

$params = array(
    'subject' => t('Client Requests Quote'),
    'body' => t("Body of the email goes here"),
);
drupal_mail("samplemail", "samplemail_html_mail", "email@email.com", language_default(), $params, "email@email.com", TRUE);

我甚至尝试使用下面的钩子,我得到了相同的结果。

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()有效,但它以纯文本形式出现。

3 个答案:

答案 0 :(得分:3)

在你的模块中你不应该使用hook_mail,它应该是_hook()。

更改* function hook_mail($ key,&amp; $ message,$ params){* to function samplemail_mail($ key,&amp; $ message,$ params){

答案 1 :(得分:1)

尝试使用以下代码段。

// 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;
  }
}

注意:'test'是模块名称。

答案 2 :(得分:0)

解决方案!

尝试使用“registration_form”模块中的以下代码。此代码将在页面刷新时经常发送邮件,因为邮件触发器在hook_init中完成。随时随地使用。



    /**
     * 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;
        }
    }