国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python django實現簡單的郵件系統發送郵件功能

2019-11-25 16:01:10
字體:
來源:轉載
供稿:網友

本文實例講述了Python django實現簡單的郵件系統發送郵件功能。分享給大家供大家參考,具體如下:

django郵件系統

Django發送郵件官方中文文檔

總結如下:

1、首先這份文檔看三兩遍是不行的,很多東西再看一遍就通順了。
2、send_mail()、send_mass_mail()都是對EmailMessage類使用方式的一個輕度封裝,所以要關注底層的EmailMessage。
3、異常處理防止郵件頭注入。
4、一定要弄懂Email backends 郵件發送后端
5、多線程的郵件發送。

個人簡單配置如下:

首先是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服務器
外發服務器:smtp.vip.sina.com
收件服務器:pop3.vip.sina.com
新浪免費郵件
外發服務器:smtp.sina.com.cn
收件服務器:pop3.sina.com.cn
163郵箱smtp服務器
pop: pop.163.com
smtp: smtp.163.com
QQ郵箱smtp服務器及端口
接收郵件服務器:imap.exmail.qq.com,使用SSL,端口號993
發送郵件服務器:smtp.exmail.qq.com,使用SSL,端口號465或587
yahoo郵箱smtp服務器
接:pop.mail.yahoo.com.cn
發:smtp.mail.yahoo.com
126郵箱smtp服務器
pop: pop.126.com
smtp: smtp.126.com
新浪免費郵箱
POP3:pop.sina.com
SMTP:smtp.sina.com
SMTP端口號:25
新浪VIP郵箱
POP3:pop3.vip.sina.com
SMTP:smtp.vip.sina.com
SMTP端口號:25
新浪企業郵箱
POP3:pop.sina.com
SMTP:smtp.sina.com
SMTP端口號:25
雅虎郵箱
POP3:pop.mail.yahoo.cn
SMTP:smtp.mail.yahoo.cn
SMTP端口號:25
搜狐郵箱
POP3:pop3.sohu.com
SMTP:smtp.sohu.com
SMTP端口號:25
TOM郵箱
POP3:pop.tom.com
SMTP:smtp.tom.com
SMTP端口號:25
Gmail郵箱
POP3:pop.gmail.com
SMTP:smtp.gmail.com
SMTP端口號:587 或 25
QQ郵箱
POP3:pop.exmail.qq.com
SMTP:smtp.exmail.qq.com
SMTP端口號:25
263郵箱
域名:263.net
POP3:263.net
SMTP:smtp.263.net
SMTP端口號:25
域名:x263.net
POP3:pop.x263.net
SMTP:smtp.x263.net
SMTP端口號:25
域名:263.net.cn
POP3:263.net.cn
SMTP:263.net.cn
SMTP端口號:25
域名:炫我型
POP3:pop.263xmail.com
SMTP:smtp.263xmail.com
SMTP端口號:25
21CN 免費郵箱
POP3:pop.21cn.com
SMTP:smtp.21cn.com
IMAP:imap.21cn.com
SMTP端口號:25
21CN 經濟郵郵箱
POP3:pop.21cn.com
SMTP:smtp.21cn.com
SMTP端口號:25
21CN 商務郵郵箱
POP3:pop.21cn.net
SMTP:smtp.21cn.net
SMTP端口號:25
21CN 快感郵箱
POP3:vip.21cn.com
SMTP:vip.21cn.com
SMTP端口號:25
21CN Y郵箱
POP3:pop.y.vip.21cn.com
SMTP:smtp.y.vip.21cn.com
SMTP端口號:25
中華網任我郵郵箱
POP3:rwpop.china.com
SMTP:rwsmtp.china.com
SMTP端口號:25
中華網時尚、商務郵箱
POP3:pop.china.com
SMTP:smtp.china.com
SMTP端口號:25

那么發送郵件如下:

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','pythonsuper@gmail.com',['1565208411@qq.com','xinxinyu2011@163.com'])#     message2 = ('subject2','this is the message of email2','pythonsuper@gmail.com',['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','from@example.com','1565208411@qq.com'    text_content = 'This is an important message'    html_content = u'<b>激活鏈接:</b><a  rel="external nofollow" >http:www.baidu.com</a>'    msg = EmailMultiAlternatives(subject,text_content,form_email,[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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 崇左市| 民权县| 恩平市| 班玛县| 广州市| 富锦市| 开原市| 苏尼特右旗| 扎鲁特旗| 宝应县| 西贡区| 临猗县| 贡山| 湾仔区| 繁昌县| 聊城市| 兴宁市| 京山县| 横山县| 邮箱| 长白| 花垣县| 泰和县| 长治市| 平塘县| 芜湖市| 北海市| 越西县| 栾川县| 墨脱县| 孟津县| 武义县| 滕州市| 江陵县| 纳雍县| 海丰县| 大洼县| 滨海县| 太和县| 务川| 徐州市|