MIME information in the mail body with PHP

I am trying to use the PHP Mail function to send an email with an HTML body and an attached file. Without the attached file, I get my HTML email address without problems, but when I try to attach the file, I have all the MIME information included in the body - also the attachment encoded.

Here's the code for the email function without attachments - this works fine:

$this->to = $to; $this->subject = $subject; $this->message = $message; $this->headers = "From: " . Mailer::FROM_EMAIL . "\r\n"; $this->headers .= "MIME-Version: 1.0\r\n"; $this->headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; 

And here is the code for the email version with the app:

  $this->to = $to; $this->subject = $subject; $this->attachment = chunk_split(base64_encode(file_get_contents($attachment))); //create a boundary string. It must be unique //so we use the MD5 algorithm to generate a random hash $boundary = md5(date('r', time())); $this->headers = "From: webmaster@example.com \r\nReply-To: webmaster@example.com \r\n"; $this->headers .= "MIME-Version: 1.0\r\n "; $this->headers .= "Content-Type: multipart/mixed; boundary=\"PHP-mixed-$boundary\"\r\n"; $this->message = "--PHP-mixed-$boundary Content-Type: text/html; charset=\"ISO-8859-1\" " . $message . " --PHP-mixed-$boundary Content-Type: application/pdf; name=\"test.pdf\" Content-Transfer-Encoding: base64 Content-Disposition: attachment ".$this->attachment ." --PHP-mixed-$boundary--"; 

I use this function to send email:

 public function send(){ if (preg_match(Mailer::PATTERN, trim(strip_tags($this->to)))) { $cleanedTo = trim(strip_tags($this->to)); } else { return FALSE; } return mail ($cleanedTo, $this->subject, $this->message, $this->headers); } 

And I create a Mailer object as follows:

 $mailer = new Mailer(" youremail@gmail.com ", "test mail", "Some <b>old good</b> HTML email", 'pdf/test.pdf'); //$mailer = new Mailer(" youremail@gmail.com ", "test mail", "Some <b>old good</b> HTML email"); $mailer->send(); 

And I get the following message:

 --PHP-mixed-e37929b72bbc6f8e3b37cf802619aac1 Content-Type: text/html; charset="ISO-8859-1" Some <b>old good</b> HTML email --PHP-mixed-e37929b72bbc6f8e3b37cf802619aac1 Content-Type: application/pdf; name="test.pdf" Content-Transfer-Encoding: base64 Content-Disposition: attachment JVBERi0xLjMKMSAwIG9iago8 ... FT0YK --PHP-mixed-e37929b72bbc6f8e3b37cf802619aac1-- 

I think I'm really close to the answer, but ... I need your help to find it.

+4
source share
2 answers
 $this->headers .= "MIME-Version: 1.0\r\n "; $this->headers .= "Content-Type: multipart/mixed; boundary=\"PHP-mixed-$boundary\"\r\n"; 

Remove the space after breaking the line in the MIME-Version line. This ending space before the Content-Type will make it a continuation of the previous line.

Btw: If your code runs on Linux / Unix, use "\ n" only at the end of each line.

+1
source

use this header:

 Content-Type:multipart/mixed; 

I also suggest seeing this class

0
source

All Articles