Python send out email In three ways , Use login mail server respectively 、 Use smtp service 、 call sendmail Command to send three ways
Please refer to MIPO blog for the original text :Python send out email In three ways
Python send out email Relatively simple , You can send it by logging in to the mail service ,linux You can also call sendmail Order to send , You can also use local or remote smtp Service to send mail , Whether it's a single , Mass hair , It's easier to copy . Benmipo blog first introduces some of the simplest ways to send e-mail , image html mail , Attachments are also supported , If necessary, check the document .
One 、 Log in to the mail server
adopt smtp Log in to a third party smtp E-mail , Support 25 and 465 port
vim python_email_1.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#!/usr/bin/env python # -*- coding:utf-8 -*- # # author: mimvp.com # 2015.10.05 import smtplib from email.mime.text import MIMEText smtpHost = 'smtp.exmail.qq.com' sender = '[email protected]' password = "mimvp-password" receiver = '[email protected]' content = 'hello mimvp.com' msg = MIMEText(content) msg[ 'Subject' ] = 'email-subject' msg[ 'From' ] = sender msg[ 'To' ] = receiver ## smtp port 25 smtpServer = smtplib.SMTP(smtpHost, 25 ) # SMTP smtpServer.login(sender, password) smtpServer.sendmail(sender, receiver, msg.as_string()) smtpServer.quit() print 'send success by port 25' ## smtp ssl port 465 smtpServer = smtplib.SMTP_SSL(smtpHost, 465 ) # SMTP_SSL smtpServer.login(sender, password) smtpServer.sendmail(sender, receiver, msg.as_string()) smtpServer.quit() print 'send success by port 465' |
Carry out orders :
$ python python_email_1.py
send success by port 25
send success by port 465
Send results , You'll get two emails , One of the screenshots is shown below :

Two 、 Use smtp service
Test to fail , Skip or leave a message to correct
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#!/usr/bin/env python # -*- coding:utf-8 -*- # # author: mimvp.com # 2015.10.05 import smtplib from email.mime.text import MIMEText import subprocess smtpHost = 'smtp.exmail.qq.com' sender = '[email protected]' password = "mimvp-password" receiver = '[email protected]' content = 'hello mimvp.com' msg = MIMEText(content) if __name__ = = "__main__" : p = subprocess.Popen([ '/usr/sbin/sendmail' , '-t' ], stdout = subprocess.PIPE) print ( str (p.communicate())) p_res = str (p.communicate()[ 0 ]) msg = MIMEText(p_res) msg[ "From" ] = sender msg[ "To" ] = receiver msg[ "Subject" ] = "hello mimvp.com" s = smtplib.SMTP(smtpHost) s.login(sender, password) s.sendmail(sender, receiver, msg.as_string()) s.quit() print 'send success' |
3、 ... and 、 call sendmail command
Call native linux Oneself sendmail The service sends mail , No need to start sendmail Background processes , No need for sender login , The sender can be any name , There is no limit to .
Particular attention :sendmail Order to send email , The default with 25 Port number , Due to Alibaba cloud 、 Tencent cloud has been banned 25 Port number , Therefore, this example needs to be opened in 25 Port machine test
vim python_email_3.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#!/usr/bin/env python # -*- coding:utf-8 -*- # # author: mimvp.com # 2015.10.05 from email.mime.text import MIMEText from subprocess import Popen, PIPE import commands import sys reload(sys) sys.setdefaultencoding( 'utf-8' ) def send_mail(sender, recevier, subject, html_content): msg = MIMEText(html_content, 'html' , 'utf-8' ) msg[ "From" ] = sender msg[ "To" ] = recevier msg[ "Subject" ] = subject p = Popen([ "/usr/sbin/sendmail" , "-t" ], stdin=PIPE) p.communicate(msg.as_string()) sender = '[email protected]' recevier = '[email protected]' subject = 'sendmail-subject' html_content = 'hello mimvp.com' send_mail(sender, recevier, subject, html_content) |
Carry out orders :
python python_email_3.py
Receiving results :
