When using MIME Mail and SMTP Authentication the output for emails is incorrect.

Email starts off with....
boundary="=_9e927b29a844f2257f15bc0cf0875e3a" --=_9e927b29a844f2257f15bc0cf0875e3a Content-Transfer-Encoding: 8bit Content-Type: text/html; charset=UTF-8

Has the rest of the mail and then...
--=_9e927b29a844f2257f15bc0cf0875e3a Content-Transfer-Encoding: base64 Content-ID: <8376a3e45f72bfd957f7bb16c41fe715> Content-Type: image/png; name=logo.png Content-Disposition: inline; filename=logo.png; size=10205 iVBORw0KGgoAAAANSUhEUgAAAPoAAAD6CAYAAACI7Fo9AAAACXBIWXMAAAsTAAALEwEAmpwYAAAA GXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAJ2pJREFUeNrsnQl8FdX1x09e9gUS 1oQ9hLAUkIQqoqh/gq1bqyXW9t/SKmAXW2utqC1ufwvWqqW1mlqt1GqN2lZbq0JdqtZKaBUFVBK2 CoQQQtgCgSRkX+A/52XGPkLy5t6ZOzP3zpzf5zMG4b2XeTP3O2e5554bc+LECSAppwL9Z752ZOhH fsS/z7b5+asj/lyqHXX6Uar/XQndArUUQ6BLq2z9KIj4M8KcLsn51evgV+pHScSfSQQ6qQ+o8/Wj

And so on....(the base64 encoded image).

Looks like this problem is related to https://www.drupal.org/node/2786907.

Will investigate and submit patch. I need this functionality.

Comments

monstrfolk created an issue. See original summary.

monstrfolk’s picture

Issue summary: View changes
salvis’s picture

Hmm, what is SMTP Authentication's part in this story?

Your patch from #2786907: Issues related to multipart/alternative email certainly looks reasonable. What makes you think it's the culprit?

I'm a bit less sure about the other patch from #1341772: File attachments, but two people confirmed that it was working.

Is SMTP Authentication futzing with the multipart structure in some way? Why would it do that? If we have to choose whether we can run with or without SMTP Authentication, then we have to choose the latter and ask the other module to look into the issue.

salvis’s picture

You're not seeing the #2848309: 500 Error Issues due to $plain issue, are you?

monstrfolk’s picture

No 500 errors and I do not think that the the patch I made is the problem. I have been looking at this issue throughout the day and not 100% certain of the problem. I mentioned the other issue because this issue somewhat resembles the same type of issue.

I believe this issue is coming from the combination of Mail Mine with SMTP authentication. Do not see this behavior when STMP authentication is turned off. It could also be from how MailSystem converts the HTML to plain text. That module does some weird stuff I have noticed.

salvis’s picture

To check out mailsystem_html_to_text(), you could try to temporarily bypass it by just doing a return $string; at the top of the function, and then see whether the corruption goes away.

Otherwise, would you please take it up with project/smtp?

monstrfolk’s picture

Problem is with Mail MIME module. You are the maintainer of that module also, is that correct? Should I move this issue to that module?

monstrfolk’s picture

At least i think it is.....

Everything goes haywire with the message in Mail MINE at the get function

Line 430) public function &get($params = NULL, $filename = NULL, $skip_head = FALSE)

when it calls

Line 441) $parent_get = parent::get($params, $filename, $skip_head);

I don't know if this is because the message is not formatted correctly from HTML Mail or the regex statement in the get function mentioned above.

salvis’s picture

I can't exactly call myself "maintainer" of these two modules, but I try to keep them afloat by committing patches that are contributed and approved by the community, because I'm the maintainer of Subscriptions and many Subscriptions users rely on these modules.

So, yes, if it's a Mail MIME issue, then please move it over there and we'll continue there.

monstrfolk’s picture

Project: HTML Mail » Mail MIME
salvis’s picture

Title: SMTP Authentication with MIME Mail Images » Corruption of MIME structure with images

So SMTP Authentication is not under suspicion anymore, right?

Did this issue pop up after committing #1341772: File attachments? Were you able to attach images before the recent commits to HTML Mail?

monstrfolk’s picture

I have been investigating more. Images work fine with Mail MIME and no SMTP integration. Not working with SMTP integration. I do not think that the commit is the issue and I had never used this module before. My first interaction with HTML Mail was the day I submitted the patch for the other SMTP error (the boundary/content type).

I am starting to believe that SMTP is the culprit, but am still looking into if this module is properly formatting the message data being sent to SMTP modlue. That module is particular about the exact format of the messages it relays.

The message body has boundary="=_9e927b29a844f2257f15bc0cf0875e3a" --=_9e927b29a844f2257f15bc0cf0875e3a Content-Transfer-Encoding: 8bit Content-Type: text/html; charset=UTF-8 in the data right before calling the mail function in HTMLMailSystem__SmtpMailSystem.mail.inc.

Therefore, I still believe this module has something to do with the error. Could you take a look into this at all?

Is the message body supposed to have the boundary and encoding information applied to the front of the message?

salvis’s picture

Could you take a look into this at all?

No, I'm sorry, I've never used these modules and don't know how to install and configure them.

Here's how it's supposed to work:
https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html

ilopata1@hotmail.com’s picture

I am experiencing the same issue when using HTML Mail with SMTP Authentication. All works fine with Mime Mail disabled, but enabling it generates corrupted Mime messages -- neither hmtl not plain text can be read correctly.

salvis’s picture

Would you like to post a good and a bad email message (including all the headers and the MIME structure but without the content)?

spelcheck’s picture

Edit: This appears to simply be an issue of SMTP module using PHPMailer, and in doing so requires the perfectly formatted MIME generated by Mail MIME to be deconstructed, then sent in parts to PHPMailer. It just doesn't deconstruct nested boundaries correctly, and in turn takes whether nested boundaries within the body of the email, strips the headers and sets it as Content Type: text/html. I don't see these two modules playing nice together. MIME Mail module has a 'Link images only'option to prevent auto-embedding images, but that still leaves us with images the end user needs to download. I'll be creating an issue on SMTP that should address auto-embedding images, but nothing needs to change with Mail MIME. (https://www.drupal.org/project/smtp/issues/1813164#comment-13120306)

@salvis, here it is a few years late :P

I'm one of the unfortunate people using HTMLMail (using Mail MIME) and SMTP, hoping to embed images. As a few have stated, the email is delivered is pretty rough shape, displaying a bunch of boundary tags, base64 dumped, etc.

I'm in the early stages of debugging this, but first things first I sent an email through HTMLMail (using Mail MIME)->SMTP, grabbed the source, simplified boundary ids, cids, etc. Then I sent the identical email through PHPMailer itself, then did the same cleaning.

I added linefeeds here and there to each so that when loaded up side-by-side it'll be obvious what is different. I did this because there are some places you can add extra linefeeds, and some places that break the email. Anyways take each of these, save them with an .eml extension and test in your local outlook or whatever you have.

To get the HTMLMail (using Mail MIME)->SMTP email working, I found under PLAIN MESSAGE HERE I only had to add a semicolon after 'charset=UTF-8', remove the 'Content-Transfer-Encoding: 8bit' line, and indent 'boundary=inner_boundary' with at least a space and remove any linefeeds between it and the line above it.

PHPMailer directly (works)

Content-Type: multipart/alternative;
Content-Type: multipart/alternative;
	boundary="outer_boundary"
Content-Transfer-Encoding: 8bit

This is a multi-part message in MIME format.
--outer_boundary
Content-Type: text/plain; charset=us-ascii


PLAIN MESSAGE HERE

--outer_boundary
Content-Type: multipart/related;
	boundary="inner_boundary"



--inner_boundary
Content-Type: text/html; charset=us-ascii


<p>HTML MESSAGE HERE</p>
<img src="cid:my-photo" />

--inner_boundary
Content-Type: image/png; name="test.png"
Content-Transfer-Encoding: base64
Content-ID: <my-photo>
Content-Disposition: inline; filename=test.png




iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAIAAAADnC86AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJ
bWFnZVJlYWR5ccllPAAAAWNJREFUeNrsmMFKw0AQhnc2WWsrFEEpCEXx2KuoFwVBCt705Mmr6Avo
UfHmI/gcngTfwWfwIgjFQJKmyaa72bHeWs0uKSQW6+aQQzbsR2b++Wc2gBdkJhclxILnHOwWf/W1
3W3tnSKq3FU5COpPdzXFywdH7d3NQ20N8DhSz/ekMHiKUFORGlYxDkc3K65ywDArMFoD+SWwEuYa
RZbx4rtNGMjbxhGudQjmJFMp1do6MW3UaL7vXyGPAH6KEFCm6y8Pk4/GvCg8f2zuHFcV3EvQhlrG
QUVQMQisqi34f4Gp41aFcZnJuUTcT6P8UkZEtthw2ILWMJXig5BCfu+Ucbj03czGnMvHugCmA3sH
152zGx049Xve7bbL/TzL/GrlqyTUfvEyJIQkuimg1/dMLSLLVuRHDZLyxQXUpIBRSBRlduayM9df
y7EpyeA4VZ0WVSbFUHt8GvKYTSMCsP9A5h78KcAAc3l8NPTOczMAAAAASUVORK5CYII=
--inner_boundary--

--outer_boundary--

HTMLMail (mimemail) -> SMTP (broken)

Content-Type: multipart/alternative;
 boundary="outer_boundary"
Content-Transfer-Encoding: 8bit

This is a multi-part message in MIME format.
--outer_boundary
Content-Transfer-Encoding: 8bit
Content-Type: text/plain; charset=UTF-8

PLAIN MESSAGE HERE

--outer_boundary
Content-Type: text/html;charset=UTF-8
Content-Transfer-Encoding: 8Bit

boundary=inner_boundary"

--inner_boundary
Content-Transfer-Encoding: 8bit
Content-Type: text/html; charset=UTF-8

<p>HTML MESSAGE HERE</p>
<img src="cid:random_cid" />

--inner_boundary
Content-Transfer-Encoding: base64
Content-ID: <random_cid>
Content-Type: image/png;
 name=test.png
Content-Disposition: inline;
 filename=test.png;
 size=449

iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAIAAAADnC86AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJ
bWFnZVJlYWR5ccllPAAAAWNJREFUeNrsmMFKw0AQhnc2WWsrFEEpCEXx2KuoFwVBCt705Mmr6Avo
UfHmI/gcngTfwWfwIgjFQJKmyaa72bHeWs0uKSQW6+aQQzbsR2b++Wc2gBdkJhclxILnHOwWf/W1
3W3tnSKq3FU5COpPdzXFywdH7d3NQ20N8DhSz/ekMHiKUFORGlYxDkc3K65ywDArMFoD+SWwEuYa
RZbx4rtNGMjbxhGudQjmJFMp1do6MW3UaL7vXyGPAH6KEFCm6y8Pk4/GvCg8f2zuHFcV3EvQhlrG
QUVQMQisqi34f4Gp41aFcZnJuUTcT6P8UkZEtthw2ILWMJXig5BCfu+Ucbj03czGnMvHugCmA3sH
152zGx049Xve7bbL/TzL/GrlqyTUfvEyJIQkuimg1/dMLSLLVuRHDZLyxQXUpIBRSBRlduayM9df
y7EpyeA4VZ0WVSbFUHt8GvKYTSMCsP9A5h78KcAAc3l8NPTOczMAAAAASUVORK5CYII=
--inner_boundary--

--outer_boundary--

The next step is looking in smtp.mail.inc under mailWithoutQueue() function, within // Processes the message's body section. At a glance, it splits the entire email input on the outermost boundary. This leaves us with two body_parts, which it simply looks for 'text/plain' or 'text/html' in each of the parts. It finds 'text/plain' in the first body_part, strips the headers (because PHPMailer automatically sets Content-Type: 'text/plain' for AltBody), then sets it as $mail->AltBody. It then hastily finds 'text/html' in the second body_part, assumes there's nothing else left to be done, strips the headers and sets it as $mail->Body.

The problem is that when processing the message's body, it only knows to look for those two things, and it doesn't expect any nesting. It seems we need to do another _get_substring to get the boundary, and another _boundary_split to get the inner body_parts, then somehow get those inner body_parts sent to PHPMailer so that it formats correctly. Seeing as the direct PHPMailer email includes everything that's needed for the email to be formatted correctly, I think this is doable.

I'll post what I find, but this will at least keep this thread of closing.

spelcheck’s picture

Better plan: disable MailMime and instead have SMTP module auto-attach the files directly through PHPMailer, searching for <img> tags within the body and replacing the urls with corresponding cids used when attaching them with AddEmbeddedImage().

The big reason I had MailMime enabled was to auto-embed images through HTMLMail, so might as well handle this within SMTP. Currently with MailMime disabled, HTMLMail sends the entire email as text/html content type, which withing smtp.mail.inc falls under 'default' in // Processes the message's body section. Will create another case for 'text/html', probably looking for <img> srcs that are relative/root-relative, that way the user still has the option of not embedding an image, simply by using an absolute url.

spelcheck’s picture

Title: Corruption of MIME structure with images » SMTP module breaks multipart structure when images are present.
Status: Active » Closed (works as designed)

Closing, because Mail Mime creates perfectly formatted mime encoded emails and handles embedding of images correctly, but when those images are present SMTP module will always destroy those multipart boundaries. SMTP disassembles mime-encoded emails before pushing it back through PHPMailer. As Mail Mime doesn't have an option to disable the embedding of images, it simply will never work upstream of SMTP module when images are present.

If looking to automatically embed local images and get the benefit of HTML formatted emails, alternate options could be using HTML Mail (without Mail Mime) to SMTP module, or Mime Mail (with embedding of images disabled) to SMTP Module.

A patch is available that brings image embedding into SMTP module here: https://www.drupal.org/project/smtp/issues/1813164

salvis’s picture

Assigned: monstrfolk » Unassigned
Category: Bug report » Support request
Status: Closed (works as designed) » Active

Thank you very much for following up here and pushing the SMTP issue, spelcheck!

If I understand you correctly, then your SMTP patch should solve this issue, when/if it's committed.

Let's keep this open as support request until there's an SMTP release with the fix.