首先你要申请一个全新的邮箱,首选新浪邮箱。
设置中打开SMTP服务,如下图所示:
项目中的配置:
# settings.py
EMAIL_HOST = "STMP服务器|smtp.sina.com"
EMAIL_PORT = 端口|25
EMAIL_HOST_USER = '邮箱账号'
EMAIL_HOST_PASSWORD = '登录密码'
EMAIL_USE_TLS = TLS加密|True
EMAIL_FROM = '发送者|与EMAIL_HOST_USER保持一致即可'
使用有两种方法
1. send_mail():
from django.core.mail import send_mail
send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=None, auth_password=None, connection=None, html_message=None)
参数解释:
- subject:(str)邮件标题
- message:(str)邮件内容|普通文本
- from_email:(str)发送者
- recipient_list:(list)接收邮箱列表
- fail_silently:(bool)错误异常详情(SMTPException)
- auth_user:(str)可选|邮箱账号|已经配置
- auth_password:(str)可选|邮箱密码|已经配置
- connection:可选|邮件后端|已经配置
- html_message:(str)可选|HTML内容
- 返回值:0或1,代表失败或成功
例:
from django.core.mail import send_mail
send_mail(subject='xxx', message='xxx', from_email=‘1@sina.com’, recipient_list=['1@qq.com', ], fail_silently=False)
2. send_mass_mail():
from django.core.mail import send_mass_mail
datatuple = (subject, message, from_email, recipient_list)
send_mass_mail(datatuple,fail_silently = False,auth_user = None,auth_password = None,connection = None)
参数解释:
- datatuple:(tuple)格式:(subject, message, from_email, recipient_list) |多个收件人可嵌套
- 其余参数同上
- 返回值:成功传递消息的数量。
区别:
send_mass_mail()每次只打开一个连接,而 send_mail() 每发送一个邮箱就打开一次。相对而言send_mass_mail()效率更高。
---End---