我正在编辑一个表单,我希望在用户填写和发送表单时作为邮件接收。我以前在邮件中收到过变量,比如:Name:$Name。现在contact.php返回一个空白页,我无法以邮件形式接收表单。
我之前试过,
$body_message = 'Name: ' . $name . "'r'n";
$body_message .= 'E-mail: ' . $mail_from . "'r'n";
$body_message .= 'Message: ' . $message;
但它不起作用。此外,
$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个错误。
1.)您从表单中为$mail分配电子邮件地址。然后访问$mail实例,该实例应被误认为PHPMailer实例。
$mail = $_POST['mail'];
....
....
$mail->Subject = 'Reservation';
$mail->Body = '<b>You have a reservation!</b></br>'
....
....
2.)您正在退出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();
}
此行:
$mail = $_POST['mail'];
意味着$mail
是一个包含一些电子邮件的字符串。
这个:
$mail->Subject
意味着CCD_ 2是某个类的实例。
它是字符串还是某个类的实例?你知道吗?
如前所述,在foreach
中使用return
会导致脚本或函数(无论您有什么)停止脚本执行或结束函数执行