Send emails using Python (complete source code attached)

Table of contents

1. Background

1.1. Preface

1.2. Description

2. SMTP protocol

2.1. Function of SMTP protocol

2.2. SSL function

3. Steps

3.1. Open QQ mailbox SMTP

4. Code

4.1. Complete source code

5. Results

5.1. Code running results

6. Summary

6.1. Summary


1. Background

1.1. Preface

I wrote a simple automated ticket purchase program for 2023 12306. This part is to solve the problem of sending notifications after successful ticket purchase. The content of the notifications is simple Chinese sentences, so you can just learn to send simple emails without in-depth understanding of the protocols used. The specific process of sending and receiving;

Article link: 12306 automated ticket purchasing .

1.2. Description

Operating system: win 10;

Editor:pycharmedu;

Language and version: python 3.10;

Libraries used: smtplib, MIMEText, Header;

Protocol used: SMTP;

Implementation idea: Use the SMTP protocol framework of QQ mailbox to forward content.

2. SMTP protocol

2.1. Function of SMTP protocol

        SMTP (Simple Mail Transfer Protocol) is a simple mail transfer protocol. It is a set of rules for transmitting mail from the source address to the destination address, which controls the letter transfer method.

Keywords: mail transmission, letter transfer

2.2. SSL function

        SSL (Secure Sockets Layer ) Secure Sockets Layer. Used to secure Word Wide Web (WWW) communications. The main task is to provide privacy, information integrity and identity authentication.

Keywords: privacy, identity authentication

3. Steps

3.1. Open QQ mailbox SMTP

Note: The account that sends emails only needs to enable this service, and the service that receives emails does not need to be enabled.

1) Log in to your QQ mailbox on the computer and click Settings after logging in.

As shown in Figure 1 below:

picture

2) Find the account in the email settings and click

As shown in Figure 2 below:

figure 2

 3) Scroll down to find the service to open and verify according to the prompts.

As shown in Figure 3 below

Tips: After the verification is completed, there will be a key, which needs to be remembered;

image 3

4. Code

4.1. Complete source code

What is implemented here is that one of our mailboxes sends a ticket purchase success prompt message to another mailbox (the other mailbox can be any mailbox). Here, the QQ mailbox is used to send it to another QQ mailbox.

import smtplib  # 导入协议
from email.mime.text import MIMEText  # 导入发送包装
from email.header import Header  # 标题包

send_addr = "发送人邮箱"
receive_addr = "接收人邮箱"
key = "验证成功出现的密钥"  # 密钥
text = "成功抢到票,请前往购票平台付款!"  # 发送信息正文内容
smtp_addr = "smtp.qq.com"

msg = MIMEText(text, 'plain', 'utf-8')
msg["From"] = send_addr  # 发送地址
msg["To"] = receive_addr  # 接收地址
msg["Subject"] = Header(text, 'utf-8')  # 标题

server = smtplib.SMTP_SSL(smtp_addr, 465)  # url和端口
server.login(send_addr, key)  # 登录邮箱
server.sendmail(send_addr, receive_addr, msg.as_string())  # 发送方、接收方、发送信息格式等
server.quit()  # 退出

5. Results

5.1. Code running results

As shown in Figure 4 below:

 Figure 4

6. Summary

6.1. Summary

1) The required functions have been completed and the email can be sent successfully;

2) Is there any other better way to remind?

        My mind is blank. .

Guess you like

Origin blog.csdn.net/qq_57663276/article/details/128626069