python写邮箱系统_Python django实现简单的邮件系统发送邮件功能
本文實例講述了Python django實現(xiàn)簡單的郵件系統(tǒng)發(fā)送郵件功能。分享給大家供大家參考,具體如下:
django郵件系統(tǒng)
Django發(fā)送郵件官方中文文檔
總結(jié)如下:
1、首先這份文檔看三兩遍是不行的,很多東西再看一遍就通順了。
2、send_mail()、send_mass_mail()都是對EmailMessage類使用方式的一個輕度封裝,所以要關(guān)注底層的EmailMessage。
3、異常處理防止郵件頭注入。
4、一定要弄懂Email backends 郵件發(fā)送后端
5、多線程的郵件發(fā)送。
個人簡單配置如下:
首先是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網(wǎng)]' #為郵件Subject-line前綴,默認(rèn)是'[django]'
EMAIL_USE_TLS = True #與SMTP服務(wù)器通信時,是否啟動TLS鏈接(安全鏈接)。默認(rèn)是false
#管理員站點
SERVER_EMAIL = 'xinxinyu2011@163.com' #The email address that error messages come from, such as those sent to ADMINS and MANAGERS.
這里推薦:各大型郵箱smtp服務(wù)器及端口收集
各大型郵箱smtp服務(wù)器及端口收集:
新浪郵箱smtp服務(wù)器
外發(fā)服務(wù)器:smtp.vip.sina.com
收件服務(wù)器:pop3.vip.sina.com
新浪免費郵件
外發(fā)服務(wù)器:smtp.sina.com.cn
收件服務(wù)器:pop3.sina.com.cn
163郵箱smtp服務(wù)器
pop: pop.163.com
smtp: smtp.163.com
QQ郵箱smtp服務(wù)器及端口
接收郵件服務(wù)器:imap.exmail.qq.com,使用SSL,端口號993
發(fā)送郵件服務(wù)器:smtp.exmail.qq.com,使用SSL,端口號465或587
yahoo郵箱smtp服務(wù)器
接:pop.mail.yahoo.com.cn
發(fā):smtp.mail.yahoo.com
126郵箱smtp服務(wù)器
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
新浪企業(yè)郵箱
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 經(jīng)濟(jì)郵郵箱
POP3:pop.21cn.com
SMTP:smtp.21cn.com
SMTP端口號:25
21CN 商務(wù)郵郵箱
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
中華網(wǎng)任我郵郵箱
POP3:rwpop.china.com
SMTP:rwsmtp.china.com
SMTP端口號:25
中華網(wǎng)時尚、商務(wù)郵箱
POP3:pop.china.com
SMTP:smtp.china.com
SMTP端口號:25
那么發(fā)送郵件如下:
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'})
#調(diào)用相應(yīng)的方法
# 方式五:發(fā)送多用途郵件
subject,form_email,to = 'hello','from@example.com','1565208411@qq.com'
text_content = 'This is an important message'
html_content = u'激活鏈接:http:www.baidu.com'
msg = EmailMultiAlternatives(subject,text_content,form_email,[to])
msg.attach_alternative(html_content, 'text/html')
msg.send()
# 發(fā)送郵件成功了給管理員發(fā)送一個反饋
# mail_admins(u'用戶注冊反饋', u'當(dāng)前XX用戶注冊了該網(wǎng)站', fail_silently=True)
return HttpResponse(u'發(fā)送郵件成功')
return render_to_response('common/test.html')
截圖如下:
class Send_mail(object):
'''發(fā)送郵件'''
def __init__(self,sender,passward,receivers):
self.sender=sender
self.password=passward
self.receivers=receivers
def send(self,ShowText,Name,Header_show):
'''
:param ShowText: 發(fā)送內(nèi)容
:param Name: 發(fā)送者
:param Header_show: 發(fā)送文件抬頭
: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()
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python Socket編程技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
本文標(biāo)題: Python django實現(xiàn)簡單的郵件系統(tǒng)發(fā)送郵件功能
本文地址: http://www.cppcns.com/jiaoben/python/196583.html
總結(jié)
以上是生活随笔為你收集整理的python写邮箱系统_Python django实现简单的邮件系统发送邮件功能的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python攻城师的成长————MySQ
- 下一篇: websocket python爬虫_p