邮件发送方式二,巧用get_template和EmailMultiAlternatives实现邮件html模板化发送


发布时间:2020-01-20 02:10    作者: Uncle Hui   已过去:6 月,2 周   阅读总量:256 已被赞:0


本篇主要介绍django发送邮件的另一种方式,EmailMultiAlternatives。

需求:

      为了实现博客内容推送,改变邮件内容单一字符串的形式,经过不断思考,结合百度上的经验文章,最后完成效果如下:

  

实现过程:

第一步:配置settings.py,这里和邮件发送方式一的配置是一样的。

#邮箱配置
EMAIL_HOST = "smtp.163.com"  # SMTP服务器主机
EMAIL_PORT = 25             # 端口
EMAIL_HOST_USER = "wan5198@163.com"       # 邮箱地址
EMAIL_HOST_PASSWORD = "你的用于发送邮件的邮箱授权码"    # 授权
EMAIL_USE_TLS= True
EMAIL_FROM = "wan5198@163.com"#邮箱配置

第二步:新建一个py文件,我这里下一步要实现定时发送,所以我新建了一个tasks.py

导入部分:

import datetime #时间
from .models import Read_post #表
from django.core.mail import EmailMultiAlternatives  #邮件服务  *
from django.template.loader import  get_template  #获取指定html *

 函数部分:

def email_subscriptions():
    # 当前的时间:
    now_time = datetime.datetime.now().date()
    # 昨天的时间
    zuo_day_time = now_time - datetime.timedelta(days=1)
    # 昨天阅读的人及文章
    z_day_post = Read_post.objects.filter(read_time=zuo_day_time).all().order_by('-read_num')[:10]
    w ={}
    n=1
    for i in z_day_post:
        w['name%s'%n] = i.post_title
        w['url%s'%n] = i.post_title_id
        n = n+1
    context =w
    # kk.html发送的html模板的名称,放在templates下
    email_template_name = 'kk.html'
    t = get_template(email_template_name)
    html_content = t.render(context)  #将上面的字典传过去
    EMAIL_TITLE = '晖哥哥博客每日文章阅读TOP10推荐' #邮件标题
    DEFAULT_FROM_EMAIL = "wan5198@163.com"  #和setting.py,EMAIL_FROM里配置的要一样
    email = 'wan5198@qq.com' #接收者邮件地址,实际中我们可以传递一个邮件地址列表进去批量发送给很多人
    msg = EmailMultiAlternatives(EMAIL_TITLE, html_content, DEFAULT_FROM_EMAIL, [email])
    msg.attach_alternative(html_content, "text/html")
    msg.send()

 kk.html:

<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<head>
    <title>Vinta</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2 user-scalable = yes">
    <style>
        html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video {
            margin: 0;
            padding: 0;
            border: 0;
            font-size: 100%;
            font: inherit;
            vertical-align: baseline;
        }
        body {
            line-height: 1;
        }
        body {
            background: #424242; /* 标准的语法 */
            font-family: "HelveticaNeue", "Helvetica Neue", Helvetica, Arial, serif;
            font-size: 14px;
            font-weight: 400;
            line-height: 1.5em;
        }
        h1, h2, h3, h4 {
            font-family: "HelveticaNeue", "Helvetica Neue", Helvetica, Arial, serif;
            color: #000000;
            font-style: normal;
            line-height: 1em;
        }
        h1 {
            font-size: 18px;
            text-transform: uppercase;
            font-weight: 700;
            margin-bottom: 15px;
        }
        h2 {
            font-size: 16px;
            font-weight: 700;
            margin-top: 20px;
            margin-bottom: 5px;
        }
        h3 {
            font-size: 15px;
            color: #5e5e5e;
            font-style: italic;
        }
        h4 {
            font-size: 16px;
            font-style: italic;
            font-weight: 400;
            margin-bottom: 0px;
            position: absolute;
            top: -7px;
            width: 130px;
            margin-left: -65px;
            left: 50%;
        }
        #wrapper {
            width: 940px;
            margin: 0 auto;
        }
        .logo {
            width: 276px;
            height: 58px;
            padding: 40px 0px;
            margin: 0 auto;
        }
        /*----- main content of page -----*/
        #content {
            background:#ffffff;
            width: 620px;
            padding: 40px 160px;
            float: left;
            box-shadow: 0px 1px 2px 0px #000000;
            -moz-box-shadow: 0px 1px 2px 0px #000000;
            -webkit-box-shadow: 0px 1px 2px 0px #000000;
            text-align: left;
        }
        .launch {
            font-size: 2em;
            font-weight: bolder;
            width: 402px;
            height: 108px;
            margin: 0 auto;
        }
    </style>
</head>
<body>
<div id="wrapper">
    <div class="logo"></div>
    <!--content starts-->
    <div id="content">
        <div class="launch" align="center"><img src="http://www.hui2017.top/static/2wm.jpg" height="70" width="auto" style="position: relative;left: -80%;top: -40%"/><br>晖哥哥博客每日文章推送<br>TOP10</div>
        <br>
        <br>
        <br>
        <hr>
        <h1> 亲爱的博客订阅用户你好:</h1>
        <h2 style="line-height: 30px"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;昨日晖哥哥博客浏览前十的文章分别是:</h2>
        <ul>
            <li><a href="http://www.hui2017.top/blog/{{ url1 }}">{{ name1 }}</a></li>
            <li><a href="http://www.hui2017.top/blog/{{ url2 }}">{{ name2}}</a></li>
            <li><a href="http://www.hui2017.top/blog/{{ url3 }}">{{ name3 }}</a></li>
            <li><a href="http://www.hui2017.top/blog/{{ url4 }}">{{ name4 }}</a></li>
            <li><a href="http://www.hui2017.top/blog/{{ url5 }}">{{ name5 }}</a></li>
            <li><a href="http://www.hui2017.top/blog/{{ url6 }}">{{ name6 }}</a></li>
            <li><a href="http://www.hui2017.top/blog/{{ url7}}">{{ name7 }}</a></li>
            <li><a href="http://www.hui2017.top/blog/{{ url8 }}">{{ name8 }}</a></li>
            <li><a href=http://www.hui2017.top/blog/"{{ url9 }}">{{ name9 }}</a></li>
            <li><a href=http://www.hui2017.top/blog/"{{ url10 }}">{{ name10 }}</a></li>
        </ul>
    <h2>{{ name }}</h2>
        <br>
        <br>
        <a href="http://www.hui2017.top">
            <button style="width: 620px;border-radius: 3px;background: #3498db;border: 4px;height: 35px;border: #2e6da4;color: white;font-family: 宋体;font-weight: bolder">
                点击前往晖哥哥博客查看更多
            </button>
        </a>
        <br>
        <br>
        <br>
        <hr>
    </div>
</div>
</body>
</html>

 调用函数 email_subscriptions(),测试效果

from .tasks import email_subscriptions
def eeee(request):
    email_subscriptions()
    return HttpResponse(111111)

 

定时发送和批量发送下一篇再记录!主要发送功能已经实现!

点赞

0




登陆后方可评论