Reputation: 103
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();
}
Upvotes: 2
Views: 8809
Reputation: 1846
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();
}
Upvotes: 1
Reputation: 54796
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.
Upvotes: 0