Python django实现简单的邮件系统发送邮件功能
本篇章节讲解Python django实现简单的邮件系统发送邮件功能。分享给大家供大家参考,具体如下: django邮件系统 Django发送邮件官方中文文档 总结如下: 1、首先这份文档看三两遍是不行的,很多东西再看一遍就通顺了。 个人简单配置如下: 首先是settings.py文件 #settings.py #邮件配置 EMAIL_HOST = 'smtp.gmail.com' #SMTP地址 EMAIL_PORT = 25 #SMTP端口 EMAIL_HOST_USER = 'pythonsuper@gmail.com' #我自己的邮箱 EMAIL_HOST_PASSWORD = '******' #我的邮箱密码 EMAIL_SUBJECT_PREFIX = u'[CoorCar网]' #为邮件Subject-line前缀,默认是'[django]' EMAIL_USE_TLS = True #与SMTP服务器通信时,是否启动TLS链接(安全链接)。默认是false #管理员站点 SERVER_EMAIL = 'xinxinyu2011@163.com' #The email address that error messages come from,such as those sent to ADMINS and MANAGERS. 这里推荐:各大型邮箱smtp服务器及端口收集 各大型邮箱smtp服务器及端口收集: 新浪邮箱smtp服务器 那么发送邮件如下: def setEmail(request): if request.method == "POST": # 方式一: # send_mail('subject','this is the message of email','pythonsuper@gmail.com',['1565208411@qq.com','1373763906@qq.com'],fail_silently=True) # 方式二: # message1 = ('subject1','this is the message of email1','xinxinyu2011@163.com']) # message2 = ('subject2','this is the message of email2',['1373763906@qq.com','xinxinyu2011@163.com']) # send_mass_mail((message1,message2),fail_silently=False) # 方式三:防止邮件头注入 # try: # send_mail(subject,message,from_email,recipient_list,fail_silently,auth_user,auth_password,connection) # except BadHeaderError: # return HttpResponse('Invaild header fount.') # 方式四:EmailMessage() #首先实例化一个EmailMessage()对象 # em = EmailMessage('subject','body','from@example.com',['1565208411@qq.com'],['xinxinyu2011@163.com'],header={'Reply-to':'another@example.com'}) #调用相应的方法 # 方式五:发送多用途邮件 subject,form_email,to = 'hello','1565208411@qq.com' text_content = 'This is an important message' html_content = u'<b>激活链接:</b><a href="http://www.baidu.com" rel="external nofollow" >http:www.baidu.com</a>' msg = EmailMultiAlternatives(subject,text_content,[to]) msg.attach_alternative(html_content,'text/html') msg.send() # 发送邮件成功了给管理员发送一个反馈 # mail_admins(u'用户注册反馈',u'当前XX用户注册了该网站',fail_silently=True) return HttpResponse(u'发送邮件成功') return render_to_response('common/test.html') 截图如下: class Send_mail(object): '''发送邮件''' def __init__(self,sender,passward,receivers): self.sender=sender self.password=passward self.receivers=receivers def send(self,ShowText,Name,Header_show): ''' :param ShowText: 发送内容 :param Name: 发送者 :param Header_show: 发送文件抬头 :return: ''' message = MIMEText('%s'%(ShowText),'plain','utf-8') message['From'] = Header("%s"%(Name),'utf-8') message['To'] = Header("jiayuguang@qq.com") message['Subject'] = Header("%s"%(Header_show),'utf-8') smtpObj=smtplib.SMTP('smtp.163.com') smtpObj.set_debuglevel(1) smtpObj.login(self.sender,self.password) smtpObj.sendmail(self.sender,self.receivers,message.as_string()) smtpObj.quit() 更多关于Python相关内容可查看本站专题:《Python Socket编程技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》 希望本文所述对大家Python程序设计有所帮助。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |