django傳送郵件相關配置
阿新 • • 發佈:2018-12-31
1、用django傳送簡單郵件相關配置:
#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.
2、相關郵箱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
3.例項程式碼
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">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')
原文連結:http://www.cnblogs.com/BeginMan/p/3443158.html
另注:
如出現報錯:(553, b'Mail from must equal authorized user', '[email protected]')
檢視函式中的發件郵箱,必須和配置檔案中的傳送郵箱保持一致。