django发邮件的功能很简单,只需简单的配置即可,发邮件的代码里面已经封装好了,调用send_mail()函数就可以了
实现多个邮件发送可以用send_mass_mail()函数
先导入send_mail函数from django.core.mail import send_mail
,进入源码里面看看具体函数对应的参数
subject,message,from_email 和recipient_list 这四个参数是必须的。
def send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=None, auth_password=None, connection=None, html_message=None): """ Easy wrapper for sending a single message to a recipient list. All members of the recipient list will see the other recipients in the 'To' field. If auth_user is None, use the EMAIL_HOST_USER setting. If auth_password is None, use the EMAIL_HOST_PASSWORD setting. Note: The API for this method is frozen. New code wanting to extend the functionality should use the EmailMessage class directly. """ connection = connection or get_connection( username=auth_user, password=auth_password, fail_silently=fail_silently, ) mail = EmailMultiAlternatives(subject, message, from_email, recipient_list, connection=connection) if html_message: mail.attach_alternative(html_message, 'text/html') return mail.send()settings.py配置
发送邮件之前先在setting.py配置文件里面配置相关的邮箱信息,比如我这里是用的QQ邮箱,使用SSL加密方式,需要授权码登录
(至于如何获取授权码,可以在QQ邮箱设置里面开启,发送短信“配置邮件客户端”到1069070069)
# settings.py EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' EMAIL_USE_SSL = True # SSL加密方式 EMAIL_HOST = 'smtp.qq.com' # 发送邮件的邮箱 的 SMTP服务器,这里用了163邮箱 EMAIL_PORT = 465 # SMTP服务器端口 EMAIL_HOST_USER = '283340479@qq.com' # 发件人 EMAIL_HOST_PASSWORD = '授权码' # 密码(这里使用的是授权码) EMAIL_FROM = 'yoyo<283340479@qq.com>' # 邮件显示的发件人
如果是其它的企业邮箱,直接密码登录的话,使用TLS方式
EMAIL_USE_SSL = True EMAIL_HOST = 'smtp.xx.com' # 如果是其它企业邮箱 EMAIL_PORT = 25 EMAIL_HOST_USER = 'xxx@xx.com' # 帐号 EMAIL_HOST_PASSWORD = '**********' # 密码 EMAIL_FROM = 'yoyo<xx@xx.com>' # 邮件显示的发件人
EMAIL_USE_SSL 和 EMAIL_USE_TLS 是互斥的,只能有一个为 True。
views和urls.py在views.py里面写个视图函数,调用发送邮件的功能
from django.http import HttpResponse from django.core.mail import send_mail def mail(request): send_mail('Subject here', # 主题 'Here is the message.', # 正文 '283340479@qq.com', # 发件人 ['xxxxx@qq.com'], # 收件人 fail_silently=False) return HttpResponse('邮件发送成功,收不到就去垃圾箱找找吧!')
urls.py写个访问地址触发发邮件
from django.conf.urls import url from hello import views urlpatterns = [ # 新增用户 url(r'^register/', views.register), url(r'^login/', views.login), url(r'^reset/', views.reset_psw), url(r'^mail/', views.mail), ]
浏览器上访问http://localhost:8000/mail/后,就能收到邮件了
前面讲的send_mail()函数只能发送一个邮件,如果想实现发送多个邮件,可以用send_mass_mail()函数
send_mass_mail函数先倒入from django.core.mail import send_mass_mail
查看对应的源码
def send_mass_mail(datatuple, fail_silently=False, auth_user=None, auth_password=None, connection=None): """ 给定tuple数据类型(subject,message,from_email,recipient_list),发送每封邮件到每个收件人列表。 返回发送的电子邮件数量。 如果from_email为None,请使用DEFAULT_FROM_EMAIL设置。 如果设置了auth_user和auth_password,请使用它们登录。 如果auth_user为None,请使用EMAIL_HOST_USER设置。 如果auth_password为None,请使用EMAIL_HOST_PASSWORD设置。 注意:此方法的API已冻结。 想要扩展的新代码功能应该直接使用EmailMessage类。 """ connection = connection or get_connection( username=auth_user, password=auth_password, fail_silently=fail_silently, ) messages = [ EmailMessage(subject, message, sender, recipient, connection=connection) for subject, message, sender, recipient in datatuple ] return connection.send_messages(messages)
从上面介绍可以看出,需传元祖类型的数据,如果想实现更多的给你可以用EmailMessage类
发送多个邮件多个邮件的配置信息放到一个元祖里面,传给datatuple参数,代码实现如下
from django.http import HttpResponse from django.core.mail import send_mail, send_mass_mail # Create your views here. def mass_mail(request): '''发送多个邮件''' message1 = ('Subject 1', 'Here is the message', '2833404xx@qq.com', # 发件人 ['xxx@xx.com']) # 收件人,多个收件人逗号隔开 message2 = ('Another Subject2', 'Here is another message', '2833404xx@qq.com', ['xxx@xx.com']) send_mass_mail((message1, message2), fail_silently=False) return HttpResponse('邮件发送成功,收不到就去垃圾箱找找吧!')