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

Each large mailbox SMTP server and port collection:

Sina Mailbox SMTP Server
Outgoing servers: smtp.vip.sina.com
Pickup Server: pop3.vip.sina.com
Sina free Mail
Outgoing servers: smtp.sina.com.cn
Pickup Server: pop3.sina.com.cn
163 Mailbox SMTP Server
Pop:pop.163.com
Smtp:smtp.163.com
QQ 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 587
Yahoo! Mailbox SMTP Server
Answer: pop.mail.yahoo.com.cn
Hair: smtp.mail.yahoo.com
126 Mailbox SMTP Server
Pop:pop.126.com
Smtp:smtp.126.com
Sina Free Email
POP3:pop.sina.com
SMTP:smtp.sina.com
SMTP Port number: 25
Sina VIP Mailbox
POP3:pop3.vip.sina.com
SMTP:smtp.vip.sina.com
SMTP Port number: 25
Sina Business Email
POP3:pop.sina.com
SMTP:smtp.sina.com
SMTP Port number: 25
Yahoo Mail
POP3:pop.mail.yahoo.cn
SMTP:smtp.mail.yahoo.cn
SMTP Port number: 25
Sohu Email
POP3:pop3.sohu.com
SMTP:smtp.sohu.com
SMTP Port number: 25
Tom Mailbox
POP3:pop.tom.com
SMTP:smtp.tom.com
SMTP Port number: 25
Gmail mailbox
POP3:pop.gmail.com
SMTP:smtp.gmail.com
SMTP port number: 587 or 25
QQ Mail
POP3:pop.exmail.qq.com
SMTP:smtp.exmail.qq.com
SMTP Port number: 25
263 Email
Domain Name: 263.net
Pop3:263.net
Smtp:smtp.263.net
SMTP Port number: 25
Domain Name: x263.net
POP3:pop.x263.net
SMTP:smtp.x263.net
SMTP Port number: 25
Domain Name: 263.net.cn
pop3:263.net.cn
smtp:263.net.cn
SMTP Port number: 25
Domain name: Dazzle me Type
Pop3:pop.263xmail.com
Smtp:smtp.263xmail.com
SMTP Port number: 25
21CN Free Email
Pop3:pop.21cn.com
Smtp:smtp.21cn.com
Imap:imap.21cn.com
SMTP Port number: 25
21CN Economic e-mail
Pop3:pop.21cn.com
Smtp:smtp.21cn.com
SMTP Port number: 25
21CN Business e-mail
Pop3:pop.21cn.net
Smtp:smtp.21cn.net
SMTP Port number: 25
21CN Pleasure Mailbox
Pop3:vip.21cn.com
Smtp:vip.21cn.com
SMTP Port number: 25
21CN y Mailbox
Pop3:pop.y.vip.21cn.com
Smtp:smtp.y.vip.21cn.com
SMTP Port number: 25
China Web Mail
POP3:rwpop.china.com
SMTP:rwsmtp.china.com
SMTP Port number: 25
China Network fashion, business email
POP3:pop.china.com
SMTP:smtp.china.com
SMTP Port number: 25

Then send the following message:

?
12345678910111213141516171819202122232425262728 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‘)

As follows:

?
12345678910111213141516171819202122 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 implements a Simple mail system send mail function

Related Article

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.