django —— 邮件

官方文档 1.11

配置settings.py

# QQ邮箱为例, 其他邮箱对应的SMTP配置可查官方
EMAIL_HOST = "smtp.qq.com"
EMAIL_PORT = 465
EMAIL_HOST_USER = "*********@qq.com"
EMAIL_HOST_PASSWORD = "dzptkzrdxcembieg"  
EMAIL_USE_SSL = True
EMAIL_FROM = "no-replay<********@qq.com>"
EMAIL_TO = ["**********@163.com", "**************@163.com"]


send_mail 简易文本邮件

(subject, message, from_email, recipient_list)

from __future__ import absolute_import
import time
import os

import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "BoYa.settings")
django.setup()

from django.core.mail import send_mail
from users.models import ContactInfo
from BoYa.settings import EMAIL_FROM, EMAIL_TO


def send_tip_email():
    u = ContactInfo.objects.order_by("-id").first()
    email_title = "有新的信息"
    email_body = "UserName: {}, \nEmail: {}, \nPhone: {}, \nWebSite: {}, \n" \
                      "Message: {}".format(u.name, u.email, u.phone, u.website, u.message,
                                           time.strftime("%Y-%m-%d %H:%M:%S"))
    send_status = send_mail(email_title, email_body, EMAIL_FROM, ["zxb2031053@163.com"])

    return send_status  #发送成功状态码为1
    

send_mass_mail() 连接一次邮件服务器发送多份不同的邮件

message1 = ('Subject here', 'Here is the message', 'from@example.com', ['first@example.com', 'other@example.com'])
message2 = ('Another Subject', 'Here is another message', 'from@example.com', ['second@test.com'])
send_mass_mail((message1, message2), fail_silently=False)

EmailMultiAlternativesEmailMessage发送多媒体邮件

# -*- coding: utf-8 -*-
from __future__ import absolute_import
import os
import time

import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "BoYa.settings")
django.setup()

from django.core.mail import EmailMultiAlternatives, EmailMessage, send_mail
from BoYa.settings import EMAIL_FROM, EMAIL_HOST_USER

subject, from_email, to = 'hello', EMAIL_FROM, 'zxb2031053@163.com'

text_content = 'This is an important message.'
html_content = '<h1>This is a test <a href="https://www.baidu.com">message</a></h1>' \
               '<p>This is an <strong>important</strong> message.</p>'
# msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
# msg.attach_alternative(html_content, "text/html")
# msg.send()

msg = EmailMessage(subject, html_content, from_email, [to])
msg.content_subtype = "html"  # Main content is now text/html
# msg.attach('email.py', '8888888888877777777777777777777')
msg.attach_file('./email_send.py')
msg.attach_file(r'C:\Users\Belick\Desktop\Stu\BoYa_Project\BoYa\requirements.txt')
msg.send()

发送html模板邮件

from django.template import Context, loader
from users.models import UserProfile
user = UserProfile.objects.all().first()
print user.username
context = {
    'username': user.username,
    'image_url': user.image,
}  # 变量可以在/templates/test.html模板中使用
email_template_name = 'test.html'
t = loader.get_template(email_template_name)
mail_list = ['zxb2031053@163.com',]

msg = EmailMultiAlternatives(subject, t.render(context), from_email, [to])
msg.attach_alternative(t.render(context), "text/html")
msg.send()

posted @ 2018-05-25 21:24  不专业的专业  阅读(243)  评论(0编辑  收藏  举报