Im trying to get an email to send from my php page to an email address that is set.
The email is being sent and received at the expected email address. But the contents within the email are not as expected. I got the code from watching a tutorial on youtube.
However when looking at the email is displays exactly as the code below, doesn't give me the values behind the variables e.g $name shows up in the email as $name
Any ideas?
The code im using is;
<?php
require_once 'header.php';
$to = 'emailaddress ';
$subject = 'CSG';
$name = $_POST['name'];
$email = $_POST['emailaddress'];
$message = $_POST['message'];
$body = <<<EMAIL
Hi $name, You have recently requested a notification of your password on the Coleg Sir Gar Loan Syste site.
From $name
EMAIL;
$header = '$email';
if ($_POST){
mail($to, $subject, $body, $header);
$feedback = 'Email Sent';
}
?>
Firstly, variables do not get parsed inside single quotes, that's why you're seeing $email
rather than the email itself inside the email body.
So change $header = '$email';
to either $header = "$email";
or remove the quotes entirely.
I.e.: $header = $email;
Then the header is failing you. It expects to be a From:
(email address) - The "From" in your mail will come back as your server's name rather than the (intended) email address from the person sending it.
Consult the manual:
Sidenote about your heredoc.
Even though mail is going out, you still have a trailing space in your opening identifier:
$body = <<<EMAIL
and that may throw a Parse error: syntax error, unexpected '<<' (T_SL)
error.
Testing this came back as:
email@example.com Hi Fred, You have recently requested a notification of your password on the Coleg Sir Gar Loan Syste site. From Fred
and the "From:" as default@cpanel.example.com
rather than whoever@example_mailer.xxx
.
As noted in comments by dieend, you can try bracing the variables {$var}
, however with or without them, produced the same results for me.
If that still doesn't work, then it may be caused by the trailing space in your opening identifier; you need to remove it.
Copy/paste exactly as shown:
$body = <<< EMAIL
Hi {$name}, You have recently requested a notification of your password on the Coleg Sir Gar Loan Syste site.
From {$name} - {$email}
EMAIL;
$header = "From:" . $email; // Now you have a valid From
if ($_POST){
mail($to, $subject, $body, $header);
echo $feedback = 'Email Sent';
}
Final notes: