Python send email is still relatively simple, can be sent by the login mail service, Linux can also use the call SendMail command to send, you can also use local or remote SMTP service to send mail, whether it is a single, Mass, or CC is easier to implement.
First, a few of the simplest way to send messages, such as HTML mail, attachments, etc. are also supported, need to look up documents to
1 Login Mail Service
#!/usr/bin/env python#-*-coding:utf-8-*-#python2.7x#send_simple_email_by_account.py @2014-07-30#author: Orangleliu "Use Python to write mail simple use 126 mailbox Service" ' Import smtplibfrom email.mime.text import mimetextsmtpserver = ' Smtp.126.com ' sender = ' [email protected] ' Password = ' xxxx ' message = ' I send a message by Python. Hello ' msg = mimetext (message) msg[' Subject '] = ' Test Email by Python ' msg[' from '] = sendermsg[' to '] = Destinationmailserver = S Mtplib. SMTP (smtpserver) mailserver.login (sender, password) mailserver.sendmail (sender, [sender], msg.as_string ()) Mailserver.quit () print ' Send email success '
2 Calling the SendMail command (Linux)
#-*-Coding:utf-8-*-#python2.7x#send_email_by_.py#author:orangleliu#date:2014-08-15 " With the SendMail command, this time the mail can still be issued, hostname configuration may need to change the "from Email.mime.text import mimetextfrom subprocess import Popen, Pipedef get_sh_res (): p = Popen (['/application/2.0/nirvana/logs/log.sh '], stdout=pipe) return str ( P.communicate () [0]) def mail_send (sender, Recevier): print "Get email info ..." msg = Mimetext (Get_sh_res ()) msg["from") = Sender msg["to"] = Recevier msg["Subject"] = "Yestoday interface log Results" p = Popen ([" /usr/sbin/sendmail ","-T "], stdin=pipe) res = p.communicate (msg.as_string ()) print ' mail sended ... ' if __name_ _ = = "__main__": s = "[email protected]" r = "[email protected]" mail_send (S, R)
3 using the SMTP service to send (local or remote server)
#!/usr/bin/env python#-*-coding:utf-8-*-#python2.7x#send_email_by_smtp.py#author:orangleliu#date:2014-08-15 " Use the local SMTP service to send mail under Linux If you want to turn on the SMTP service, check the method #ps-ef|grep sendmail#telnet localhost 25 This time the message can be issued, hostname configuration may need to change the " Import smtplibfrom email.mime.text import mimetextfrom subprocess import Popen, Pipedef get_sh_res (): p = Popen (['/ap Plication/2.0/nirvana/logs/log.sh '], stdout=pipe) return str (p.communicate () [0]) def mail_send (sender, Recevier ): msg = Mimetext (Get_sh_res ()) msg["from"] = Sender msg["to"] = Recevier msg["Subject"] = "yestoday Interface Log Results " s = smtplib. SMTP (' localhost ') s.sendmail (sender, [recevier], msg.as_string ()) s.quit () print ' Send mail finished ... ' If __name__ = = ' __main__ ': s = ' [email protected] ' r = s mail_send (S, R)
This article is derived from"Orangleliu Notebook"Blog, be sure to keep this source http://blog.csdn.net/orangleliu/article/details/38591513