python: emailhelper

 

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
"""
python 发送邮件,用授权码
 
"""
import smtplib
from email.mime.text import MIMEText
 
 
def send():
    """
     发送邮件
    :return:
    """
    try:
        stmpserver163='smtp.163.com'
        stmpport=25
        frommail='geovindu@163.com'
        code163='geovindu'
 
        toemails=['463588883@qq.com','geovindu@jw28.com']
        #创建邮件:
        msg=MIMEText('<h3>hi, how are you. send mail<h3>','html','utf-8'#邮件内容
        msg['Subject'] = 'ICT 公益培训 考试日期通知'   #主题
        msg['To']=','.join(toemails)  #收件人
        msg['From']=frommail  #发送人
 
        #STMP
        stmp=smtplib.SMTP(stmpserver163,stmpport)  #创建STMP
        stmp.login(frommail,code163)
        # 发送邮件,发送人,收邮件人,发送内容
        stmp.sendmail(frommail,toemails,msg.as_string())
        stmp.close()
        print("email ok")
    except Exception as ex:
        print(ex)

  

 

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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
"""
python 发送邮件,用授权码
https://mailtrap.io/blog/python-send-email/
https://pythonalgos.com/how-to-send-an-email-with-an-attachment-in-python/
https://pythonroadmap.com/blog/guide/send-email-attachments-with-python
python send email attachment
https://pytutorial.com/how-to-send-an-email-with-attachment-in-python/
 
 
"""
import smtplib
from email.mime.text import MIMEText
from email.message import EmailMessage
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
 
 
def sendattachment():
    """
    还附件发送邮件
    :return:
    """
    try:
        sender_email = "geovindu@163.com"
        receiver_email = "463588883@qq.com"
        message = MIMEMultipart()
        message["From"] = sender_email
        message['To'] = receiver_email
        message['Subject'] = "sending mail using python"
        file = "doc.txt"
        attachment = open(file, 'rb')
        obj = MIMEBase('application', 'octet-stream')
        obj.set_payload((attachment).read())
        encoders.encode_base64(obj)
        obj.add_header('Content-Disposition', "attachment; filename= " + file)
        message.attach(obj)
        my_message = message.as_string()
        email_session = smtplib.SMTP('smtp.163.com', 25)
        email_session.starttls()
        email_session.login(sender_email, 'geovindu')
        email_session.sendmail(sender_email, receiver_email, my_message)
        email_session.quit()
        print("YOUR MAIL HAS BEEN SENT SUCCESSFULLY")
        #没有邮件内容,只有附件发送成功
 
        #2
        # Creating the Email Object
        subject='hi,附件'
        message = MIMEMultipart()
        message["From"] = sender_email
        message["To"] = receiver_email
        message["Subject"] = subject
 
        # Attaching the File
        attachment_path = "day7-网络编程.pdf"
 
        # Attach the file using the MIMEBase class
        attachment = open(attachment_path, "rb")
        payload = MIMEBase("application", "octet-stream")
        payload.set_payload((attachment).read())
        encoders.encode_base64(payload)
        payload.add_header(
            "Content-Disposition", f"attachment; filename= {attachment_path.split('/')[-1]}"
        )
        message.attach(payload)
 
        # Establishing the SMTP Connection
        smtp_server = "smtp.163.com"
        smtp_port = 25
 
        with smtplib.SMTP(smtp_server, smtp_port) as server:
            server.starttls()
            server.login(sender_email, "geovindu")
            server.send_message(message)
        #附件未成功
 
        #3
        '''
        import smtplib
        from email.mime.multipart import MIMEMultipart
        from email.mime.text import MIMEText
        from email.mime.base import MIMEBase
        from email import encoders
         
        # Set up email details
        sender_email = "your_email@gmail.com"
        receiver_email = "recipient_email@example.com"
        subject = "Email with Attachments"
        body = "Dear recipient,\n\nPlease find the attached files.\n\nBest regards,\nSender"
     
        # Create the email object
        message = MIMEMultipart()
        message["From"] = sender_email
        message["To"] = receiver_email
        message["Subject"] = subject
     
        # Add the body of the email
        message.attach(MIMEText(body, "plain"))
     
        # Attach the files
        attachment_paths = ["path_to_file/data.csv", "path_to_file/data.xlsx"]
     
        for attachment_path in attachment_paths:
            attachment = open(attachment_path, "rb")
            part = MIMEBase("application", "octet-stream")
            part.set_payload((attachment).read())
            encoders.encode_base64(part)
            part.add_header("Content-Disposition", f"attachment; filename= {attachment_path.split('/')[-1]}")
            message.attach(part)
     
        # Connect to the SMTP server and send the email
        smtp_server = "smtp.gmail.com"
        smtp_port = 587
     
        with smtplib.SMTP(smtp_server, smtp_port) as server:
            server.starttls()
            server.login(sender_email, "your_password")  # Replace with your password or use app-specific passwords
            server.send_message(message)
        '''
    except Exception  as ex:
        print(ex)
 
 
 
 
def send():
    """
     发送邮件
    :return:
    """
    try:
        stmpserver163='smtp.163.com'
        stmpport=25
        frommail='geovindu@163.com'
        code163='YRXSNJGQNPDZTIOE'
 
        toemails=['463588883@qq.com','geovindu@jw28.com']
        #创建邮件:
        msg=MIMEText('<h3>hi, how are you. send mail<h3>','html','utf-8'#邮件内容
        msg['Subject'] = 'ICT 公益培训 考试日期通知'   #主题
        msg['To']=','.join(toemails)  #收件人
        msg['From']=frommail  #发送人
 
        #STMP
        stmp=smtplib.SMTP(stmpserver163,stmpport)  #创建STMP
        stmp.login(frommail,code163)
        stmp.set_debuglevel(1)
        # 发送邮件,发送人,收邮件人,发送内容
        stmp.sendmail(frommail,toemails,msg.as_string())
 
        stmp.quit()
        stmp.close()
        return True
        print("email ok")
    except Exception as ex:
        print(ex)
        return False

  

posted @   ®Geovin Du Dream Park™  阅读(28)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2015-07-23 csharp: 用Enterprise Library对象实体绑定数据
2013-07-23 -ms-zoom property
2013-07-23 jQuery:mouseover and Increase the Size of an Image
2011-07-23 SQL script 會計記賬 Debit-Credit Bookkeeping
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示