How do I send mail in Python?
One, foreword
Believe that for many people the mailbox is just a trash can full of promotional emails, or a tool to receive captcha. But email has many other functions, the most important of which is messaging.
Nowadays, there are many ways for us to send messages, such as the popular QQ and wechat, or social software such as Weibo and Zhihu. We can even use different ways to communicate, such as Alipay and Taobao.
But these software usually require us to log in, and in the corresponding client to operate. However, email is different. Many languages provide APIS for email-related operations. As long as we have a mailbox, we can send emails at will. And email is much less supervised than other software.
So with all that said, what can we do with email?
In my actual work and study, I like to use email as a reminder tool. Sometimes some programs take a long time to execute, so this allows us to email our phones when the program runs successfully. In this way, we can proceed to the next step in time.
Second, preparation
Before sending an email, we need to obtain an email authorization code. This authorization code is equivalent to your email password and is usually available in the web version of email Settings. Take email 163 as an example. First, log in to email: mail.163.com/. After login, you can see the following page:
We click on Settings, then select POP3/SMTP/IMAP, and see the following page:
Click open, and then send a short message as required to obtain the authorization code. This authorization code is only displayed once, so it needs to be saved. After obtaining the authorization code, we can start sending emails.
3. Send email
The smtplib module comes with Python for sending emails, but it is complicated to use. Today, we directly use the packaged YagMail module to send emails. Let’s first install yagmail:
pip install yagmail
Copy the code
The following use is very simple, the basic steps are roughly as follows:
- Prepare the mailbox for sending messages
- Creating SMTP objects
- Prepare what to send
- Send E-mail
- Close the connection
The specific code is as follows:
import yagmail
# 1, prepare the mailbox for sending mail
username = "[email protected]"
password = "Your authorization code"
# 2. Create SMTP object
yag = yagmail.SMTP(user=username, password=password, host="smtp.163.com")
# 3. Prepare what to send
content = [
"This is an email."
]
# 4. Send an email
yag.send(to="[email protected]", subject="Test mail", contents=content)
# 5. Close the connection
yag.close()
Copy the code
Here are a few more points to note:
STMP (1)
SMTP is actually a mailbox protocol, we use yagmail.SMTP to create SMTP objects, which encapsulates the underlying details for us. All we need to do is give it the email address and authorization code used for sending emails, and give it the IP address of the email server.
Here again comes the concept of A mail server, and the usual flow for sending mail is client A -> Mail server -> client B.
The mail server acts as the postman, and we need to tell the program which postman to send the mail. Since we are using SMTP protocol and the email address is 163, we need to fill in the IP address of email server 163. Generally, the email server IP address is in the format of protocol name. Email company name.com. Of course, this is not fixed, the specific Settings can be viewed in the web version, such as the IP address of netease email is as follows:
(2) Email contents
The email content needs to be a list.
(3) Send an email
Our email code is as follows:
yag.send(to="[email protected]", subject="Test mail", contents=content)
Copy the code
Here we use three parameters, where to is the recipient’s mailbox. Subject is the body of the email and contents is the contents of the email. Yag. send has many other parameters that I won’t go into here.
4. Send attachments
Sending an attachment is very simple, we just need to write the parameters of the attachment in the contents parameter, for example:
import yagmail
username = "[email protected]"
password = "Your authorization code"
yag = yagmail.SMTP(user=username, password=password, host="smtp.163.com")
content = [
Path of attachment
"xyql.jpg"
]
yag.send(to="[email protected]", subject="Test mail", contents=content)
yag.close()
Copy the code
Because I have an image called xyqL.jpg in my program, I’ll just write it. In this case, we can simply call yagmail.inline as follows:
import yagmail
username = "[email protected]"
password = "Your authorization code"
yag = yagmail.SMTP(user=username, password=password, host="smtp.163.com")
content = [
Nana sauce,
yagmail.inline("xyql.jpg")
]
yag.send(to="[email protected]", subject="Test mail", contents=content)
yag.close()
Copy the code
It should be noted that during the test process, we found that we could not directly send the collected pictures alone, but needed to send them with some text, otherwise the email would be returned by the email server.
Let’s take a look at the differences between enclosed and enclosed images:
The difference is clear.
5. Send HTML emails
Yagmail itself sends text as HTML, so nothing needs to change as long as we send HTML mail. But for convenience, we’ll write the HTML as a separate file, such as index.html:
<! DOCTYPEhtml>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>I am a test page</title>
</head>
<body>
<h1>This is a test title</h1>
<p style="color: red">This is a test</p>
</body>
</html>
Copy the code
The following information is displayed:
The effect is very simple, so let’s take the above code and modify it slightly:
username = "[email protected]"
password = "Your authorization code"
yag = yagmail.SMTP(user=username, password=password, host="smtp.163.com")
content = [
Read content directly from an HTML file
open('index.html'.'r', encoding='utf-8').read()
]
yag.send(to="[email protected]", subject="Test mail", contents=content)
yag.close()
Copy the code
Above we read the HTML content directly with open and then send it. Here are the received renderings:
You can see that the mail is receiving normally. The above operations can meet most of the needs of our work, we can customize a reminder program.
That’s all for today. Feel reading. Interested readers can pay attention to my personal public number “new folder X”.