People who have learned Python know that the function of using Python to send email is relatively simple, can be sent by login Mail service, Linux can also use the call SendMail command to send, can also use the local or remote SMTP service to send mail, Whether it is a single, Mass, or CC is easier to achieve.
This article will be a few of the most simple way to send mail records, such as HTML mail, accessories, etc. are also supported, readers can refer to the query when needed. The specific methods are as follows:
1. Sign in to 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 messages simple
use 126 of the mailbox Service
'
import smtplib from
Email.mime.text Import mimetext
smtpserver = ' 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 Py Thon '
msg['] = sender
msg[' to '] = destination MailServer
= smtplib. SMTP (smtpserver)
mailserver.login (sender, password)
mailserver.sendmail (sender, [sender], Msg.as_ String ())
mailserver.quit ()
print ' Send email success '
2. Invoke 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. The message may not
be available at this time, hostname configuration may need to change the ' 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 = ' 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
'
Linux uses the local SMTP service to send mail
if you want to open the SMTP service, check the method
#ps-ef|grep sendmail
# telnet localhost
Mail can be sent out at this time, hostname configuration may need to change '
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 = "123456@163.com"
r = S
mail_send (S, R)
It is believed that the method shown in this article can be used for reference in Python programming.