php mail() problem when sending html mail

Hello there!

I have a simple emailing function which sends form results as an e-mail. I’m using it as a contact us and I’m trying to make send e-card functionality. Everything works fine (I load picture, text, addresses etc, put it into simple html mail and send). The problem is that I receive the mail from my server email address, not from the one specified in headers.

[php]
$email_from = "[email protected]";
$email_subject = “User $name just sent you this ecard”;
$ecard = "

New ecard from user $name



$message\n
Some footer text...
";

$headers = ‘From: $email_from . \n’;
$headers .= ‘Reply-To: $visitor_email . \n’;
$headers = ‘MIME-Version: 1.0’ . “\n”;
$headers .= ‘Content-type: text/html; charset=iso-8859-1’ . “\n”;
$to = “$rec_email”;
$email_body = “$ecard”;

mail($to, $email_subject, $email_body, $headers);
[/php]

First two lines of headers simply don’t work and are overwritten. When I remove the latter two headers, everything works fine (from and reply-to) but the e-mail is displayed as a plain text. I need to send html email but I need to include from and reply-to as well. Any ideas?

I’m new to php, so excuse my ignorance in not getting to right solution myself. Any ideas will be strongly appreciated.

OK, I tried few more tricks and in the end, I managed to get the right result. Although I don’t understand why…

The solution that worked for me was not to predefine headers but to define them inside the mail() function. So the code which worked for me was:

[php]mail($to, $email_subject, $email_body,
“From: $email_from\n” .
“Reply-To: $visitor_email\n” .
“MIME-Version: 1.0\n” .
“Content-type: text/html; charset=iso-8859-1”);[/php]

Although the problem seems to be solved now, I’m still interested why the predeclared headers did not work for me. Anyone has an idea?

You were leaving out the period on one of the headers. Instead of concatenating the variable, you were overwriting.

[php]
$headers = “From: $email_from \n”;
$headers .= “Reply-To: $visitor_email \n”;
$headers = “MIME-Version: 1.0 \n”;
$headers .= “Content-type: text/html; charset=iso-8859-1 \n”;
[/php]

Which means that you were actually only saving the MIME-Version and Content-Type headers - therefore, the server was subbing in defaults for you.

Should be:

[php]
$headers = “From: $email_from \n”;
$headers .= “Reply-To: $visitor_email \n”;
$headers .= “MIME-Version: 1.0 \n”;
$headers .= “Content-type: text/html; charset=iso-8859-1 \n”;
[/php]

Thanks dude,

I actually figured out in the morning when I reviewed once again with fresh mind :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service