I. Environmental description
We usually need to send mail, for alarm, or email verification requirements, this time the environmental requirements are as follows:
CentOS 6.x minimized installation, installation of Postfix (general system installed with a good mail system), if not please do the following:
#yum Install Postfix-y
Python 2.6+
Ii. introduction of Postfix
Postfix is the Linux platform mail system, installed by default, and automatically boot up to run without too much configuration, but there is a little need to explain, postfix
In the host binding the record domain name will not be treated as spam by default, and the host is not bound to record the domain name, most mail vendors will be considered spam.
Third, Pytho e-mail script
#cat send_mail.py
#!/usr/bin/python#coding:utf-8import smtplib# used to define mail From email.mime.text import mimetext #邮件发送的内容msg = "" " linux Basics Links: http://pan.baidu.com/s/ 1mhBDhnM "" "#接收人以字符串的形式列出to_str = " "" [email protected], "" #接收人列表to_list = to_ Str.replace ("\ n", ""). Split (",") #发送人from_user = "[email protected]" #邮件的标题title = "san linux Benefits" Mail = mimetext (msg, "plain", "utf-8") #发送的内容 #内容的类型 #内容的编码 mail["Subject"] = titlemail[ "From"] = from_usermail["to"] = to_str# log on to the SMTP server #postfix  SMTP Server address: localhost (can also be a bound domain name) # smtp server address port: 25 (default) SMTP_SERVER =  " LocalHost "smtp_port = 25server = smtplib. SMTP (Smtp_server,smtp_port) Server.sendmail (from_user,to_list,mail.as_string ()) #发送人 #接收人列表 #发送的内容server. Quit ()
#python send_mail.py
Open QQ e-mail:
650) this.width=650; "title=" Qq20170914113704.png "alt=" wkiol1m5-ccyot7faadqmjzeugs666.png-wh_50 "src=" https:// S2.51cto.com/wyfs02/m00/a5/3c/wkiol1m5-ccyot7faadqmjzeugs666.png-wh_500x0-wm_3-wmp_4-s_884041901.png "/>
As mentioned in the experiment Postfix host does not have the record domain name resolution binding, so in the QQ mailbox is in the garbage box, and my company Mail did not receive! NetEase Filtered out!
In addition, the above is for the local postfix default SMTP non-SSL sent, but also through the following QQ password authentication to send mail
If you are interested, you can try it.
Smtp_server = "smtp.qq.com" smtp_port = 465server = Smtplib. Smtp_ssl (Smtp_server,smtp_port) server.login (From_user, "Tqnmomfayqpodjdh") Server.sendmail (From_user,to_list, Mail.as_string ())
Iv. scripts with Add-ons
#!/usr/bin/python#coding:utf-8import smtplib #负责登录smtp服务器的from email.mime.text import MIMEText #用来定义邮件的from email import MIMEMultipart #定义发送邮件的根容器from email import MIMEBase #定义附件from email import Encoders #对附件进行编码msg = "" " linux Basics Links: Http://pan.baidu.com/s/1mhBDhnM "" "#接收人字符串to_str = "", [email protected], "" "# # #以上邮箱为了隐私写的是假的 #接收人列表to_list = to_str.replace (" \ n " , ""). Split (",") from_user = "[email protected]" #发送人title = "san linux benefits" #邮件的标题 # Instantiates a message root container Message = mimemultipart.mimemultipart () #定义文本项mail = mimetext (msg, " Plain "," Utf-8 ") #发送的内容 #内容的类型 # The content is encoded message.attach (mail) #附件的类型的变量mintype,subtype = "Application", "Octet-stream" #定义附件的类型file_message = mimebase.mimebase (mintype,subType) #附件添加内容with open ("By.txt", "RB") as f: file_message.set_payload ( F.read ()) #对附件进行编码Encoders. Encode_base64 (File_message) #附件的头部定义file_message. Add_header ( " Content-disposition ", " attachment ", filename = " by.txt ") Message.attach (file_message) message[" Subject "] = titlemessage[" from "] = from_usermessage ["To"] = to_str# login SMTP server #qq smtp server:localhost #smtp Server address port: 25smtp_server = "localhost" smtp_port = 25#server = smtplib. Smtp_ssl (Smtp_server,smtp_port) server = smtplib. SMTP (smtp_server,smtp_port) #server. Login (From_user, "") #如果是smtp ssl Encrypted user name password required to log in Server.sendmail (From_user,to_list,message.as_string ()) #发送人 #接收人列表 #发送的内容server. Quit ()
Effects such as:
650) this.width=650; "title=" 333333333333.png "src=" https://s4.51cto.com/wyfs02/M00/06/93/ Wkiom1m6qwgrud76aadm9ijyjmi429.png-wh_500x0-wm_3-wmp_4-s_2257709915.png "alt=" Wkiom1m6qwgrud76aadm9ijyjmi429.png-wh_50 "/>
This article is from the "Learning, learning" blog, please be sure to keep this source http://dyc2005.blog.51cto.com/270872/1965179
Postfix python send mail script configuration