编程语言
首页 > 编程语言> > php – drupal 7:邮件已发送,但正文和主题都是空的

php – drupal 7:邮件已发送,但正文和主题都是空的

作者:互联网

我正在尝试使用Drupal 7发送电子邮件.电子邮件将发送到我的电子邮件帐户,但不知何故,正文和主题是空的.请帮我解释原因.非常感谢你.

以下是我的代码:

这是表格

function get_friendform($form,&$form_submit){
    $form['fullname'] = array(
        '#title' => t('Your Full Name: '),
        '#type' => 'textfield',
        '#required' => TRUE,
        '#size' => 30,
    );

    $form['email'] = array(
        '#title' => t('Your Full Email: '),
        '#type' => 'textfield',
        '#required' => TRUE,
        '#size' => 30,
    );

    $form['friend_email'] = array(
        '#title' => t('Your Friend Email: '),
        '#type' => 'textfield',
        '#required' => TRUE,
        '#size' => 30,
    );


    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => 'submit',
        '#submit' => array('sendform_form_submit')

    );

    return $form;
}



function sendform_form_submit($form,&$form_submit){
    $fullname = $form_submit['values']['fullname'];
    $email = $form_submit['values']['email'];
    $friend_email = $form_submit['values']['friend_email'];
    $current_page = $GLOBALS['base_url'] .'/'.current_path();

    $mailto = 'thelinhuk@gmail.com';
    $mailfrom = 'thelinhuk@yahoo.com';
    $subject = "Links to event";
    $body = $current_page;
    $params = array(
        'body' => $body,
        'subject' => $subject,
    );

    if (drupal_mail('get_friendform', 'send_link', $mailto, language_default(),$params,$mailfrom,TRUE)) {
        drupal_set_message(t('Your message was sent successfully!!!'));
    }

}



function sendform_mail($key,&$message,$params) {
    $language = $message['language'];
    switch ($key) {
        case 'send_link':
            $message['subject']=t($params['subject'], $var, $language->language);
            $message['body'][]=$params['body'];
            $message['headers']['Content-Type'] = 'text/html; charset=UTF-8; format=flowed';
        break;
    }

}

非常感谢你

新代码

function sendform_form_submit($form,&$form_submit){
    $fullname = $form_submit['values']['fullname'];
    $email = $form_submit['values']['email'];
    $friend_email = $form_submit['values']['friend_email'];
    $current_page = $GLOBALS['base_url'] .'/'.current_path();

    $mailto = 'thelinhuk@gmail.com';
    $mailfrom = 'thelinhuk@yahoo.com';
    $subject = "Links to event";
    $params ='';
    $body[] = $current_page;
    $mail_message = drupal_mail('send_form', 'some_key', $mailto, language_default(), $params, $mailfrom, TRUE);
    $mail_message['subject'] = $subject;
    $mail_message['body'] = $body;

    $mail_system = drupal_mail_system($module, $key);
    $mail_message = $mail_system->format($mail_message);

    $mail_message['result'] = $mail_system->mail($mail_message);

}

解决方法:

使用以下代码示例使用drupal发送电子邮件:

$body = array();
$body[] = "Mail body";
$mail_message = drupal_mail($module, $key, $to, $lang, $params, $from, $send);
$mail_message['subject'] = $subject;
$mail_message['body'] = $body;

$mail_system = drupal_mail_system($module, $key);
$mail_message = $mail_system->format($mail_message);

$mail_message['result'] = $mail_system->mail($mail_message);

更新1:

您的代码中存在问题.您已经创建了一个函数sendform_mail(),但您从未调用它.

您可以在表单提交回调sendform_form_submit()中使用上面的代码示例.这更简单

更新2:

$mail_message = drupal_mail('send_form', 'some_key', $mailto, language_default(), $params, $mailfrom, TRUE);

在您的代码中,将最后一个值更改为false,因为您需要将发送的邮件停止到最后一行$mail_message [‘result’].

希望这有效……穆罕默德.

标签:php,drupal,drupal-7,drupal-modules
来源: https://codeday.me/bug/20190529/1180909.html