How to use variables in phpmailer message body

雨燕双飞 提交于 2019-12-08 03:06:32

问题


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.

I tried before that,

$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
';

Now my code:

$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();
}

回答1:


Your code has 2 errors.

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.) You are exiting from execution inside foreach

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

Fix

$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();
}



回答2:


This line:

$mail = $_POST['mail'];

means that $mail is a string with some email.

And this:

$mail->Subject

means that $mail is instance of some class.

Is it string or instance of some class? Can you tell?

And as already noticed - using return in your foreach causes your script or your function (whatever you have there) to either stop script execution or end function execution.



来源:https://stackoverflow.com/questions/34590065/how-to-use-variables-in-phpmailer-message-body

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!