2

Django 2, Python 3.

On sending user signup emails using Django default authentication. The sent email contains corrupted html.

contains 3D with each attribute's equal sign.

style=3D"margin:

contains = with each starting new line.

padd= ing-top:20px;

Sample Code:

<div style="padding: 40px; background: #fff;">
    <table style="width: 100%;" cellspacing="0" cellpadding="0" border="0">
        <tbody>
            <tr>
                <td style="border-bottom:1px solid #f6f6f6;">
                    <h1 style="font-size:14px; font-family:arial; margin:0px; font-weight:bold;">{% trans 'Greetings!,' %}</h1>
                    <p style="margin-top:0px; color:#bbbbbb;">{% trans 'Here are your account activation instructions.' %}</p>
                </td>
            </tr>
            <tr>
                <td style="padding:10px 0 30px 0;">
                    <p>{% trans 'Thank you for joining us.' %}</p>
                    <p>{% trans 'To activate your profile, please follow this link:' %}</p>
                    <center>
                        <a href="{{ uri }}" style="display: inline-block; padding: 11px 30px; margin: 20px 0px 30px; font-size: 15px; color: #fff; background: #4fc3f7; border-radius: 60px; text-decoration:none;">{% trans 'Activate Profile' %}</a>
                    </center>
                    <b>- {% trans 'Thanks (project team)' %}</b> </td>
            </tr>
            <tr>
                <td style="border-top:1px solid #f6f6f6; padding-top:20px; color:#777">{% trans 'If the button above does not work, try copying and pasting the URL into your browser. If you continue to have problems, please feel free to contact us at info@project.com' %}</td>
            </tr>
        </tbody>
    </table>
</div>

Generated HTML template:

<div style=3D"padding: 40px; background: #fff;">
    <table style=3D"width: 100%;" cellspacing=3D"0" cellpadding=
    =3D"0" border=3D"0">
    <tbody>
        <tr>
            <td style=3D"border-bottom:1px solid #f6f6f6;">
                <h1 style=3D"font-size:14px; font-family:ar=
                ial; margin:0px; font-weight:bold;">Greetings!,</h1>
                <p style=3D"margin-top:0px; color:#bbbbbb;"=
                >Here are your account activation instructions.</p>
            </td>
        </tr>
        <tr>
            <td style=3D"padding:10px 0 30px 0;">
                <p>Thank you for joining us.</p>
                <p>To activate your profile, please follow =
                this link:</p>
                <center>
                    <a href=3D"http://localhost:8000/accoun=
                    ts/activate/uyi90WsYyvLKmn4wwwqq/" style=3D"display: inline-block; padding:=
                    11px 30px; margin: 20px 0px 30px; font-size: 15px; color: #fff; background=
                    : #4fc3f7; border-radius: 60px; text-decoration:none;">Activate Profile</a>
                </center>
                <b>- Thanks (project team)</b> </td>
            </tr>
            <tr>
                <td style=3D"border-top:1px solid #f6f6f6; padd=
                ing-top:20px; color:#777">If the button above does not work, try copying an=
                d pasting the URL into your browser. If you continue to have problems, plea=
            se feel free to contact us at info@project.com</td>
        </tr>
    </tbody>
</table>
</div>

Please help resolve/identify the issue.

Kaleem Ullah
  • 6,799
  • 3
  • 42
  • 47

1 Answers1

1

I had a similar issue while running tests and wanted to share the issue and how to deal with it

During tests Django sets the email backend to locmem and we use the django.core.mail.outbox which returns an array with the sent emails to test their content

So, considering one sent email as the first element of such array:

from django.core import mail
sent_email = mail.outbox[0]

Checking the content and getting the clear HTML:

sent_email.alternatives[0][0]

Getting the Quoted-Printable encoding (with the =3D):

sent_email.message().get_payload()[1].get_payload()