Django HTML 电子邮件 Gmail 未呈现

Django HTML email Gmail not rendering

嗨,我正在发送电子邮件模板,但 Gmail 未呈现,请参见图片

这是我的 HTML 代码:

<html>

<head>
  <style>
    .banner-color {
      background-color: #eb681f;
    }
    
    .title-color {
      color: #0066cc;
    }
    
    .button-color {
      background-color: #0066cc;
    }
    
    @media screen and (min-width: 500px) {
      .banner-color {
        background-color: #0066cc;
      }
      .title-color {
        color: #eb681f;
      }
      .button-color {
        background-color: #eb681f;
      }
    }
  </style>
</head>

<body>
  <div style="background-color:#ececec;padding:0;margin:0 auto;font-weight:200;width:100%!important">
    <table align="center" border="0" cellspacing="0" cellpadding="0" style="table-layout:fixed;font-weight:200;font-family:Helvetica,Arial,sans-serif" width="100%">
      <tbody>
        <tr>
          <td align="center">
            <center style="width:100%">
              <table bgcolor="#FFFFFF" border="0" cellspacing="0" cellpadding="0" style="margin:0 auto;max-width:512px;font-weight:200;width:inherit;font-family:Helvetica,Arial,sans-serif" width="512">
                <tbody>
                  <tr>
                    <td bgcolor="#F3F3F3" width="100%" style="background-color:#f3f3f3;padding:12px;border-bottom:1px solid #ececec">
                      <table border="0" cellspacing="0" cellpadding="0" style="font-weight:200;width:100%!important;font-family:Helvetica,Arial,sans-serif;min-width:100%!important" width="100%">
                        <tbody>
                          <tr>
                            <td align="left" valign="middle" width="50%"><span style="margin:0;color:#4c4c4c;white-space:normal;display:inline-block;text-decoration:none;font-size:12px;line-height:20px">Webinars</span></td>
                            <td valign="middle" width="50%" align="right" style="padding:0 0 0 10px"><span style="margin:0;color:#4c4c4c;white-space:normal;display:inline-block;text-decoration:none;font-size:12px;line-height:20px">Tuesday 14, February 2017</span></td>
                            <td width="1">&nbsp;</td>
                          </tr>
                        </tbody>
                      </table>
                    </td>
                  </tr>
                  <tr>
                    <td align="left">
                      <table border="0" cellspacing="0" cellpadding="0" style="font-weight:200;font-family:Helvetica,Arial,sans-serif" width="100%">
                        <tbody>
                          <tr>
                            <td width="100%">
                              <table border="0" cellspacing="0" cellpadding="0" style="font-weight:200;font-family:Helvetica,Arial,sans-serif" width="100%">
                                <tbody>
                                  <tr>
                                    <td align="center" bgcolor="#8BC34A" style="padding:20px 48px;color:#ffffff" class="banner-color">
                                      <table border="0" cellspacing="0" cellpadding="0" style="font-weight:200;font-family:Helvetica,Arial,sans-serif" width="100%">
                                        <tbody>
                                          <tr>
                                            <td align="center" width="100%">
                                              <h1 style="padding:0;margin:0;color:#ffffff;font-weight:500;font-size:20px;line-height:24px">Invitation to Revevol webinar</h1>
                                            </td>
                                          </tr>
                                        </tbody>
                                      </table>
                                    </td>
                                  </tr>
                                  <tr>
                                    <td align="center" style="padding:20px 0 10px 0">
                                      <table border="0" cellspacing="0" cellpadding="0" style="font-weight:200;font-family:Helvetica,Arial,sans-serif" width="100%">
                                        <tbody>
                                          <tr>
                                            <td align="center" width="100%" style="padding: 0 15px;text-align: justify;color: rgb(76, 76, 76);font-size: 12px;line-height: 18px;">
                                              <h3 style="font-weight: 600; padding: 0px; margin: 0px; font-size: 16px; line-height: 24px; text-align: center;" class="title-color">Hi {{Name}},</h3>
                                              <p style="margin: 20px 0 30px 0;font-size: 15px;text-align: center;">Join our webinar to discover our latest release. Places are limited, so <b>register now</b>!</p>
                                              <div style="font-weight: 200; text-align: center; margin: 25px;"><a style="padding:0.6em 1em;border-radius:600px;color:#ffffff;font-size:14px;text-decoration:none;font-weight:bold" class="button-color">Join the conference</a></div>
                                            </td>
                                          </tr>
                                        </tbody>
                                      </table>
                                    </td>
                                  </tr>
                                  <tr>
                                  </tr>
                                  <tr>
                                  </tr>
                                </tbody>
                              </table>
                            </td>
                          </tr>
                        </tbody>
                      </table>
                    </td>
                  </tr>
                  <tr>
                    <td align="left">
                      <table bgcolor="#FFFFFF" border="0" cellspacing="0" cellpadding="0" style="padding:0 24px;color:#999999;font-weight:200;font-family:Helvetica,Arial,sans-serif" width="100%">
                        <tbody>
                          <tr>
                            <td align="center" width="100%">
                              <table border="0" cellspacing="0" cellpadding="0" style="font-weight:200;font-family:Helvetica,Arial,sans-serif" width="100%">
                                <tbody>
                                  <tr>
                                    <td align="center" valign="middle" width="100%" style="border-top:1px solid #d9d9d9;padding:12px 0px 20px 0px;text-align:center;color:#4c4c4c;font-weight:200;font-size:12px;line-height:18px">Regards,
                                      <br><b>The Awesome Team</b>
                                    </td>
                                  </tr>
                                </tbody>
                              </table>
                            </td>
                          </tr>
                          <tr>
                            <td align="center" width="100%">
                              <table border="0" cellspacing="0" cellpadding="0" style="font-weight:200;font-family:Helvetica,Arial,sans-serif" width="100%">
                                <tbody>
                                  <tr>
                                    <td align="center" style="padding:0 0 8px 0" width="100%"></td>
                                  </tr>
                                </tbody>
                              </table>
                            </td>
                          </tr>
                        </tbody>
                      </table>
                    </td>
                  </tr>
                </tbody>
              </table>
            </center>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</body>

</html>

在 Django 中,这是我在 admin.py 中用于发送电子邮件模板的代码:

class OrdenAdmin(admin.ModelAdmin):
    list_display = ('cliente', 'completada', 'correo_enviado')
    actions = ['enviar_correo']

    def enviar_correo(self, request, queryset):

        for i in queryset:
            validation = i.correo_enviado

        if validation == False:

            queryset.update(correo_enviado=True)

            for obj in queryset:
                email = obj.cliente.email

            subject = 'Cravings & balloons pedido confirmado'
            html_message = render_to_string(
                'email-order-confirmation.html', {'Sender_Address': 'cokelopez@gmail.com'})
            plain_message = strip_tags(html_message)

            send_mail(subject, plain_message, 'aaaaa@gmail.com',
                      [email], fail_silently=True)

        else:
            for obj in queryset:
                email = obj.cliente.email
            messages.warning(request, "ya se le envió correo a: " + email)

我已经尝试过使用不同代码的不同模板,这些模板应该由 gmail 呈现,查看代码是否有问题,但我找不到问题所在

您必须使用EmailMultiAlternatives发送HTML封邮件,例如:

from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string


def enviar_correo(self, request, queryset):

    ...

    subject = 'Cravings & balloons pedido confirmado'

    html_message = render_to_string('email-order-confirmation.html', {'Sender_Address': '*********@gmail.com'})
    plain_message = strip_tags(html_message)

    email = EmailMultiAlternatives(
        subject,
        plain_message,
        '*********@gmail.com',
        [email],
    )
    email.attach_alternative(html_message, 'text/html')
    email.send()

PS:你不应该发邮件问这个问题。