General e-mail method
When I used to automate mail function via Python is this:
smtplib Import from email.mime.text Import MimeText from email.header Import Header # mailbox server transmits the smtpserver = ' smtp.sina.com ' # Transmit mailbox user / password User = ' [email protected] ' password = ' 123456 ' # send mail SENDER = ' [email protected] ' # receives mail Receiver = ' [email protected] ' # send mail subject Subject = ' Python email the Test ' # type of writing HTML message body msg= MIMEText('<html><h1>你好!</h1></html>','html','utf-8') msg['Subject'] = Header(subject, 'utf-8') # 连接发送邮件 smtp = smtplib.SMTP() smtp.connect(smtpserver) smtp.login(user, password) smtp.sendmail(sender, receiver, msg.as_string()) smtp.quit()
In fact, this code is not complicated, as long as you understand the mailbox used to send mail, then the problem is you need to consider:
Your mail account login / password
Other mail account
Message content (header, body, attachments)
E-mail server (SMTP.xxx.com/pop3.xxx.com)
yagmail realization mail
yagmail may be easier to achieve automatic email function.
github project Address: https://github.com/kootenpv/yagmail
installation
pip install yagmail
A simple example
yagmail Import # linked mailbox server #yag = yagmail.SMTP (the User = " [email protected] " , password = " 1234 " , Host = ' smtp.126.com ' ) YAG = yagmail.SMTP (the User = ' xxx. [email protected] ' , password = ' 11111 ' , Host = ' smtp.exmail.qq.com ' , Port = 465 ) # ' '' # first parameter sender mail address # second parameter code # the third parameter transmission server mailbox # fourth parameter transmit mailbox port # '' ' # Message Body Contents = [' This IS at The body, and here Wallpaper IS the Just text HTTP: //somedomain/image.png ' , ' . The Find by You CAN AN Audio File attached ' , ' /local/path/song.mp3 ' ] title = '' # title #title = U ' message header ' yag.send ( ' [email protected] ' , title, Content, ' accessories Catalog ' , [ ' Cc 1 ' , ' Cc 2 ' ]) # ' ' # The first parameter multi-recipient email address if the recipient can use the list # second argument title # The third parameter text # The fourth parameter to send attachments if attachment list can be used more than # fifth parameter Cc Cc if more people can use the list of # '' ' # Send Mail yag.send ( ' [email protected] ' , ' Subject ' , Contents)
Note:
The title will be garbled when the message is sent successfully, yagmail support uncode coding
The correct title is the
title = u '', this would resolve the problem of garbled
Send a message to multiple users
# Send Mail yag.send ([ ' [email protected] ' , ' [email protected] ' , ' [email protected] ' ], ' Subject ' , Contents)
Just want to receive mail into a list can be.
Send e-mail with attachments
# Send Mail yag.send ( ' [email protected] ' , ' send attachments ' , Contents, [ " D: //log.txt " , " D: //baidu_img.jpg " ])
Just add a list of attachments can be sent.
! # / usr / bin / Python # - * - Coding: UTF . 8 - * - Import yagmail Import SYS from_user = ' [email protected] ' from_pwd = ' Q1w2e3 ' from_host = ' smtp.exmail.qq.com ' from_portt = ' 465 ' # recipient list to_user = ' [email protected] ' # message header title = U ' MySQL slow query statistics ' # body of the message (reception parameter 1) Contents = sys.argv [ 1 ] # Accessory (reception parameter 2) DATE = the sys.argv [ 2 ] the sql_select = ' / the root / mysql_slowLog_file / SELECT / sql_select_ ' + DATE + ' .txt ' REPORT_NAME = ' / the root / mysql_slowLog_file / HTML / mysql_slow_ ' + DATE + ' .html ' File = [sql_select, report_name] # Cc list c_user = ' [email protected] ' # linked mailbox server YAG = yagmail.SMTP (the User = from_user, password = from_pwd, Host = from_host, Port = from_portt) # send e-mail yag.send (to_user, title, Contents,file, c_user)