1. 程式人生 > >Python django實現簡單的郵件系統發送郵件功能

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

conn ive smtplib send sub 方式 fix www. obj

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

本文實例講述了Python django實現簡單的郵件系統發送郵件功能。

django郵件系統

Django發送郵件官方中文文檔

總結如下:

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

個人簡單配置如下:

首先是settings.py文件

?
1 2 3 4 5 6 7 8 9 10 #settings.py #郵件配置 EMAIL_HOST = ‘smtp.gmail.com‘ #SMTP地址 EMAIL_PORT = 25 #SMTP端口 EMAIL_HOST_USER = [email protected] #我自己的郵箱 EMAIL_HOST_PASSWORD = ‘******‘ #我的郵箱密碼 EMAIL_SUBJECT_PREFIX = u‘[CoorCar網]‘ #為郵件Subject-line前綴,默認是‘[django]‘
EMAIL_USE_TLS = True #與SMTP服務器通信時,是否啟動TLS鏈接(安全鏈接)。默認是false #管理員站點 SERVER_EMAIL = [email protected] #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

那麽發送郵件如下:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 def setEmail(request): if request.method == "POST": # 方式一: # send_mail(‘subject‘, ‘this is the message of email‘, ‘[email protected]‘, [‘[email protected]‘,‘[email protected]‘], fail_silently=True) # 方式二: # message1 = (‘subject1‘,‘this is the message of email1‘,‘[email protected]‘,[‘[email protected]‘,‘[email protected]‘]) # message2 = (‘subject2‘,‘this is the message of email2‘,‘[email protected]‘,[‘[email protected]‘,‘[email protected]‘]) # 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‘,‘[email protected]‘,[‘[email protected]‘],[‘[email protected]‘],header={‘Reply-to‘:‘[email protected]‘}) #調用相應的方法 # 方式五:發送多用途郵件 subject,form_email,to = ‘hello‘,[email protected],[email protected] 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,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‘)

截圖如下:

技術分享圖片

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 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("[email protected]") 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 django實現簡單的郵件系統發送郵件功能