Simple php mail header with variable - php

I cannot get this header to show the variable needed for some strange reason. I know its something very slight. I know that this works:
$headers .= 'Bcc: example#aol.com' . "\r\n";
Can someone please help me with this. FULL CODE:
<?php
if ($_POST){
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$message .= "<hr />Sent from your Website at example.com.";
$to = 'person#aol.com';
$from = $email;
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// THE EMAIL IS NOT SENDING THE SENDER OF THE EMAIL
// the from and bcc do not work as needed!
// More headers, put on double quotes
$headers .= 'From:'.$from."\r\n";
$headers .= 'Bcc: '.$from."\r\n";
// send
if (mail($to,$subject,$message,$headers))
{
echo "Message was successfully sent! $from";
} else {
echo "Something went wrong..";
}
} else {
}
?>

You are missing a dot (period) after your first 'Bcc: '.
it should be
$headers .= 'Bcc: ' . $from . "\r\n";
UPDATE:
You need to use double quotes when using special characters like \r and \n. In your original example code you used double quotes, but I see now with your full code that you're not actually using them, you're using single quotes.
Single quoted strings in PHP show special characters as literals.
So your lines should be
$headers .= 'From: ' . $from . "\r\n";
$headers .= 'Bcc: ' . $from . "\r\n";

Use this will work perfectly:
$headers .= 'From: "'.$from.'"' . "\r\n";

easiest and the best way
"From: ". $email. "\r\n";

Related

how to change the from attribute to my email in mail() function

I am trying to send an email to my customers when they place an order on my website. But the from appears like this:
From : n9da2313
I need to exchange it with info#awraq.me
I tried this but didn't work
`
$to = "founder#awraq.me";
$subject = "Your order";
$message = "Your order was placed successfuly";
$headers = "From: info#awraq.me";
$headers .= "\r\nReply-To: info#awraq.me";
$headers .= "\r\nX-Mailer: PHP/".phpversion();
mail($to,$subject,$message,$headers,"-f info#awraq.me");
`
try this-
$to = "somebody#example.com, somebodyelse#example.com";
$subject = "HTML email";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: <webmaster#example.com>' . "\r\n";
$headers .= 'Cc: myboss#example.com' . "\r\n";
mail($to,$subject,$message,$headers);
If its not working then check if you have done some configuration in php.ini file.

PHP processing form : unknown sender

I created a form with a processing PHP file. Everything works fine. I receive the mail but it's from "Unknow sender" in Gmail. Why please ?
I would like to see in my email box the name and the firstname of the person who fills the form. What's wrong in my code ?
<?php
if(isset($_POST) && isset($_POST['form3_firstname']) && isset($_POST['form3_name']) && isset($_POST['form3_email']) && isset($_POST['form3_telephone']) && isset($_POST['form3_message'])) {
extract($_POST);
if(!empty($form3_firstname) && !empty($form3_name) && !empty($form3_email) && !empty($form3_telephone) && !empty($form3_message)) {
$to = 'XXXXXX#gmail.com'; // My real email
$subject = 'Contact from the site';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-Type: text/html; charset=UTF-8' . "\r\n";
$headers .= 'From:' .$form3_firstname. " " .$form3_name. "\r\n";
$headers .= 'Reply-To:'.$form3_email. "\r\n";
$message = '<html><body>';
$message .= '<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>';
$message .= '<table>';
$message .= '<tr><td colspan="2"><p>MESSAGE</p></td></tr>';
$message .= '<tr><td>Firstname :</td><td>'.$form3_firstname.'</td></tr>';
$message .= '<tr><td>Name :</td><td>'.$form3_name.'</td></tr>';
$message .= '<tr><td>Email :</td><td>'.$form3_email.'</td></tr>';
$message .= '<tr><td>Telephone :</td><td>'.$form3_telephone.'</td></tr>';
$message .= '<tr><td>Message :</td<td>'.stripslashes($form3_message).'</td></tr>';
$message .= '</table>';
$message .= '</body></html>';
if(mail($to, $subject, $message, $headers)){
echo "Form sent";
} else {
echo "Form not sent";
}
} else {
echo "You have not filled in the field";
}
}
?>
Replace the $form3_name with $form3_email
$headers .= 'From:' .$form3_firstname. " " .$form3_name. "\r\n";
^^^^^^^^^^^^ //<----- Here
That's a name , not an email address , and that's the reason you get that error.
Also, you need to wrap them in tags <>
The right way..
$headers .= 'From:' .$form3_firstname. " ".'<'.$form3_email.'>'."\r\n";
replace "\r\n" with "\n" and your problem will be solved... and also put return-path in your headers...
$headers = 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-Type: text/html; charset=UTF-8' . "\n";
$headers .= 'From: \''.$form3_firstname.'\' <'.$form3_firstname.'>\r\nReturn-Path: <'.$form3_firstname.'>
$headers .= 'From:' .$form3_firstname. " " .$form3_name. "\n";
$headers .= 'Reply-To:'.$form3_email. "\n";
please let me know if you want furtther guidance...
Because you do not supply an emailaddress in your "From:" header. There always needs to be an emailaddress.
Try something like:
$headers .= "From: $form3_firstname $form3_name <$form_email>\r\n";
Mind you, you may have to test and/or escape your form variables; for instance, check that there is no newline in there, otherwise your form might be abused for spamming.

Using variables PHP

So, this is part of the code in my sendemail.php and it works perfectly:
$email = $_POST["email"];
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: <webmaster#example.com'>' . "\r\n";
When trying to edit the From: to the email received from my form the script suddently it doesn't work. Could you help me?
I tried this:
$email = $_POST["email"];
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: <"$email">' . "\r\n";
The problem is this line:
$headers .= 'From: <"$email">' . "\r\n";
It should be:
$headers .= 'From: <"' . $email . '">' . "\r\n";
Because the contents of the variable are in single quotes, $email becomes a literal "$email" which is at least causing problem with receiving the email as spam filters will usually raise a red flag when it sees this.
Another way to deal with variables in highly formatted content is to use sprintf:
So the example that John Conde gives looks like this:
$headers .= 'From: <"' . $email . '">' . "\r\n";
But using sprintf it would look like this:
$headers .= sprintf('From: <"%s">' . "\r\n", $email);
The benefit is you do not have to worry about balancing quotes. And since the variables are always at the end of the line, it makes it a tad easier to review for debugging in my opinion.
EDIT: Also, since this formats it better but is still not sending (according to the original poster’s comments) try the following variations instead. No quotes:
$headers .= sprintf('From: <%s>' . "\r\n", $email);
No < and >:
$headers .= sprintf('From: %s' . "\r\n", $email);

Multiple recipients with PHP script?

So the issue is I want multiple recipients for my PHP form.
What happens is the site user enters there email and then another email for another peroson (for example there doctor).
So what I need is the the doctor to be emailed to.
This is what I am using to know success
$mail_to = $field_emaildoc .$field_email;
This doesent seem to work?
Any ideas would be great :)
Thanks
one option is to add a "Cc" to your header:
$sender_email = 'email#domain.com';
$sender_name = 'YOUR NAME';
$send_to = 'email#domain.com';
$send_to_copy = 'anotheremail#domain.com';
$message = 'THIS IS YOUR MESSAGE';
$subject = 'THIS IS YOUR SUBJECT';
$headers = 'Content-type: text/html; charset=iso-8859-1' . "\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= "From: $sender_name<" . $sender_email . ">" . "\r\n";
$headers .= "X-Sender: $sender_name<" . $sender_email . ">" . "\n";
$headers .= "X-Mailer: PHP " . phpversion() . "\n";
$headers .= "X-Priority: 3" . "\n";
$headers .= "X-Sender-IP: " . $_SERVER['REMOTE_ADDR'] . "\n";
$headers .= "Return-Path: $sender_name<" . $sender_email . ">" . "\r\n";
$headers .= "Reply-To: $sender_name<" . $sender_email . ">" . "\r\n";
$headers .= "Cc: $send_to_copy" . "\r\n";
mail($send_to,$subject,$message,$headers);
The problem with this, is that the person receiving the email can see who was copied in. An alternative would be to use: "Bcc" instead of "Cc" or just use the mail() function twice and remove the "Cc" or "Bcc":
mail($send_to1,$subject,$message,$headers);
mail($send_to2,$subject,$message,$headers);
You should put comma between mail addresses . Look explanation of to parameter, here : http://php.net/manual/en/function.mail.php
You'll need a comma:
$mail_to = $field_emaildoc . ',' . $field_email;
Use the default mail function from PHP.
The recipients are devided by a Comma.
When you want CC and BCC you can set the header. Example from PHP.net:
$header .= 'To: Simone <simone#example.com>, Andreas <andreas#example.com>' . "\r\n";
$header .= 'From: Geburtstags-Erinnerungen <geburtstag#example.com>' . "\r\n";
$header .= 'Cc: geburtstagsarchiv#example.com' . "\r\n";
$header .= 'Bcc: geburtstagscheck#example.com' . "\r\n";
mail($empfaenger, $betreff, $nachricht, $header);

What is the correct format for this email header?

How can I send the $email correctly?
$headers .= 'From: SUB: .$email.' . "\r\n";
Either use double quotes (don't recommend it):
$headers .= "From: SUB: {$email}\r\n";
Or do it properly and get the variable outside the quotes:
$headers .= 'From: SUB: '.$email."\r\n";
As you can see, you were very close, but the ' should be placed before the first dot and not after the email variable :-)
$email should be in double quotes "From: Sub: $email\r\n"; or concat properly 'From: Sub: ' . $email . "\r\n"
$headers = 'From: Sub:'. $email . "\r\n";
You should make it quoted-printable:
$headers .= sprintf("From: \"SUB:\" <%s>\r\n", $email);
Will create a header-line like the following:
From: "SUB:" <mail#example.com>

Categories