Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multipart/mixed content-type ignored

I had code that outputed a mime email in a hard coded fashion, it was trivially easy. I made a new api to handle any context and as far as I can tell I copied and pasted the relevant mime syntax. Yet gmail displays raw mime.

I'm using the php mail function.

The below code is copied from gmail's "Show Original", which except for the headers is exactly what is seen when viewing the email as if though the "Content-Type: multipart/mixed..." header was seen as text/plain... what am I doing wrong?

Return-Path: <[email protected]>
Received: from www.blah.com (www.blah.com. [xxx.xx.xxx.xx])
        by mx.google.com with ESMTPSA id c8sm28269395qam.21.2014.07.15.16.54.29
        for <[email protected]>
        (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
        Tue, 15 Jul 2014 16:54:29 -0700 (PDT)
Received: by www.blah.com (Postfix, from userid 27)
    id 7BF8B1CF2; Tue, 15 Jul 2014 19:54:28 -0400 (EDT)
To: [email protected]
Subject: blah blah blah
X-PHP-Originating-Script: 0:Blah.php
From: blah <[email protected]>
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="--=_NextPart_BIG_UUID_1"
Message-Id: <[email protected]>
Date: Tue, 15 Jul 2014 18:54:28 -0400 (EDT)

----=_NextPart_BIG_UUID_1  
content-type: multipart/alternative; boundary="--=_NextAltPart_BIG_UUID_2"

----=_NextAltPart_BIG_UUID_2
content-type: text/plain; charset=iso-8859-1
content-transfer-encoding: quoted-printable

test text content 

----=_NextAltPart_BIG_UUID_2
content-type: multipart/related; boundary="--=_NextAltRelPart_BIG_UUID_3"

----=_NextAltRelPart_BIG_UUID_3
content-type: text/html; charset=UTF8
content-transfer-encoding: quoted-printable

<html><body><h1>BLAH</h1></body></html>

----=_NextAltRelPart_BIG_UUID_3
content-type: text/plain;
Content-transfer-encoding: base64
Content-ID: <whatever_doesnt_matter.txt>

dGVzdCBjb250ZW50


----=_NextAltRelPart_BIG_UUID_3--

----=_NextAltPart_BIG_UUID_2--

----=_NextPart_BIG_UUID_1 
Content-Type: application/pdf; name=blah.pdf 
ContentDisposition: attachment;
Content-Transfer-Encoding: base64

JVBERBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLA
HBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLA
HBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLA
HTUzMj5dPj4Kc3RhcnR4cmVmCjQ1ODkyNgolJUVPRgo=


----=_NextPart_BIG_UUID_1--
like image 672
user3338098 Avatar asked Dec 19 '25 01:12

user3338098


1 Answers

It seems fine, and it indeed works fine when I try to recreate the problem (example near the end). You need to pay close attention to the exact MIME syntax so that you produce the correct line endings in headers and delimiters, as described in the syntax below.

Email syntax

This is an excerpt of what the structure of a multi-part message body looks like according to RFC 2046. Pay special attention to the CRLF's. (BNF syntax, somewhat simplified.)


multipart-body := [preamble CRLF]
                  dash-boundary CRLF
                  body-part *encapsulation
                  close-delimiter
                  [CRLF epilogue]

dash-boundary := "--" boundary

body-part := MIME-part-headers [CRLF *OCTET]

encapsulation := delimiter
                 CRLF body-part

delimiter := CRLF dash-boundary

close-delimiter := delimiter "--"

The code

<?php

$headers  = "From: [email protected]\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"--=_NextPart_BIG_UUID_1\"\r\n";

$body  = "----=_NextPart_BIG_UUID_1\r\n";
$body .= "content-type: multipart/alternative; boundary=\"--=_NextAltPart_BIG_UUID_2\"\r\n";
$body .= "\r\n";

$body .= "----=_NextAltPart_BIG_UUID_2\r\n";
$body .= "content-type: text/plain; charset=iso-8859-1\r\n";
$body .= "content-transfer-encoding: quoted-printable\r\n";
$body .= "\r\n";
$body .= "test text content\n";

$body .= "\r\n----=_NextAltPart_BIG_UUID_2";
$body .= "\r\n";
$body .= "content-type: multipart/related; boundary=\"--=_NextAltRelPart_BIG_UUID_3\"\r\n";
$body .= "\r\n";

$body .= "----=_NextAltRelPart_BIG_UUID_3\r\n";
$body .= "content-type: text/html; charset=UTF8\r\n";
$body .= "content-transfer-encoding: quoted-printable\r\n";
$body .= "\r\n";
$body .= "<html><body><h1>BLAH</h1></body></html>\n";

$body .= "\r\n----=_NextAltRelPart_BIG_UUID_3\r\n";
$body .= "content-type: text/plain;\r\n";
$body .= "Content-transfer-encoding: base64\r\n";
$body .= "Content-ID: <whatever_doesnt_matter.txt>\r\n";
$body .= "\r\n";
$body .= "dGVzdCBjb250ZW50\r\n";

$body .= "\r\n----=_NextAltRelPart_BIG_UUID_3--\r\n";

$body .= "\r\n----=_NextAltPart_BIG_UUID_2--\r\n";

$body .= "\r\n----=_NextPart_BIG_UUID_1\r\n";
$body .= "Content-Type: application/pdf; name=blah.pdf\r\n";
$body .= "ContentDisposition: attachment;\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n";
$body .= "\r\n";
$body .= "JVBERBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLA\n";
$body .= "HBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLA\n";
$body .= "HBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLA\n";
$body .= "HTUzMj5dPj4Kc3RhcnR4cmVmCjQ1ODkyNgolJUVPRgo=\r\n";

$body .= "\r\n----=_NextPart_BIG_UUID_1--";

$to = '[email protected]';
$subject = 'MIME Test';

return mail ($to, $subject, $body, $headers);
//echo "$headers\n$body\n";

References

  • RFC 5322 Internet Message Format
  • RFC 2045 (MIME) Part One: Format of Internet Message Bodies
  • RFC 2046 (MIME) Part Two: Media Types
like image 178
Nisse Engström Avatar answered Dec 20 '25 18:12

Nisse Engström



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!