如何在phpmailer邮件正文中使用变量 [英] How to use variables in phpmailer message body

查看:248
本文介绍了如何在phpmailer邮件正文中使用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编辑一个表单,我希望在用户填写并发送时以邮件形式接收.我以前在邮件上收到过一些变量,例如:Name:$ name.现在contact.php返回一个空白页,我无法收到邮件形式的表格.

I'm editing a form and I want to receive as mail when users fill it and send it. I received variables on mail before, such like: Name: $name. Now contact.php returns a blank page and I can't receive the form as mail.

我之前尝试过,

$body_message = 'Name: ' . $name . "\r\n"; 
$body_message .= 'E-mail: ' . $mail_from . "\r\n"; 
$body_message .= 'Message: ' . $message;

但是它不起作用.而且,

But it doesn't work. And that also,

$mail->Body = '<b>You have a reservation!</b>
Name: $name
Surname: $surname
Phone: $phone
Mail: $mail
Address: $address
Services: $check
';

现在我的代码:

$name = $_POST['name'];
$surname = $_POST['surname'];
$mail = $_POST['mail'];
$phone = $_POST['phone'];
$address= $_POST['address'];

if(!empty($_POST['check_list'])) {
    foreach($_POST['check_list'] as $check) {           
        return $check;
    }
}

$mail->Subject = 'Reservation';
$mail->Body    = '<b>You have a reservation!</b></br>'
.'Name: '.$name.'</br>'
.'Surname: '.$surname.'</br>'
.'Mail: '.$mail.'</br>'
.'Phone: '.$phone.'</br>'
.'Address: '.$address.'</br>'
.'Services: '.$check;

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
    header('Location: index.php');
    exit();
}

推荐答案

您的代码有2个错误.

Your code has 2 errors.

1.)您从表单将电子邮件地址分配给$ mail.然后访问$ mail实例,该实例应该被误认为PHPMailer实例.

1.) You assign email address to $mail from form. Then access $mail instance which should be mistaken as PHPMailer instance.

$mail = $_POST['mail'];
....
....
$mail->Subject = 'Reservation';
$mail->Body    = '<b>You have a reservation!</b></br>'
....
....

2.)您将退出foreach内部的执行

2.) You are exiting from execution inside foreach

if(!empty($_POST['check_list'])) {
    foreach($_POST['check_list'] as $check) {           
        return $check;
    }
}

修复

$name    = $_POST['name'];
$surname = $_POST['surname'];
$email   = $_POST['mail'];
$phone   = $_POST['phone'];
$address = $_POST['address'];
$services= array();

if (!empty($_POST['check_list']) && is_array($_POST['check_list'])) {
    foreach ($_POST['check_list'] as $check) {           
        $services[] = $check;
    }
}

$mail = new PHPMailer;
//other PHPMailer config options
....
....
$mail->Subject = 'Reservation';
$mail->Body    = '<b>You have a reservation!</b></br>'
.'Name: '.$name.'</br>'
.'Surname: '.$surname.'</br>'
.'Mail: '.$email.'</br>'
.'Phone: '.$phone.'</br>'
.'Address: '.$address.'</br>'
.'Services: '. implode(', ', $services);

if (!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
    header('Location: index.php');
    exit();
}

这篇关于如何在phpmailer邮件正文中使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆