Using variables PHP - 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);

Related

Something wrong with $headers

I created a feedback form and have something wrong with $headers, because it works perfect when I give fixed urls to $headers but when i give him variables it stops work. This is my code
<?php
$to = "mail#mail.com";
$name =$_POST['name'];
$email =$_POST['email'];
$subject =$_POST['subject'];
$messege =$_POST['messege'];
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers .= 'From: ' . $email . "\r\n";
$headers .= 'Bcc: ' . $email . "\r\n";
mail($to,$subject,$messege,$headers);
?>
Who can say why?
Share you HTML (form) codes with us to more guidance.
And you can check form method, according to your php codes, form method must be post. and you can echo $_POST['name'] (for example) that you know this filled or empty.

Simple php mail header with variable

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";

HTML Email occasionally sending source code

At my company, we have had several clients claim that their order confirmation emails are coming through just as the HTML source, not formatted at all. However the majority of our clients are receiving their emails through fine.
I think I may have found a link which explains why only some clients receive the unformatted email, however I'm not sure of the cause. It seems that users with a custom domain name for their email address are experiencing the problems and regular email domains such as #gmail.com or #icloud.com are working fine.
The problem is not related to the client used to view the email as I have been able to replicate the problem with my private domain name and the source code is displayed in all clients.
Any idea what would be the cause of this?
Here are my headers:
$headers = "From: ".$from."\r\n";
$headers .= "Reply-To: ".$from."\r\n";
$headers .= "Return-Path: ".$from."\r\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1";
Try following headers for HTML Email...
$from = $from . ' <' . $from . '>';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=utf-8" . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
$headers .= 'Reply-To: ' . $from . "\r\n";
$headers .= 'Return-Path: ' . $from . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion() . "\r\n";
$headers .= 'X-Originating-IP: ' . $_SERVER['SERVER_ADDR'] . "\r\n";

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