This article mainly introduces Python to send email 3 methods, this article explains the use of the login mail server method, call the SendMail command, use the SMTP service to send three kinds of methods, the need for friends can refer to the
Python send email is relatively simple, you can log in to send the 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 is a single, Mass, or CC is easier to achieve.
A few of the most simple way to send the mail record, like HTML mail, accessories, etc. are also supported, need to check the document can be
1, Login Mail Service
The code is as follows:
#!/usr/bin/env python
#-*-Coding:utf-8-*-
#python2.7x
#send_simple_email_by_account. PY @2014-07-30
#author: Orangleliu
'''''
Write messages using Python simple
Use 126 of the mailbox service
'''
Import Smtplib
From Email.mime.text import Mimetext
SmtpServer = ' smtp.126.com '
Sender = ' liuzhizhi123@126.com '
Password = "xxxx"
Message = ' I send a message by Python. Hello
msg = mimetext (message)
msg[' Subject '] = ' Test Email by Python '
Msg[' from '] = Sender
Msg[' to '] = destination
MailServer = Smtplib. SMTP (SmtpServer, 25)
Mailserver.login (sender, password)
Mailserver.sendmail (sender, [sender], msg.as_string ())
Mailserver.quit ()
print ' Send email success '
2, call SendMail command (Linux)
The code is as follows:
#-*-Coding:utf-8-*-
#python2.7x
#send_email_by_. py
#author: Orangleliu
#date: 2014-08-15
'''''
It's the way the SendMail command is used.
This is a time when mail can be sent out, hostname configuration may need to be changed
'''
From Email.mime.text import Mimetext
From subprocess import Popen, PIPE
Def 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 = "957748332@qq.com"
r = "Zhizhi.liu@chinacache.com"
Mail_send (S, R)
3, using the SMTP service to send (local or remote server)
The code is as follows:
#!/usr/bin/env python
#-*-Coding:utf-8-*-
#python2.7x
#send_email_by_smtp. py
#author: Orangleliu
#date: 2014-08-15
'''''
Use a local SMTP service to send mail under Linux
If you want to start the SMTP service, check the method
#ps-ef|grep SendMail
#telnet localhost 25
This is a time when mail can be sent out, hostname configuration may need to be changed
'''
Import Smtplib
From Email.mime.text import Mimetext
From subprocess import Popen, PIPE
Def get_sh_res ():
p = Popen (['/application/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 = "zhizhi.liu@chinacache.com"
R = S
Mail_send (S, R)