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)
EmailMultiAlternatives
和 EmailMessage
发送多媒体邮件
# -*- 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()
django —— 邮件的更多相关文章
- Django 邮件推送 解决附件中文名字乱码
Django邮件功能,详情可看官方文档(https://docs.djangoproject.com/en/1.10/topics/email/). 最近做这个功能时遇到的问题,发送附件,收到后中文名 ...
- django日志,django-crontab,django邮件模块
django 日志 四大块,格式器,过滤器,处理器,日志管理器 LOGGING = { 'version': 1, 'disable_existing_loggers': True, 'formatt ...
- django邮件
1.邮件变量 (django settings.py) ADMINS = [('JOHN','JOHN@example.com'),('zhang','zhang@example.com')] #设置 ...
- 毕业设计——Django邮件发送功能实现及问题记录
django发送邮件:send_mail()&send_mass_mail() 自强学堂 刘江的博客 HTTPS,TLS和SSL django发送邮件及其相关问题 步骤 : 0. 登录作为发送 ...
- Django——邮件发送
在settings中添加关键信息 EMAIL_HOST = 'smtp.qq.com' #不同的邮箱有不同的发件地址(收件地址) EMAIL_PORT = 25 #smtp端口号 EMAIL_HOST ...
- python 项目实战之Django 邮件发送
发送邮件¶ 虽然 Python 借助 smtplib 模块简化了发送邮件的流程,但是 Django 在其基础上提供了更简化的支持.这些封装意在加快邮件发送,方便在开发时测试发送邮件,在不支持 SMTP ...
- Django:邮件功能实现
django-users2和django的邮件功能模块都有相关的实现 ----------------------------------------------------------------- ...
- django邮件发送
需要一个邮箱,设置pop3 设置setting EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' EMAIL_HOST = ' ...
- django邮件相关设置
EMAIL_HOST = 'smtp.mxhichina.com' # 阿里云企业邮箱 EMAIL_HOST_PORT = 25 # 邮箱服务器端口 EMAIL_HOST_USER = '****** ...
随机推荐
- Java NIO系列教程(十) Java NIO DatagramChannel
Java NIO中的DatagramChannel是一个能收发UDP包的通道.因为UDP是无连接的网络协议,所以不能像其它通道那样读取和写入.它发送和接收的是数据包. 打开 DatagramChann ...
- Spring ResponseEntity
简单记录下 ResponseEntity 的使用方式 @RequestMapping(value="/demo1" ) public ResponseEntity demo1(){ ...
- Spark2.1.0——剖析spark-shell
在<Spark2.1.0——运行环境准备>一文介绍了如何准备基本的Spark运行环境,并在<Spark2.1.0——Spark初体验>一文通过在spark-shell中执行wo ...
- Nginx初探
nginx是一款轻量级的web服务器.反向代理服务器和电子邮件服务器,占有内存少,并发能力强. 本文将简单介绍如何安装.启动nginx,部署web项目,应用反向代理. 一.安装 可参考https:// ...
- oracle的学习笔记(转)
Oracle的介绍 1. Oracle的创始人----拉里•埃里森 2. oracle的安装 [连接Oracle步骤](](https://img2018.cnblogs.com/blog/12245 ...
- IntelliJ IDEA 启动tomcat服务器报Error running 'Unnamed': Address localhost:1099 is already in use错误的问题
在使用Intellij IDEA运行web项目时,出现 :Error running Tomcat8: Address localhost:1099 is already in use,使其web项目 ...
- java.lang.ClassCastException: java.lang.Short cannot be cast to java.lang.String(Short类型无法强转成String类型)
有一行Java代码如下: String code1 = (String)qTable1.getValueAt(i, 0); 这是一个Java的图形界面获取表格中值的代码,其中qTable1.getVa ...
- 编程输出杨辉三角的前10行---多维数组的应用---java实现
import java.util.Scanner;public class yanghui{ public static void main(String[] args){ Scanner sc=n ...
- Magic Number (zoj3622)
Magic Number (zoj3622) Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/32768K (Java/Oth ...
- POJ3090(SummerTrainingDay04-M 欧拉函数)
Visible Lattice Points Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7450 Accepted: ...