使用迭代的模型数据发送电子邮件html

问题描述 投票:0回答:2

我正在尝试设置每周发送一次的电子邮件警报。我打算为这个函数设置一个cronjob,所以它会这样做。

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText


def send_email():
    try:
        msg = MIMEMultipart('alternative')
        msg['From'] = "<email>"
        msg['To'] = "<email>"
        msg['Subject'] = "sub"

        html = """
        <html>
        <head></head>
        <body>
            <p> hello</p>
        </body>
        </html>
        """
        part1 = MIMEText(html, 'html')

        msg.attach(part1)
        mail = smtplib.SMTP("smtp.email.client", 587, timeout=20)
        mail.starttls()

        recepient = ['<email>']

        mail.login('<email>', '<password>')
        mail.sendmail("[email protected]", recepient, msg.as_string())
        mail.quit()

    except Exception as e:
        raise e

我正在尝试将html镜像作为我项目中的模板之一,在此模板中,我将迭代模型数据以创建图表。

views.朋友

class TestView(LoginRequiredMixin, TemplateView):
    template_name = "test.html"

    def get_context_data(self, **kwargs):
        context = super(TestView, self).get_context_data(**kwargs)
        context['testrail'] = TestRail.objects.all()
        return context

的test.html

    <body>
<h1>4.2.0 Test Plan</h1>
{% for x in testrail %}
<h3>{{x.component}}</h3>
<table class="tg">
<tr>
    <th class="tg-baqh"></th>
    <th class="tg-0lax">#</th>
  </tr>
      <tr>
    <td class="tg-hmp3">Total</td>
    <td class="tg-hmp3">{{x.total_count}}</td>
  </tr>
  <tr>
    <td class="tg-hmp3">Passed</td>
    <td class="tg-hmp3">{{x.passed_count}}</td>
  </tr>
    <tr>
    <td class="tg-0lax">Untested</td>
    <td class="tg-0lax">{{x.untested_count}}</td>
  </tr>
     <tr>
    <td class="tg-0lax">Failed</td>
    <td class="tg-0lax">{{x.failed_count}}</td>
  </tr>
     <tr>
    <td class="tg-0lax">Reviewed</td>
    <td class="tg-0lax">{{x.reviewed_count}}</td>
  </tr>
     <tr>
    <td class="tg-0lax">Harness Failures</td>
    <td class="tg-0lax">{{x.test_harness_issue_count}}</td>
  </tr>
     <tr>
    <td class="tg-0lax">Product Failures</td>
    <td class="tg-0lax">{{x.bug_failure_count}}</td>
  </tr>
    <tr>
    <td class="tg-0lax">Coverage %</td>
    <td class="tg-0lax">{{x.coverage_percentage}}%</td>
  </tr>
    <tr>
    <td class="tg-0lax">Passed %</td>
    <td class="tg-0lax">{{x.passed_percentage}}%</td>
  </tr>
    <tr>
    <td class="tg-0lax">Reviewed %</td>
    <td class="tg-0lax">{{x.reviewed_percentage}}%</td>
  </tr>
    <tr>
    <td class="tg-0lax">Harness Failure %</td>
    <td class="tg-0lax">{{x.harness_percentage}}%</td>
  </tr>
    <tr>
    <td class="tg-0lax">Product Failure %</td>
    <td class="tg-0lax">{{x.product_failure_percentage}}%</td>
  </tr>
  </table>

现在我无法弄清楚如何能够镜像我上面的内容,因为它正在使用模型数据,并使用css文件。

我不一定需要.css文件才能工作,但想知道如何能够在电子邮件的html部分中显示模型数据。

我怎么能够在电子邮件中迭代并显示模型数据(比如在我的模板中)?

django
2个回答
1
投票

你为什么不使用Django的邮件系统。它使用smtplib,但会省去处理细节的麻烦。只需在settings.py中添加基本设置即可

# SMTP Host settings
EMAIL_HOST = ''
EMAIL_PORT = ''

# if your SMTP host needs authentication
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''

# Mail is sent using the SMTP host and port specified in the EMAIL_HOST and
# EMAIL_PORT settings. The EMAIL_HOST_USER and EMAIL_HOST_PASSWORD settings, if
# set, are used to authenticate to the SMTP server, and the EMAIL_USE_TLS and
# EMAIL_USE_SSL settings control whether a secure connection is used.

然后你可以简单地做:

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

   def send_email():
        context = {}
        subject = "subject"
        from_email = "[email protected]"
        body_template = "template.html"
        body = render_to_string(body_template, context).strip()
        to_emails = ["list of emails"]
        email = EmailMessage(subject, body, from_email, to_emails)
        email.content_subtype = "html"
        email.send()

2
投票

你可以使用django模板中的render_to_string方法将你的html和上下文渲染成这样的字符串。

from django.template import loader

text = loader.render_to_string(<TEMPLATE_NAME>, {'x': <MODEL_INSTANCE>})

根据需要使用您的电子邮件发送文本

© www.soinside.com 2019 - 2024. All rights reserved.