0

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: <[email protected]'>' . "\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";

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

+ for speed, but you should add more detail.
Was in the process of doing it as you wrote this. :)
Hmm, the email hasn't reched it's destination, however it does when hardcoding an email adress insted of using $email.
That's explained in my answer so I am unsure what you're trying to say
Ohh, missed that one, but how do I make this without the single quotes then?
|
0

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

2 Comments

The email doesn't get sent, however it does when hardcoding an example email adress.
Still no success, this is weird.. This is the default W3Schools sample that works perfectly: $headers .= 'From: <[email protected]>' . "\r\n";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.