Created
June 17, 2019 07:29
-
-
Save mylamour/df1fcb89cc975cd827d7511755a5b834 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: UTF-8 -*- | |
import smtplib | |
import pandas as pd | |
from email.header import Header | |
from email.mime.text import MIMEText | |
# python -m smtpd -n -c DebuggingServer localhost:1025 | |
SERVER = '' | |
USER = '' | |
PASS = '' | |
SENDER = '' | |
SESSION = smtplib.SMTP(SERVER) | |
SESSION.login(USER, PASS) | |
def sendmail(content,owner): | |
recipients = [owner] | |
message = MIMEText(content.encode('utf-8'),'plain','utf-8') # plain or html | |
message['Subject'] = Header("【您有一份礼包待领取】xxxxx",'utf-8') | |
SESSION.sendmail(SENDER, recipients, message.as_string()) | |
if __name__ == '__main__': | |
emailinfo = pd.read_excel('./email.xls') | |
noticeit = emailinfo[emailinfo['是否已安装xxx']=="否"] | |
for mail in zip(noticeit['姓名'],noticeit['电子邮件']): | |
try: | |
name = mail[0] | |
email = mail[1] | |
print("正在向***{}***发送邮件:{}\t\t".format(name, email),end="") | |
line1 = "{},你好:".format(name) | |
msg = "{}\n\t\t{}\n\t".format(line1, line2) | |
sendmail(msg,email) | |
print("--------发送成功-------- ✔️") | |
except Exception as e: | |
print("正在向***{}***发送邮件:{}\t\t".format(name, email),end="") | |
print("--------发送失败-------- ️❎") | |
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# +TLS | |
SESSION = smtplib.SMTP_SSL() | |
SESSION.connect(SMTPADDR, 465) | |
SESSION.login(SENDER, password) | |
MAILMSG = MIMEMultipart() | |
MAILMSG['Subject'] = SUBJECT | |
MAILMSG['From'] = SENDER | |
MAILMSG['To'] = toaddrs | |
MAILMSG.attach(MIMEText(msg, 'html', 'utf-8')) | |
SESSION.sendmail(SENDER, toaddrs, MAILMSG.as_string()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
这个点十分的坑,由于之前并不知道发送邮件是否会有tls的设计,导致代码一直报错,未调试出来(在两个服务器上,一个可以,一个不可以),后来发现其实是因为smtp服务器一个有tls,一个没有。