python2.7 发邮件脚本

本文介绍了一种使用Python的smtplib和email模块发送带有附件的电子邮件的方法,并讨论了邮件被标记为垃圾邮件的问题及解决策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import smtplib
import time
from email.header import Header
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import sys

reload(sys)
sys.setdefaultencoding('utf8')

msg_from = "xxxxxxxxxxxxxxxxx"  # 发件人邮箱
passwd = "xxxxxxxxxx"  # 邮箱登陆密码
msg_to = ["xxxxxxxxxxxxx", "xxxxxxxxxxxxx"]  # 接收人邮箱

m = MIMEMultipart()
subject = u'xxxxxxxxxxx'
m['Subject'] = Header(subject, 'utf-8')
m['from'] = msg_from
m['to'] = ';'.join(msg_to)
# m["cc"] = msg_from
t = time.strftime('%Y-%m-%d',time.localtime(time.time()))
m.attach(MIMEText(u'xxxxxxxxxxxxxxxxxxxxxxx', 'plain', 'utf-8'))
# 添加xls文件
att1 = MIMEApplication(open('./xxxx/xxx.xlsx', 'rb').read())
att1["Content-Type"] = 'application/octet-stream'
att1.add_header('Content-Disposition', 'attachment', filename=('utf-8', '', u'xxxxxxxxxxxxxx_{}.xlsx'.format(t).encode('utf-8')))
m.attach(att1)

s = smtplib.SMTP("smtp.qiye.163.com", 25)
s.starttls()
s.login(msg_from, passwd)
s.sendmail(msg_from, msg_to, m.as_string())
s.quit()

遇到一个奇葩问题,发送的邮件被判定为垃圾邮件。。

解决方法:

    在网页端设置白名单,或调整反垃圾级别。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值