核心功能 - 自动发送多封邮件

这个脚本可以帮助我们批量定时发送邮件,邮件内容、附件也可以自定义调整,非常的实用。

相比较邮件客户端,Python脚本的优点在于可以智能、批量、高定制化地部署邮件服务。

需要的第三方库:

Email - 用于管理电子邮件消息;

Smtlib - 向SMTP服务器发送电子邮件,它定义了一个 SMTP 客户端会话对象,该对象可将邮件发送到互联网上任何带有 SMTP 或ESMTP 监听程序的计算机;

Pandas - 用于数据分析清洗地工具;

实现代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81


import smtplib 


from email.message import EmailMessage


import pandas as pd


 


def send_email(remail, rsubject, rcontent):


    email = EmailMessage()                          ## Creating a object for EmailMessage


    email['from'] = 'The Pythoneer Here'            ## Person who is sending


    email['to'] = remail                            ## Whom we are sending


    email['subject'] = rsubject                     ## Subject of email


    email.set_content(rcontent)                     ## content of email


    with smtplib.SMTP(host='smtp.gmail.com',port=587)as smtp:     


        smtp.ehlo()                                 ## server object


        smtp.starttls()                             ## used to send data between server and client


        smtp.login("deltadelta371@gmail.com","delta@371") ## login id and password of gmail


        smtp.send_message(email)                    ## Sending email


        print("email send to ",remail)              ## Printing success message


 


if __name__ == '__main__':


    df = pd.read_excel('list.xlsx')


    length = len(df)+1


 


    for index, item in df.iterrows():


        email = item[0]


        subject = item[1]


        content = item[2]


 


        send_email(email,subject,content)