写在前面
我购买的腾讯云默认自带Python3.6.8版本,但是我自己重新编译安装了Python3.6.0版本
环境准备
- 下载Python3.6.0
[root@VM-0-17-centos ~]# wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tgz
[root@VM-0-17-centos ~]# tar -xf Python-3.6.0.tgz
[root@VM-0-17-centos ~]# cd Python-3.6.0/
[root@VM-0-17-centos Python-3.6.0]# ./configure && make && make install
- 为Python3.6.0做软链接
[root@VM-0-17-centos ~]# ln -s /usr/local/bin/python3.6 /usr/bin/python3
[root@VM-0-17-centos ~]# ll /usr/local/bin/python3
lrwxrwxrwx 1 root root 9 Aug 14 11:53 /usr/local/bin/python3 -> python3.6
- 将要发送邮件的邮箱授权
以163邮箱为例:
开启SMTP服务,用手机发送号码进行授权,记得保存授权码!!
- 开始写脚本!
#!/usr/bin/python3
#-*- coding: utf-8 -*- # 使其支持中文字符
import smtplib # 调用模块
from email.header import Header
from email.mime.text import MIMEText
#第三方SMTP服务
host = "smtp.163.com" # 邮箱服务
port = 465 # 端口号
user = "xxxxxxxx@163.com" # 用户名
password = "xxxxxxxxxxx" # 授权码
sender = "xxxxxxxxxxxx@163.com" # 发件人
receivers = ["xxxxxx@163.com",'xxxxxx@163.com','xxxxxx@163.com',"xxxxxx@163.com","xxxxxx@163.com","xxxxxx@163.com"] # 收件人,可以群发邮件,多个用户名就以列表形式展现
content = '''
这是一封测试邮件
内容随你喜欢
随便写
''' # 邮件内容
subject = '测试邮件' # 邮件主题
#发送邮件
def send_email():
msg = MIMEText(content+'\n send_email' ,'plain' ,'utf-8')
msg['From'] = user
msg['To'] = ','.join(receivers) # 群发邮件形式,如果单独发送邮件直接调用receivers即可
msg['Subject'] = subject+' send_email'
try:
#验证
smtpObj = smtplib.SMTP_SSL(host,port) # 启用SSL发短信
smtpObj.login(user,password) # 用户名密码验证
smtpObj.sendmail(sender,receivers,msg.as_string()) # 发送
print("邮件发送成功")
except smtplib.SMTPException as e:
print(e)
if __name__ == '__main__':
send_email()
- 邮件发送成功