PHP mail function prepends a spurious character in Windows - php

I am attempting to use the built-in "mail" function in PHP, which has worked fine for my needs in a hosted LAMP environment with SiteGround. In this case I am running my own server on Windows, and my messages arrive with a spurious character (a capital 'B') prepended to the body text. I have tried replacing $mailBody in the call with a simple 'abc' string, and the spurious capital 'B' is still prepended. I am guessing that the problem is caused by a character-encoding mismatch. I have tried altering the "Content-type" in the header from text/plain to text/html, and the charset from windows-1251 to utf-8, neither of which make any difference.
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=windows-1251\r\n";
$headers .= "From: $confirmation_sender\r\n";
$bMailSuccess = mail ($strEmail, $business_name . ' Booking Confirmation', $mailBody, $headers);
php.ini
SMTP = internalmail
As you can see, mail is directed to a server called 'internalmail', and the logs on this server suggest that the spurious character is already part of the body text that it receives. Can anyone suggest a fix (preferably other than "install some other mail client")?

Related

PHP error_log Msg Type 1, Multiple or malformed newlines found in additional_header

An error with the php error_log function when selecting message_type 1 and sending an email. Any value I place in the extra_headers parameter, stops any email being received and the error created.
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= 'From: error-404#'.substr($_SERVER['SERVER_NAME'],4)."\r\n";
$log = var_export(debug_backtrace(), true);
error_log("<HTML><body><h1>404 ERROR: $today</h1><br /><p>$log</p></body></HTML>", 1, "webmaster#domain.com", $headers);
mod_fcgid: stderr: PHP Warning: error_log(): Multiple or malformed newlines found in additional_header in .....
Current PHP version: 5.6.24
Thanks.
The documentation of function error_log() for argument $extra_headers says:
This message type uses the same internal function as mail() does.
The documentation of function mail() says for $additional_headers:
Multiple extra headers should be separated with a CRLF (\r\n)
This is (I hope) the reason you use CRLF (\r\n) to separate the lines in the mail header.
However, the same documentation page also says in a note, several paragraphs below:
If messages are not received, try using a LF (\n) only. Some Unix mail transfer agents (most notably » qmail) replace LF by CRLF automatically (which leads to doubling CR if CRLF is used). This should be a last resort, as it does not comply with » RFC 2822.
I cannot tell about qmail but I encountered the same behaviour when the email server was sendmail. The problem vanished when I used LF (\n) as the end-of-line marker in the headers.
This has worked for me. Not too sure how correct this solution is.
//create a boundary string. It must be unique so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: $from_name <$from_mail> \r\nReply-To: $from_mail";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: text/html; charset=\"iso-8859-1\"; boundary=\"PHP-alt-".$random_hash."\"";
error_log($sMsg, 1, $emailto, $headers);
I hope it helps somebody else.
Thank you for all your replies.
Do not end extra_header with newline.
Below code is OK:
error_log("sometext", 1, "ohmyson#gmail.com",
"Subject: Foo\nFrom: Rizzlas#my.domain");

Sending email through PHP, pound symbol

I need to send emails using PHP's mail() function. The code I am using is this:
$email_message = chunk_split(base64_encode($email_message));
$headers = "Content-Transfer-Encoding: base64\r\n\r\n";
mail($to, $subject, $email_message, $headers);
There is a pound sterling symbol in the email which is not handled properly, i.e. recipient receives incorrect symbol. As its to do with character encoding and I am not sure how to set it to tell the email client how the characters are being encoded and how to deal with the pound symbol correctly. Can this information be put in the headers?
Are you correctly setting the encoding on the email? This is done by setting
'Content-type: text/html; charset=utf-8'
in the message's headers.
Plenty of documentation here if you scroll down: http://php.net/manual/en/function.mail.php
If you have pound signs coming out as  then look below
$costsum = "£".$costsum; (Does not work)
$costsum = "£".$costsum; (Does not work)
$costsum = "#163;".$costsum; (Does not work)
One answer I found was this!
$costsum = "\243".$costsum;
The \243 is a pound sign in whatever encoding that is.
I tried all the pages with UTF-8 and that didn't work either.
It was used to email a spreadsheet.

Which line break in php mail header, \r\n or \n?

I've seen a lot of examples using the php mail function. Some of them use \r\n as line break for the header, some use \n.
$headers = "From: Just Me\n";
$headers .= "Reply-To: Just me <$email>\n";
vs
$headers = "From: Just Me\r\n";
$headers .= "Reply-To: Just me <$email>\r\n";
which one is correct?
Sometimes I've had cases where \r\n is used and part of the header is interpreted by some email clients as mail text (losing these header information) - is this because \r\n is wrong?
The CRLF \r\n, should be used according to the php documentation. Also, to conform to the RFC 2822 spec lines must be delimited by the carriage return character, CR \r immediately followed by the line feed, LF \n.
Since \r\n is native to Windows platforms and \n to Unix, you can use the PHP_EOL­Docs constant on Windows, which is the appropriate new line character for the platform the script is currently running on.
Just in case a search engine picks this up and saves someone else the frustration I went through: here's an additional curiousity.
On php 5.2x on Linux, I had \r\n on my email headers in php mail(), after an upgrade to php 5.3.3, the formatting and sending mysteriously failed. Removing the \r fixed the script (after examining many many other possibilities).
As stated above, \r\n is what you should use according to the RFC, but this breaks your headers on several mail systems (f.i. Outlook 2003). Even though \n is not the 'proper' line break to use, in my experience it works correctly on all mail systems I've encountered so far. Because of this, I always use just \n.
The RFC formally mandates CRLF (\r\n) but using Unix breaks (\n) for headers will save you a lot of hassle. Some mail servers, such as qmail, will reject your message if it uses \r\n.
Source: experience, confirmed by this note: http://www.php.net/function.mail#40204
My experience:
HTML emails were working in web clients, but breaking in MS based desktop clients (entourage, outlook). Was using \r\n. Removed the \r on the MIME-Version only and now works across the board.
I've had the problem of gmail misunderstanding \r\n headers, but simply leaving the header line breaks at \n was not enough in my case, because in that case some versions of Outlook showed emails as empty.
The solution in https://stackoverflow.com/a/7960957 (I chose to install postfix 2.9 on lucid from a ppa) coupled with using \n seems to work everywhere now.
I changed my script to use PHP_EOL instead which seems to work -- like this:
//Set Content-type header
$headers = "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/html; charset=iso-8859-1" . PHP_EOL;
//Additional headers
$headers .= "From: $from" . PHP_EOL;
$headers .= "Cc: $cc" . PHP_EOL;
$headers .= "Content-type: text/html" . PHP_EOL;
$headers .= "Bcc: $bcc" . PHP_EOL;
NB. Be sure to us " instead of ' as the latter doesn't seem to work!
> $mail = new PHPMailer;
$mail->isSMTP();
**$mail->isHTML(true);**
Insert this code after working
> all html tag <br> <p> in $mail->Body='Hello<br> how are you ?<b>';

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.com>");
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
}

Postfix screwing up email headers!

I've set up a local dev environment on snow leopard, and have set postfix up to send email via my isp mail server.
I eventually got postfix to work after much frustration, but now when my emails send the header information is bunged up!
I'm using the following php code:
$email = "me#mydomain";
$subject = "Email tester";
$body = "Simple test";
$header = "From: me#mydomain \r\n";
$header .= "MIME-VERSION: 1.0\r\n";
$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$phpversion = phpversion();
$header .= "X-Mailer: PHP v$phpversion\r\n";
mail($email,$subject,$body,$header);
echo "message sent!";
The To: and Subject: headers display as they should, in the header!, but the rest display in the email body. This makes the email look like the from field in email client is empty.
I've tried a variety of php scripts, some very simple, but its the same thing, headers always displaying in the email body.
I'm thinking it could be a postfix problem, but not sure, anyone encountered this type of problem before?
Use PHP_EOL instead of \r\n in *additional_headers*, i.e. $header in your example. PHP_EOL will substitute the newline correspondingly to the OS you are running on.
Also, message should contain LN only i.e. \n. This is accordingly to PHP documentation.
Each line should be separated with a LF (\n). Lines should not be larger than 70 characters.
Make sure you meet both of criterias in your script - I've tried to achieve it and finally got it working with the default configuration of Postfix.
This is almost 100% not a Postfix problem, but something caused by your code. The body starts once a blank CRLF is seen after the headers.
You should dump out your email body text and see if you're not accidentally introducing an extra CRLF.
Investigating this problem further (basically because I didn't want to improve lots of scripts just because of that), I've come to the point that there is a strong conflict between PHP and Postfix developers, which is not fixed until now. You can read wide info here:
http://www.mail-archive.com/postfix-users#postfix.org/msg03226.html
Postfix expects EOL to be LF when picking up mail from sendmail on unix and
replaces that with CRLF when sending. When it gets CRLF it still replaces the
LF and we get CRCRLF.
It explains broken headers. To solve this, you must exactly know how your Postfix and PHP/mail system works. If you experience problems like described above, just use "\n" instead of "\r\n". If you program a complicated system which could run on both Windows/Unix, just introduce an extra param like $eeol="\r\n"; which will be put instead of direct "\r\n" tag and that way could be easily configured for any system.
I suppose that's the approach Postfix author recommends:
It would be really good if PHP application programmers formatted email messages in a consistent manner. For example, they could use a variable that contains the END-OF-LINE terminator, instead of hard-coding LF or CRLF line terminators all over the place.

Categories