Im having problems with line breaks when sending mail with php. The main issue here is, that the line breaks doesnt work in mails with attachment (multipart/mixed) but in plain text they do.
<?php
function sendMail($email, $name, $pdfpath) {
// $file should include path and filename
$filename = basename($pdfpath);
$file_size = filesize($pdfpath);
$content = chunk_split(base64_encode(file_get_contents($pdfpath)));
$uid = md5(uniqid(time()));
$subject = "New attachment mail: ".$name;
$subject = "=?UTF-8?B?".base64_encode($subject)."?=";
$body = "Hello Dude!\n\n"
."Testing\n\n"
."Testing\n\n\n\n"
."Testing\r\n"
."Testing\n";
//combine two headers (attachment mails needs to have specific headers)
$header .= $this->attachment_headers;
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/plain; charset=UTF-8\r\n";
$header .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
$header .= $body."\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
//( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
return mail($email, $subject, "", $header);
}
?>
So, the output with this in my email is following:
Hello Dude!
Testing
Testing
Testing
Testing
So, it does add a new line, but only one, I need to have more. How can I do that?
Ok, I can’t find any help from web so I came up with a bubblegum solution. Seems like that for some reason multipart/mixed message strips/removes all additional lines.
See the space between the line break, and the outcome is as it’s supposed to.