2

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

4

3 回答 3

3

在你的模块中你不应该使用 hook_mail 它应该是<MODULE_NAME>_hook().

改变

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

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

于 2013-08-19T13:47:12.690 回答
1

解决方案 !

尝试在“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;
        }
    }

于 2013-08-27T06:54:48.627 回答
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' 是模块名称。

于 2013-08-21T09:51:25.543 回答