Skip to content

Commit db53b3b

Browse files
author
guozhen3
committed
python-auto-email
1 parent 02df595 commit db53b3b

4 files changed

Lines changed: 19 additions & 57 deletions

File tree

data/email_list.xlsx

-32 Bytes
Binary file not shown.

img/自动发送邮件.png

45.6 KB
Loading
-678 Bytes
Binary file not shown.

src/send_email.py

Lines changed: 19 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -8,74 +8,36 @@
88
from email.utils import parseaddr, formataddr
99
from email.mime.application import MIMEApplication
1010
from datetime import date, datetime
11-
sender = '113097485@qq.com'
12-
receiver = '113097485@qq.com'
13-
14-
15-
def read_excel():
16-
# 打开Excel表格
17-
# 读取群发邮箱,将需要群发的邮箱复制到test的excel列表清单中
18-
ExceFileUrl = xlrd.open_workbook(r'../data/email_list.xlsx')
19-
# print(ExceFileUrl.sheet_names())
20-
# 获取第一个sheet页面
21-
sheet_date = ExceFileUrl.sheet_by_index(0)
22-
# print(sheet_date.name,sheet_date.nrows,sheet_date.ncols)
23-
# 获取第2列的邮箱列表
24-
email_data = sheet_date.col_values(1)
25-
# print(email_data)
26-
return email_data
2711

2812

2913
def sender_mail():
30-
# 创建对象
31-
smt_p = smtplib.SMTP()
32-
# 设置smtp服务器
33-
smt_p.connect(host='smtp.qq.com', port=25)
34-
# 在qq邮箱设置开启SMTP服务并复制授权码
35-
password = "8ey0nd88$$"
36-
# 进行邮箱登录一次,填写你本人的邮箱
37-
smt_p.login(sender, password)
38-
count_num = 1
39-
# 使用for循环来进行发邮件
40-
for i in read_excel():
14+
smt_p = smtplib.SMTP() # 创建对象
15+
smt_p.connect(host='smtp.qq.com', port=25) # 设置smtp服务器
16+
sender = '113097485@qq.com'
17+
password = "*****************" # 在qq邮箱设置开启SMTP服务并复制授权码到password
18+
smt_p.login(sender, password) # 进行邮箱登录一次,填写你本人的邮箱
19+
receiver_addresses, count_num = ['guozhennianhua@163.com'], 1
20+
for email_address in receiver_addresses:
4121
# 表格中邮箱格式不正确,如有空字符,在发邮件的时候会出现异常报错,捕获到这些异常就跳过
4222
try:
43-
44-
# 邮件设置
4523
msg = MIMEMultipart()
46-
msg['From'] = "brook"
47-
# 收件人
48-
msg['To'] = i
49-
msg['Cc'] = 'guozhennianhua@163.com'
50-
# 主题名称
51-
msg['subject'] = Header('通知', 'utf-8')
52-
# 附件 —附加发送excel、word、图片格式,新建文件夹,将以下路径及文件名称替换即可。
24+
msg['From'] = "zhenguo" # 设置发邮件人
25+
msg['To'] = email_address # 收件人
26+
# msg['Cc'] = 'guozhennianhua@163.com'
27+
msg['subject'] = Header('通知', 'utf-8') # 主题名称
5328
msg.attach(
54-
MIMEText('您好,' 'XXX2.0全新升级,XXX1.0版本到2018年10月31号停止所有服务。', 'plain', 'utf-8'))
29+
MIMEText('您好\n这是一封测试邮件,使用Python实现自动发邮件,请勿回复本邮件功能~\n\n 祝您工作愉快!', 'plain', 'utf-8'))
5530
xlsxpart = MIMEApplication(
56-
open(r'../data/email_test.xlsx', 'rb').read())
31+
open(r'./data/email_test.xlsx', 'rb').read())
5732
xlsxpart.add_header('Content-Disposition',
5833
'attachment', filename='1.xlsx')
59-
msg.attach(xlsxpart)
60-
message_docx1 = MIMEText(
61-
open(r'../data/email_test.docx', 'rb').read(), 'base64', 'utf8')
62-
message_docx1.add_header(
63-
'crontent-disposition', 'attachment', filename='测试.docx')
64-
msg.attach(message_docx1)
65-
message_image = MIMEText(
66-
open(r'../data/email_test.jpg', 'rb').read(), 'base64', 'utf8')
67-
message_image.add_header(
68-
'content-disposition', 'attachment', filename='plot2.jpg')
69-
msg.attach(message_image)
70-
# 发送邮件
71-
smt_p.sendmail(sender, i, msg.as_string())
72-
# sleep10秒避免发送频率过快,可能被判定垃圾邮件。
73-
time.sleep(10)
74-
print('第%d次发送给%s' % (count_num, i))
34+
msg.attach(xlsxpart) # 添加邮件的附件
35+
smt_p.sendmail(sender, email_address, msg.as_string()) # 发送邮件
36+
time.sleep(10) # sleep10秒避免发送频率过快,可能被判定垃圾邮件。
37+
print('第%d次发送给%s' % (count_num, email_address))
7538
count_num = count_num + 1
76-
except (UnicodeEncodeError, smtplib.SMTPRecipientsRefused, smtplib.SMTPSenderRefused, AttributeError) as e:
77-
# 打印出来发送第几次的时候,邮箱出问题,一个邮箱最多不超过300个发送对象
78-
print('第%d次给%s发送邮件异常' % (count_num, i))
39+
except Exception as e:
40+
print('第%d次给%s发送邮件异常' % (count_num, email_address))
7941
continue
8042
smt_p.quit()
8143

0 commit comments

Comments
 (0)