Which Line Break in PHP Mail Header, \R\N or \N

sending mail inserts a random space at the 995th character in the to: email list

Finally I was able to solve this problem like this:
I have the to list in a $strTo variable which contains the emails like this "email1, email2, email3, email4, "... with each email separated by ", " .

Before doing the mail() function I did this:

if($strTo != ""){
$strTo = wordwrap($strTo,900,"\r\n ");
$strTo .= "\r\n";
}

Which means I put carriage return, new line and a white space at every 900 characters(without breaking any words) and finally, at the end of the string I inserta carriage return and line feed.

Required Mail Headers

The headers need a white space at the bottom to separate the header from main body.
Tools like Spam Assassin will give you a big mark down for that.

Also you should use \r\n as a line terminator instead of just \n

From PHP.net

Multiple extra headers should be separated with a CRLF (\r\n).

Email using php will not send when $headers added

Try adding From: $from\r\n to your headers before the Content-Type in the quotes and getting rid of the from argument when you call the mail function. Unless you set a default from in php.ini, or use the additional_parameters argument (the fifth one), if you want to set the 'from' value, the function has four mandatory arguments, the last of which is headers. It seems that your from is meeting the requirements of the headers argument, but once you add the fifth argument you are incorrectly using the function.

how to format message body of email?

You want a new line (\n) not an HTML line break (<br>) since your email isn't marked as having an HTML body (and emails that have HTML bodies should really have multipart MIME bodies with both plain text and HTML versions since "HTML only" is a nice flag for spam detectors).

Why Use /r/n When Developing a PHP Script?

Taken from here

\r\n are end of line characters for Windows systems.

\n is the end of line character for UNIX systems.

It is to ensure cross-OS compatibility.

How do I change the from email address associated with PHP contact form

You are overwriting the $headers variable on each line, you instead want to append to it. Replace:

$headers = 'MIME-Version: 1.0' . "\r\n";
$headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers = "From: $from";
$headers = "$name \n $phone \n $email";

With:

$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: $from";
$headers .= "$name \n $phone \n $email";

I'm not too sure what that last line is accomplishing, should it be in the $message instead?

PHP mail() attachment problems

$to = "myemail@mydomain.com";
$from = "Website <website@mydomain.com>";
$subject = "Test Attachment Email";

$separator = md5(time());

// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;

// attachment name
$filename = "document.pdf";

//$pdfdoc is PDF generated by FPDF
$attachment = chunk_split(base64_encode($pdfdoc));

// main header
$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";

// no more headers after this, we start the body! //

$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$body .= "This is a MIME encoded message.".$eol;

// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;

// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";

// send message
if (mail($to, $subject, $body, $headers)) {
echo "mail send ... OK";
} else {
echo "mail send ... ERROR";
}

What I've done:

  • Separated the body from the headers
  • removed extra EOLs put before separators


Related Topics



Leave a reply



Submit