Python has learned that the use of practical python to 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 use the local or remote SMTP service to send mail, Whether it's a single, Mass, or CC is easier to implement.
This article will be a few of the simplest way to send messages, such as HTML mail, attachments, etc. are also supported, readers can refer to the query when needed. Here's how:
1. Log in to the mail service
The specific code is as follows:
#!/usr/bin/env python#-*-coding:utf-8-*-#python2.7x#send_simple_email_by_account.py @2014-08-18#author: Orangleliu "Use Python to write mail simple use 126 mailbox Service" ' Import smtplibfrom email.mime.text import mimetextsmtpserver = ' Smtp.126.com ' sender = ' 12345678@126.com ' 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. Call the SendMail command (Linux)
The specific code is as follows:
#-*-Coding:utf-8-*-#python2.7x#send_email_by_.py#author:orangleliu#date:2014-08-18 " 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 = "12345678@qq.com" r = "123456@163.com" mail_send (S, R)
3 using the SMTP service to send (local or remote server)
The specific code is as follows:
#!/usr/bin/env python#-*-coding:utf-8-*-#python2.7x#send_email_by_smtp.py#author:orangleliu#date:2014-08-18 " 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 = "123456@163.com" r = S mail_send (S, R)
It is believed that the method shown in this paper can provide some reference value for Python program design.