Multiple headers in Email

Created on 16 April 2023, over 1 year ago
Updated 2 August 2023, over 1 year ago

I am using Drupal 9.5.7 and PHP 8.1 and trying to send email using rules. But the emails looks like the image attached. The Header looks like its being sent multiple times. And the email is not being sent from the email account in the Mime mail conf settings.

πŸ’¬ Support request
Status

Closed: cannot reproduce

Version

1.0

Component

Miscellaneous

Created by

πŸ‡¦πŸ‡ΊAustralia sufian@vivacityhealth.com.au

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • Status changed to Postponed: needs info over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States tr Cascadia

    Well, first off I can't tell anything from a screen shot of a partial email text with a proportional font. Please attach the raw text of the email, including all headers, so that I can see exactly what is being generated by Mime Mail.

    The partial text that you posted looks correct. A multipart/alternative email will have two parts, plain text and html text, with the same boundaries, which is the --5963 etc part in your image. What I can see looks correct, but what I CAN'T see, because you left it out, could be wrong.

    The Header looks like its being sent multiple times.

    You don't show the Headers in your image, so I can't possibly respond to this.

  • πŸ‡¦πŸ‡ΊAustralia sufian@vivacityhealth.com.au

    Thanks for your prompt response. Here the text that I am getting in the email. The problem is that it is coming as plain text and is not being transformed.

    From:
    Date: Fri, 14 Apr 2023 at 9:35 pm
    Subject:
    To:

    This is a multi-part message in MIME format.

    --e23b57a52ff21eb45e3b52db9d85f3879b14601f1
    Content-Type: multipart/alternative;
    boundary="2114a266cb966f31838b5a95c6794c87861e8f760"
    Content-Transfer-Encoding: 8bit

    --2114a266cb966f31838b5a95c6794c87861e8f760
    Content-Type: text/plain; charset=utf-8
    Content-Disposition: inline
    Content-Transfer-Encoding: 8bit

    cedcedc

    --2114a266cb966f31838b5a95c6794c87861e8f760
    Content-Type: text/html; charset=utf-8
    Content-Disposition: inline
    Content-Transfer-Encoding: 8Bit





    Mime Mail Example message template

    cedcedc


    --2114a266cb966f31838b5a95c6794c87861e8f760--

    --e23b57a52ff21eb45e3b52db9d85f3879b14601f1--

  • πŸ‡ΊπŸ‡ΈUnited States tr Cascadia

    That's still not the full raw message source with headers. Your email program will have an option to show message source and to show the headers. Save that as a text file and attach it here by uploading it as a file - don't just paste it into your comment. Things like spaces and blank lines are important and do not show up properly either in images or in a browser.

    The problem here is almost certainly a problem with the headers, but again you are not showing the headers so I can't tell. And if it's a problem with the headers, it's almost certainly a problem with your website configuration or your mailserver configuration. This is unlikely to be a problem with Mime Mail.

  • πŸ‡¦πŸ‡ΊAustralia sufian@vivacityhealth.com.au

    Okay. I have attached the file now with full headers. This time the HTML tags as you might see in the file are being replaced.
    Thanks for your help.

  • πŸ‡ΊπŸ‡ΈUnited States tr Cascadia

    Yes, that is what I needed to see.

    But are you sure you're using Mime Mail and not some other module to send this email?

    The problem I have is that there are several places where it appears that Mime Mail could not possibly have generated this email.

    Specifically, the part boundary is your attachment is: --zROn_pCj
    But Mime Mail can't generate that - Mime Mail uses -- followed by a 40 character hexidecimal number, (which CANNOT include the letters z, R, O, n, p, or j or an underscore, by definition), followed by a single digit part number. This should look something similar to: --c2555f8d5789197906915c87aa81a9594748adf20 and can never look like --zROn_pCj.

    There are other problems as well, such as the absence of the text "This is a multi-part message in MIME format." and the lack of the "Content-Type: multipart/mixed" header, but basically this output does not look like it is coming from Mime Mail so I don't know what to tell you.

  • πŸ‡ΊπŸ‡ΈUnited States tr Cascadia

    The body of your email in #7 looks correct. The body is everything after the MIME-Version: 1.0 header field.

    But the header of your email has Content-Type: text/plain which means the whole body will be interpreted as plain text and your email client will display the HTML tags etc. because the client doesn't know how to properly format the message.

    The Content-Type header field that Mime Mail would have generated for your example in #7 is exactly Content-Type: multipart/mixed; boundary="80ea9dce168d8f737e76f7bdadd269ac41a3b0f61"

    So it looks like you still have some code that is altering the headers generated by Mime Mail.

  • πŸ‡¦πŸ‡ΊAustralia sufian@vivacityhealth.com.au

    Okay so problem for sure arises when we have PHP 8 on. It works absolutely fine on PHP 7.4. Also confirming I have fresh installation of Drupal 9, no extra modules or custom code. Dropping PHP version down to 7.4 makes it work but we cannot do it on the production environment because of other dependencies that require PHP 8.1.

  • πŸ‡¦πŸ‡ΊAustralia sufian@vivacityhealth.com.au

    I am wondering if there is a solution to this ? Or were you able to reproduce this issue at your end or is it just me ?

    Thanks

  • πŸ‡ΊπŸ‡ΈUnited States tr Cascadia

    No, I have never seen this happen. From what you are showing me, the body of the email is now correct and being generated by the Mime Mail module. The only thing that seems to be wrong for you is the Content-Type header.

    As I said in #8, the header of your email has Content-Type: text/plain which means the whole body will be interpreted as plain text and your email client will display the HTML tags etc. because the client doesn't know how to properly format the message.

    The Mime Mail module does not and can not set the Content-Type header field to Content-Type: text/plain.

    The code in Mime Mail looks like this:

        // Header for mail body.
        $body = '';
        $headers = ['Content-Type' => "$content_type; boundary=\"$boundary\""];
        if (!$sub_part) {
          $headers['MIME-Version'] = '1.0';
          $body = 'This is a multi-part message in MIME format.' . static::CRLF;
        }

    This code creates the the MIME-Version: 1.0 header field, and this code creates the This is a multi-part message in MIME format. text that appears in your email body. I can tell that this code is running for you because those two things appear in your email output. But as you can see, this code also generates the Content-Type header with a boundary= part to it. Your Content-Type header does NOT have this boundary= part, and Mime Mail can't generate a Content-Type header without that boundary= part. There is nothing in this that depends on the PHP version.

    So because Mime Mail did not create the Content-Type: text/plain header that appears in your email, the only conclusion I can come to is that this header is being altered by some other code somewhere. Additional information that supports this conclusion comes from your raw output:

    X-MS-Exchange-Organization-MessageDirectionality: Incoming
    X-MS-PublicTrafficType: Email
    X-MS-TrafficTypeDiagnostic:
     MW2NAM04FT027:EE_|BYAPR05MB4920:EE_|CY4PR05MB3559:EE_
    Content-Type: text/plain
    X-MS-Exchange-Organization-AuthSource:
     MW2NAM04FT027.eop-NAM04.prod.protection.outlook.com
    X-MS-Exchange-Organization-AuthAs: Anonymous

    Note that the Content-Type header in your output is mixed in with all the X-MS- headers added by your mail server. This is unusual to see, because I would expect the mail server to either append or pre-pend these additional headers, but that's not what's happening.

    I suggest looking into your mail server logs - you might have to turn on debugging output for this - to examine the exact text of the email that your MTA received from Drupal. I suspect this header is being changed by your server for some reason. If you can see the exact text that Drupal is sending, then perhaps I can spot that reason.

    It's possible this is happening because of line ending characters, which are supposed to be \r\n for emails but PHP has a long history using the wrong line ending and mail servers have a long history of ignoring wrong line endings and Drupal has a long history of using \n instead of \r\n to "avoid" problems. Windows uses different line ending characters by default than Unix, so part of this might also be due to your email server software. One thing I have seen happen is that the mail server doesn't recognize a line ending in the headers, because it is expecting a different character, so some headers get wrapped and appended to each other into one big long header. If that happens, perhaps your server doesn't detect the "Content-Type" header because that only appears somewhere in the body of a big, long, wrapped header. And if the server doesn't detect the "Content-Type" header, perhaps it adds its own default header of text/plain, which is a reasonable default, but it will break the emails sent by Mime Mail. This is just a theory, but line endings are a common source of problems for emails send with PHP and Drupal.

  • Hi,

    I'm running Mime Mail on PHP 8.1 and we have the same problem.
    I have a similar site running on PHP 7.4. There are no problems.
    I will try the patch and let you know if that solves the problem.

  • The patch from #11 solves the problem for me.

  • Status changed to Closed: cannot reproduce over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States tr Cascadia
Production build 0.71.5 2024