开发者

Why is email displaying mime boundary in body/

开发者 https://www.devze.com 2023-02-17 02:33 出处:网络
I am trying to send an email via PHP mail() with attachments. The attachments come through OK and the content of the body is there but I also see the mime boundary and content type rather than the em

I am trying to send an email via PHP mail() with attachments.

The attachments come through OK and the content of the body is there but I also see the mime boundary and content type rather than the email being HTML

    --==Multipart_Boundary_x9e92752e972e274a182cc4cafd83c674xContent-Type: text/html; charset="iso-8859-1"Content-Transfer-Encoding: 7bit

<p>
    Body content</p>
<p> <snip>

The code looks like this:

 $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";

   $headers = "From: $from\r\n" .
   "MIME-Version: 1.0\r\n" .
      "Content-Type: multipart/mixed;\r\n" .
      " boundary=\"{$mi开发者_开发技巧me_boundary}\"";

    $message = "--{$mime_boundary}Content-Type: text/html; charset=\"iso-8859-1\"\r\n"
    ."Content-Transfer-Encoding: 7bit\r\n\r\n" . $bodyText . "\r\n";   

      $message .=  "--{$mime_boundary}\n" .
      "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
      "Content-Transfer-Encoding: 7bit\n\n" .
   $message . "\n\n";

then the attachment routine

I've tried many variations on the code without success. No doubt it will be something trivial I just can't see.

Thanks in advance


You forgot the correct line endings twice.

   $message = "--{$mime_boundary}Content-Type"

There is the first \r\n missing.

 $message .=  "--{$mime_boundary}\n" .

Here you only wrote \n instead of \r\n.

Also don't forget that you need a terminating MIME boundary at the very end with a closing double minus as terminator:

  $message .=  "--{$mime_boundary}--\n" .


It looks like you're missing the required CRLF after the boundary delimiter, and also possibly that the delimiter may be indented (which it must not be), though that might just be the code formatting here. It also looks like you're using only newlines instead of the mandatory CRLFs.

You should seriously consider using a mature mailing library, like SwiftMailer to assemble MIME messages for you with ease.

0

精彩评论

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

关注公众号