How to use variables in phpmailer message body

半腔热情 提交于 2019-12-06 10:02:00

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

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.

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