项目背景:
由于近几年Google对用户隐私越来越重视,所以之前使用的开启“不安全应用登录”的方法,已经被禁止了,这篇文章将介绍怎么使用最新的方法实现python自动发送Google邮件。
解决步骤:
1、在你的google账号中选择“安全性”设置中的“两步验证”,在这里你需要在Google列举的几个验证方式中选取至少2个进行验证。其中必备的是“电话验证”,即验证你的手机号。在这里你可以使用你想验证的手机号,需要能够接收验证码。
另外一个最好是辅助邮箱验证,因为有的人在刚开始创建Google账号的时候,已经填写。或者也可以是其他的验证方式。
2、验证完成之后,接下来最重要的步骤来了(这是最快的步骤):回到账号刚打开的界面,在左上角搜索框输入“专用密码”,点击进去之后启用“专用密码”,命名一个项目。创建之后,会有一个专用密码弹出来,一定要复制,并且不要忘记!!!
忘记之后,重新创建一个即可。
3、准备工作做好之后,就可以开始写python程序发送邮件了。接下来就是python程序的示例:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# Gmail账户信息
gmail_user = '*****@gmail.com'
gmail_password = '********'
# 邮件内容
to_email = '*******@gmail.com'
subject = 'Subject of the Email'
body = 'This is the body of the email.'
# 创建邮件对象
msg = MIMEMultipart()
msg['From'] = gmail_user
msg['To'] = to_email
msg['Subject'] = subject
msg.attach(MIMEText(body, 'plain'))
try:
# 连接到Gmail的SMTP服务器
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls() # 启用TLS加密
server.login(gmail_user, gmail_password) # 登录Gmail账户
text = msg.as_string()
server.sendmail(gmail_user, to_email, text) # 发送邮件
server.quit()
print('Email sent successfully')
except Exception as e:
print(f'Failed to send email: {e}')
在这里,使用到smtplib模块,该模块可专门用于发送google邮件。其中,gmail_user指的是发邮件的地址,gmail_password指的是发邮件账户的专用密码。to_email指的是你要发邮件给谁,body指的是邮件的主要内容,发邮件的时候使用的网络是:smtp.gmail.com,端口号是587。之后就可以发送邮件了。
使用smtplib模块,可以发送不同形式的邮件,你可以将body写为html形式的内容,这样就可以以网页的形式发送邮件,示例如下:
代码:
运行结果:
发送图片附件:
发送word文档附件:
Shell脚本实现:
首先你需要下载ssmtp,来支持发送Google邮件。命令:
yum install ssmtp
配置ssmtp的配置文件:一定要加上TLS_CA_File,有好多网友都是没有加这个导致不能够实现
vim /etc/sstmp/sstmp.conf
#修改文件中的Hostname,mailhub,UseSTARTTLS,UseTLS,AuthUser,
#AuthPass,FromLineOverride,TLS_CA_File,其中的AuthUser,
#AuthPass修改为自己的google邮箱地址和专用密码
#
# /etc/ssmtp.conf -- a config file for sSMTP sendmail.
#
# See the ssmtp.conf(5) man page for a more verbose explanation of the
# available options.
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
# root=liu140397@gmail.com
# The place where the mail goes. The actual machine name is required
# no MX records are consulted. Commonly mailhosts are named mail.domain.com
# The example will fit if you are in domain.com and your mailhub is so named.
mailhub=smtp.gmail.com:587
UseSTARTTLS=YES
UseTLS=YES
AuthUser=liu140397@gmail.com
AuthPass=smhdtjvgxfmlavak
FromLineOverride=YES
Hostname=racknerd-d12b29
# Example for SMTP port number 2525
# mailhub=mail.your.domain:2525
# Example for SMTP port number 25 (Standard/RFC)
# mailhub=mail.your.domain
# Example for SSL encrypted connection
# mailhub=mail.your.domain:465
# Where will the mail seem to come from?
#RewriteDomain=
# The full hostname
#Hostname=
# Set this to never rewrite the "From:" line (unless not given) and to
# use that address in the "from line" of the envelope.
#FromLineOverride=YES
# Use SSL/TLS to send secure messages to server.
#UseTLS=YES
#IMPORTANT: The following line is mandatory for TLS authentication
TLS_CA_File=/etc/pki/tls/certs/ca-bundle.crt
# Use SSL/TLS certificate to authenticate against smtp host.
#UseTLSCert=YES
# Use this RSA certificate.
#TLSCert=/etc/pki/tls/private/ssmtp.pem
# Get enhanced (*really* enhanced) debugging information in the logs
# If you want to have debugging of the config file parsing, move this option
# to the top of the config file and uncomment
#Debug=YES
之后创建1个txt文档检测是否成功:
To: **@gmail.com
From: **@gmail.com
Subject: Test mail from ssmtp
This is a test mail sent from ssmtp on a Linux server.
然后运行sstmp命令,开始发送:
ssmtp ***@gmail.com < testmail.txt
最终是可以实现的。