Skip to content

Commit 61d6e49

Browse files
author
guozhen3
committed
python-email
1 parent db53b3b commit 61d6e49

4 files changed

Lines changed: 60 additions & 18 deletions

File tree

README.md

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -527,9 +527,55 @@ predictions = model.predict(data)
527527

528528
1 [使用Python实现群发邮件功能](./md/自动群发邮件.md)
529529

530-
531-
532-
530+
```python
531+
import smtplib
532+
from email import (header)
533+
from email.mime import (text, application, multipart)
534+
from datetime import date, datetime
535+
import time
536+
537+
538+
def sender_mail():
539+
smt_p = smtplib.SMTP() # 创建对象
540+
smt_p.connect(host='smtp.qq.com', port=25) # 设置smtp服务器
541+
sender = '113097485@qq.com' #更换为自己的邮箱
542+
password = "****************" # 在qq邮箱设置开启SMTP服务并复制授权码到password
543+
smt_p.login(sender, password) # 进行邮箱登录一次,填写你本人的邮箱
544+
receiver_addresses, count_num = [
545+
'guozhennianhua@163.com', 'xiaoxiazi99@163.com'], 1
546+
for email_address in receiver_addresses:
547+
try:
548+
msg = multipart.MIMEMultipart()
549+
msg['From'] = "zhenguo" # 设置发邮件人
550+
msg['To'] = email_address # 收件人
551+
# msg['Cc'] = 'guozhennianhua@163.com'
552+
msg['subject'] = header.Header('通知', 'utf-8') # 主题名称
553+
msg.attach(text.MIMEText(
554+
'您好!\n这是一封测试邮件,使用Python实现自动发邮件,请勿回复本邮件功能~\n\n 祝您工作愉快!', 'plain', 'utf-8'))
555+
xlsxpart = application.MIMEApplication(
556+
open(r'./data/email_test.xlsx', 'rb').read())
557+
xlsxpart.add_header('Content-Disposition',
558+
'attachment', filename='1.xlsx')
559+
msg.attach(xlsxpart) # 添加邮件的附件
560+
smt_p.sendmail(sender, email_address, msg.as_string()) # 发送邮件
561+
time.sleep(10) # sleep10秒避免发送频率过快,可能被判定垃圾邮件。
562+
print('%d次发送给%s' % (count_num, email_address))
563+
count_num = count_num + 1
564+
except Exception as e:
565+
print('%d次给%s发送邮件异常' % (count_num, email_address))
566+
continue
567+
smt_p.quit()
568+
569+
570+
sender_mail()
571+
572+
```
573+
574+
575+
576+
发送到`guozhennianhua@163.com`邮件截图:
577+
578+
![](./img/自动接收到的邮件.png)
533579

534580

535581

img/自动发送邮件.png

-45.6 KB
Binary file not shown.

img/自动接收到的邮件.png

58.7 KB
Loading

src/send_email.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,29 @@
11
import smtplib
2-
import time
3-
import xlrd
4-
from email import encoders
5-
from email.header import Header
6-
from email.mime.multipart import MIMEMultipart
7-
from email.mime.text import MIMEText
8-
from email.utils import parseaddr, formataddr
9-
from email.mime.application import MIMEApplication
2+
from email import (header)
3+
from email.mime import (text, application, multipart)
104
from datetime import date, datetime
5+
import time
116

127

138
def sender_mail():
149
smt_p = smtplib.SMTP() # 创建对象
1510
smt_p.connect(host='smtp.qq.com', port=25) # 设置smtp服务器
1611
sender = '113097485@qq.com'
17-
password = "*****************" # 在qq邮箱设置开启SMTP服务并复制授权码到password
12+
password = "rmedemkcefbkbhjc" # 在qq邮箱设置开启SMTP服务并复制授权码到password
1813
smt_p.login(sender, password) # 进行邮箱登录一次,填写你本人的邮箱
19-
receiver_addresses, count_num = ['guozhennianhua@163.com'], 1
14+
receiver_addresses, count_num = [
15+
'guozhennianhua@163.com', 'xiaoxiazi99@163.com'], 1
2016
for email_address in receiver_addresses:
2117
# 表格中邮箱格式不正确,如有空字符,在发邮件的时候会出现异常报错,捕获到这些异常就跳过
2218
try:
23-
msg = MIMEMultipart()
19+
msg = multipart.MIMEMultipart()
2420
msg['From'] = "zhenguo" # 设置发邮件人
2521
msg['To'] = email_address # 收件人
2622
# msg['Cc'] = 'guozhennianhua@163.com'
27-
msg['subject'] = Header('通知', 'utf-8') # 主题名称
28-
msg.attach(
29-
MIMEText('您好!\n这是一封测试邮件,使用Python实现自动发邮件,请勿回复本邮件功能~\n\n 祝您工作愉快!', 'plain', 'utf-8'))
30-
xlsxpart = MIMEApplication(
23+
msg['subject'] = header.Header('通知', 'utf-8') # 主题名称
24+
msg.attach(text.MIMEText(
25+
'您好!\n这是一封测试邮件,使用Python实现自动发邮件,请勿回复本邮件功能~\n\n 祝您工作愉快!', 'plain', 'utf-8'))
26+
xlsxpart = application.MIMEApplication(
3127
open(r'./data/email_test.xlsx', 'rb').read())
3228
xlsxpart.add_header('Content-Disposition',
3329
'attachment', filename='1.xlsx')

0 commit comments

Comments
 (0)