读取csv文件中的邮箱地址,发送正文为图片超链接的钉钉邮件,需要将“三方客户端安全密码”的开关关闭掉,因PC和APP端的钉钉的“三方客户端安全密码”没有展示,需要登录页面版的阿里邮箱,操作如下:什么是三方客户端安全密码,如何使用该密码_阿里邮箱(Alibaba Mail)-阿里云帮助中心 (aliyun.com)
代码如下:需替换发件人,收件人的邮箱地址,发件人的账密信息
import smtplib
import time
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
def send_email(send_to):
# 钉钉邮件
smtp_server = 'smtp.qiye.aliyun.com' # 替换为你的SMTP服务器地址
username = '123@net.cn' # 替换为你的SMTP用户名,就是发件人邮箱
password = '123' # 替换为你的SMTP密码
sender_email = username # 替换为你的发件人邮箱
# 创建邮件对象
msg = MIMEMultipart()
msg['From'] = username # 发件人地址
msg['To'] = send_to # 收件人地址,可以是多个,用逗号分隔
# msg['To'] = '123@qq.com,123@net.cn' # 收件人地址,可以是多个,用逗号分隔
msg['Cc'] = '' # 抄送人地址,可以是多个,用逗号分隔
msg['Subject'] = '活动预热丨账号免费领!立享 20 个月账号!' # 主题
# 添加文本内容,图片超链接
text = """
<html>
<body>
<a href="https://co.cn/#/Activity">
<img src="cid:image1" alt="Example Image">
</a>
</body>
</html>
"""
# 添加邮件正文
msg.attach(MIMEText(text, 'html'))
# 添加图片
with open('PC.png', 'rb') as file:
img = MIMEImage(file.read())
img.add_header('Content-ID', '<image1>')
msg.attach(img)
# 添加邮件正文
# msg.attach(MIMEText("这是一封测试邮件", 'plain'))
# 添加附件
# file_path = r'E:\data' # 附件地址
# attachment_name = 'jd_07161753.txt' # 替换为附件名称
# for file_name in attachment_name.split(";"):
# with open(file_path + "\\" + file_name, 'rb') as file:
# attachment = MIMEApplication(file.read())
# attachment.add_header('Content-Disposition', 'attachment', filename=file_name)
# msg.attach(attachment)
# 发送邮件
try:
server = smtplib.SMTP(smtp_server)
server.starttls()
server.login(username, password)
server.sendmail(sender_email, msg['To'].split(",") + msg['Cc'].split(","), msg.as_string())
server.quit()
print(msg['To'] + " 邮件发送成功")
except Exception as e:
print('邮件发送失败:', str(e))
if __name__ == '__main__':
with open('telphone.csv', 'r', encoding='utf-8') as f:
lines = f.readlines()
for line in lines:
# send_to = '123@net.cn,25@qq.com,1234@net.cn' # 收件人地址,可以是多个,用逗号分隔
send_email(line)
time.sleep(5)
效果:
如果发送新浪邮件,需要把server.starttls()注释掉,不然报错STARTTLS extension not supported by server.