Python Xiaobai realizes mass mailing

                     2020年10月2日

I saw kk’s official account push two days ago.
There is a sentence that is particularly eye-catching.
Effort is the least worthy of boasting, because this is the most basic. In the
past, all the driving forces during the postgraduate entrance examination were kk’s push, and
then there was an internal driving force.
Rarely click kk to push the
ending of the best ferryman

今天发生了一件超级有趣的事情
人生中第一次遇见跟我同名的人,
在某大的群里
哪怕姓不一样,性别也不一样,真的很少见
我说好像和您同名
他说,我的荣幸
我回:荣幸之至
然后有了对方的添加信息
“我是***,CSE PhD student,请多指教”
我们互聊了当下最顶的教授,然后也帮忙推荐了他的博导
互约了有机会回去,一起去hiking.
嗯,在某大,真的会很放心的毫防备的和大家聊,约户外活动,因为大家一心学术,品德纯良,尊重女性,谦逊自信,根本没有所谓的目的性

To learn about mass mailing today, you need to learn two modules. The
methods are smtplib and email.
Among them: smtplib is used to send emails, and email is used to build email content. Both of these are built-in Python modules.
The first line, we understand, is to introduce the smtplib module. In the third line, server is a variable, smtplib.SMTP() is the value of the variable server. We already know that smtplib is the name of the module, so what does SMTP mean?
Please create a new .py file on VS Code on your computer first. Note: This .py file cannot be named email.py, and the folder where you store this py file cannot be email.py. This is because we need to call the email module later. If the file is also named email, it will cause an error.

import smtplib

server = smtplib.SMTP()
server.connect(host, port)
server.login(username, password) 
server.sendmail(sender, to_addr, msg.as_string()) 
server.quit() 

SMTP (Simple Mail Transfer Protocol) translates to "Simple Mail Transfer Protocol". The SMTP protocol is a set of rules for transferring mail from a source server to a destination server. It can be simply understood as: we need to specify a server through SMTP so that the mail can be sent to another server.

import smtplib

server = smtplib.SMTP()
server.connect(host, port)

The fourth line of code does this work, connect to the specified server. host is the mail server to specify the connection, you can specify the domain name of the server. You can find it by searching for "xx mailbox server address". Port means "port". Port belongs to the content of computer network knowledge, you can search for it yourself. Now we only need to know that it is an [integer]. We need to specify the port number used by the SMTP service. Normally, the default port number for SMTP is 25

import smtplib

server = smtplib.SMTP_SSL()
#如果端口是用SSL加密,请这样写代码。其中server是变量名
server.connect('smtp.qq.com', 465)
#如果出现编码错误UnicodeDecodeError,你可以这样写:server.connect('smtp.qq.com', 465,'utf-8')

The fifth line of code, login means login, which is used to log in to the server you specify. You need to enter two parameters: login email and authorization code. The
sixth line of code sendmail means "send mail" and is used to send mail. The sendmail() method requires three parameters: sender, recipient and email content. The sender from_addr here is the same as the username above, both are your login emails, so you only need to set it once.
msg.as_string() is a string type: as_string() changes the sent message msg into a string type.
Take QQ mailbox as an example:

# smtplib 用于邮件的发信动作
import smtplib

# 发信方的信息:发信邮箱,QQ邮箱授权码
from_addr = 'xxx.qq@com'
password = '你的授权码数字'

# 收信方邮箱
to_addr = 'xxx.qq@com'

# 发信服务器
smtp_server = 'smtp.qq.com'

# 开启发信服务,这里使用的是加密传输
server = smtplib.SMTP_SSL()
server.login(smtp_server,465)
# 登录发信邮箱
server.login(from_addr, password)
# 发送邮件
server.sendmail(from_addr, to_addr, msg.as_string())
# 关闭服务器
server.quit()

email module: the module used to write email content. This content can be in various forms such as plain text, HTML content, pictures, attachments, etc.

from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
# smtplib 用于邮件的发信动作
import smtplib
from email.mime.text import MIMEText
# email 用于构建邮件内容

# 发信方的信息:发信邮箱,QQ 邮箱授权码
from_addr = '[email protected]'
password = '你的授权码数字'

# 收信方邮箱
to_addr = '[email protected]'

# 发信服务器
smtp_server = 'smtp.qq.com'

# 邮箱正文内容,第一个参数为内容,第二个参数为格式(plain 为纯文本),第三个参数为编码
msg = MIMEText('send by python','plain','utf-8')

# 开启发信服务,这里使用的是加密传输
server = smtplib.SMTP_SSL()
server.connect(smtp_server,465)
# 登录发信邮箱
server.login(from_addr, password)
# 发送邮件
server.sendmail(from_addr, to_addr, msg.as_string())
# 关闭服务器
server.quit()

If your Python version is 3.7, this error is likely to occur. Because Python 3.7 modified ssl.py, smtplib.SMTP_SSL also caused problems.
There is a problem to discuss.
It’s too sleepy, be lazy~
Good night

Guess you like

Origin blog.csdn.net/m0_48787202/article/details/108903699