In operation and maintenance development, using Python to send email is a very common application scenario. Today, let's talk about how GitHub's big cow gate uses Python to encapsulate and send email code.
General email method
SMTP is a protocol for sending mail. Python has built-in support for SMTP, which can send plain text mail, HTML mail and mail with attachments.
When we used to realize the automatic mail function through Python, it was like this:
import smtplib
from email.mime.text import MIMEText
from email.header import Header
# Send mailbox server
smtpserver = 'smtp.sina.com'
# Send mailbox user / password
user = 'username@sina.com'
password = '123456'
# Send mailbox
sender = 'username@sina.com'
# Receiving mailbox
receiver = 'receive@126.com'
# Send message subject
subject = 'Python email test'
# Write HTML type message body
msg = MIMEText('<html><h1>Hello!</h1></html>','html','utf-8')
msg['Subject'] = Header(subject, 'utf-8')
# Connect to send mail
smtp = smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(user, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
python needs to master the usage of two modules to send e-mail, smtplib and e-mail. These two modules are brought by python and can be used only by import. The smtplib module is mainly responsible for sending emails, and the email module is mainly responsible for constructing emails.
The smtplib module is mainly responsible for sending mail: it is an action of sending mail, connecting to the mailbox server, logging in to the mailbox, and sending mail (including sender, receiver, and mail content).
E-mail module is mainly responsible for the construction of e-mail: it refers to the construction of e-mail page display, such as sender, recipient, subject, body, attachment, etc.
In fact, this code is not complicated. As long as you understand how to use email, you must consider the following issues:
Your login email account / password
Email account of the other party
Message content (title, body, attachment)
Email server (SMTP.xxx.com/pop3.xxx.com)
What if you want to embed a picture in the body of the email? Can I link the image address directly in HTML mail? The answer is that most mail service providers will automatically block pictures with external links, because they do not know whether these links point to malicious websites.
To embed the image in the body of the email, we just need to add the email as an attachment according to the way of sending the attachment, and then we can embed the attachment as an image by referencing src="cid:0" in HTML. If there are multiple pictures, number them one by one, and then reference different cid:x.
Sending email by YAG mail
YAG mail can be more simple to achieve the automatic mail function.
GitHub project address: https://github.com/kootenpv/yagmail
Open source code, explained as follows:
yag = SMTP(args.user, args.password)
yag.send(to=args.to, subject=args.subject, contents=args.contents, attachments=args.attachments)
Installation:
pip install yagmail
Simple example:
import yagmail
#Link mailbox server
yag = yagmail.SMTP( user="user@126.com", password="1234", host='smtp.126.com')
# Mailbox text
contents = ['This is the body, and here is just text http://somedomain/image.png',
'You can find an audio file attached.', '/local/path/song.mp3']
# Send mail
yag.send('taaa@126.com', 'subject', contents)
Email multiple users:
Just change the receiving mailbox to a list.
yag.send(['aa@126.com','bb@qq.com','cc@gmail.com'], 'subject', contents)
Sending attachments
How do I send attachments? Just add a list of attachments.
yag.send('aaaa@126.com', 'Sending attachments', contents, ["d://log.txt","d://baidu_img.jpg"])
CC
# Email text and attachments
contents = ['This is the body, and here is just text http://somedomain/image.png',
'You can find an audio file attached.', '/local/path/song.mp3', 'Test mail', 'test.html', 'logo.jpg',
'yagmal_test.txt']
# Send out
yag.send(to='xx@xx.com', cc='xxx@xxx.com', subject='Sending attachments', contents=contents)
It's very simple, just out of the box