开发者

problem with php mail() - from name doesn't appear correctly

hellHi Folks,

I have a contact form on my webpage, and it workd fine so far.

Only problem is, that in my mailprogram, the name in the from field doesn't show correctly, although the sourcecode of the email seems correct:

From: Metaldemos <hello@metaldemos.com>
Reply-To: Metaldemos <hello@metaldemos.com>

Anyway, in the mailprogram, the name is 'hello'.

In php I use this headers:

$headers="Mime-Version: 1.0\nContent-Type: text/plain; charset=UTF-8\nContent-Transfer-Encoding: quoted-printable\nFrom: Metaldemos <hello@metaldemos.com>\nReply-To: Metaldemos <hello@metaldemos.com>\nReturn-Path: Metaldemos <hello@metaldemos.com>\n";

and the code for sending the mail:

mail($email, $subject, $mailbody, $headers,"-t -i -f Metaldemos <hello@metaldemos.c开发者_如何学运维om>");

Any idea on how I can fix this?

Greetz & thanks

Maenny


The above answer is correct. You need the \r\n at at the end of the "From" and "Reply-To" lines. AS WELL as at the end of ALL the other header lines.

According to the SMTP RFC (section "2.3.8. Lines")

Lines consist of zero or more data characters terminated by the sequence ASCII character "CR" (hex value 0D) followed immediately by ASCII character "LF" (hex value 0A). This termination sequence is denoted as in this document. Conforming implementations MUST NOT recognize or generate any other character or character sequence as a line terminator. Limits MAY be imposed on line lengths by servers (see Section 4).

In addition, the appearance of "bare" "CR" or "LF" characters in text (i.e., either without the other) has a long history of causing problems in mail implementations and applications that use the mail system as a tool. SMTP client implementations MUST NOT transmit these characters except when they are intended as line terminators and then MUST, as indicated above, transmit them only as a sequence.

So your header line of:

$headers="Mime-Version: 1.0\nContent-Type: text/plain; charset=UTF-8\nContent-Transfer-Encoding: quoted-printable\nFrom: Metaldemos <hello@metaldemos.com>\nReply-To: Metaldemos <hello@metaldemos.com>\nReturn-Path: Metaldemos <hello@metaldemos.com>\n";

is invalid, HTTP or SMTP headers MUST always end with \r\n not just a \n or \r

The correct line would be

$headers="Mime-Version: 1.0\r\n";
$headers.="Content-Type: text/plain; charset=UTF-8\n";
$headers.="Content-Transfer-Encoding: quoted-printable\n";
$headers.="From: Metaldemos <hello@metaldemos.com>\n";
$headers.="Reply-To: Metaldemos <hello@metaldemos.com>\n";
$headers.="Return-Path: Metaldemos <hello@metaldemos.com>\n";

You CAN put it all in one long line that's fine, I just split it up to make it clearer.

The reason it didn't work before is because you only changed FROM and REPLY-TO you have to change all of them.


Try adding both a carriage return and new line character. I know when I'm writing PHP scripts to send mail, I do something similar to the following:

...
$headers.= "From: John Doe <john.doe@example.com>\r\n";
$headers.= "Reply-To: Jane Doe <jane.doe@example.com>\r\n";
...
if (mail($to, $subject, $message, $headers)) {
    // email sent
}
else {
    // email failed
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜