Python Django implements a Simple mail system send mail function

Source: Internet
Author: User
Tags imap

Python Django implements a Simple mail system send mail function

This article describes the Python Django implementation of a Simple mail system to send mail functionality.

Django Messaging System

Django Send mail official Chinese document

Summarized as follows:

1, first this document to see 32 times is not good, a lot of things to read again on the fluent.
2, Send_mail (), Send_mass_mail () are a mild package for Emailmessage class usage, so pay attention to the underlying emailmessage.
3. Exception handling prevents message header injection.
4, must understand the email backends mail send back end
5, multi-threaded mail sent.

The personal simple configuration is as follows:

First is the settings.py file

12345678910 #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.
Recommended here: Each large mailbox SMTP server and port collection of each large mailbox SMTP server and port collection: Sina Mailbox SMTP Server outgoing server: smtp.vip.sina.com Inbox server: pop3.vip.sina.com sina free mail outgoing server: smtp.sina.com.cn Recipient server: pop3.sina.com.cn163 mailbox SMTP Server POP:POP.163.COMSMTP: SMTP.163.COMQQ mailbox SMTP server and port incoming mail server: imap.exmail.qq.com, using SSL, port number 993 outgoing mail server: smtp.exmail.qq.com, using SSL, port number 465 or 587yahoo mailbox SMTP Service Adapter: pop.mail.yahoo.com.cn: smtp.mail.yahoo.com126 Mailbox SMTP Server POP:POP.126.COMSMTP: smtp.126.com Sina free Mailbox POP3:pop.sina.comSMTP:smtp.sina.comSMTP port number: 25 Sina VIP mailbox POP3:pop3.vip.sina.comSMTP:smtp.vip.sina.comSMTP port number : 25 Sina Enterprise Mailbox POP3:pop.sina.comSMTP:smtp.sina.comSMTP port number: 25 Yahoo Mail POP3:pop.mail.yahoo.cnSMTP:smtp.mail.yahoo.cnSMTP port number: 25 Sohu Mailbox POP3 : Pop3.sohu.comSMTP:smtp.sohu.comSMTP Port number: 25TOM Mailbox POP3:pop.tom.comSMTP:smtp.tom.comSMTP Port number: 25Gmail Mailbox pop3:pop.gmail.comsmtp:s Mtp.gmail.comSMTP Port number: 587 or 25QQ Mailbox POP3:pop.exmail.qq.comSMTP:smtp.exmail.qq.comSMTP Port number: 25263 mailbox Domain name: 263.NETPOP3:263.NETSMTP:SMTP.263.NETSMTP port number: 25 Domain name: x26 3.NETPOP3:POP.X263.NETSMTP:SMTP.X263.NETSMTP Port number: 25 Domain name: 263.NET.CNPOP3:263.NET.CNSMTP:263.NET.CNSMTP port number: 25 Domain name: Dazzle meType POP3:POP.263XMAIL.COMSMTP:SMTP.263XMAIL.COMSMTP port number: 2521CN Free email pop3:pop.21cn.comsmtp:smtp.21cn.comimap:imap.21cn.comsmtp port number: 2521CN Economic mail POP3:POP.21CN.COMSMTP:SMTP.21CN.COMSMTP port number: 2521CN Business email pop3:pop.21cn.netsmtp:smtp.21cn.netsmtp port number: 2521CN Pleasure Mailbox Pop3:vip.21cn.comsmtp:vip.21cn.comsmtp Port number: 2521CN Y mailbox pop3:pop.y.vip.21cn.comsmtp:smtp.y.vip.21cn.comsmtp Port number: 25 China network mail POP3:rwpop.china.comSMTP:rwsmtp.china.comSMTP port number: 25 Chinese Network fashion, Business mailbox POP3:pop.china.comSMTP:smtp.china.comSMTP port number: 25

  

Then send the following message:

12345678910111213141516171819202122232425262728 defsetEmail(request):  ifrequest.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)    returnHttpResponse(u‘发送邮件成功‘)  returnrender_to_response(‘common/test.html‘)

As follows:

12345678910111213141516171819202122 classSend_mail(object):  ‘‘‘发送邮件‘‘‘  def__init__(self,sender,passward,receivers):    self.sender=sender    self.password=passward    self.receivers=receivers  defsend(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 implements a Simple mail system send mail function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.