forked from alanhou/python-scripting
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_attachment.py
More file actions
35 lines (30 loc) · 906 Bytes
/
Copy pathadd_attachment.py
File metadata and controls
35 lines (30 loc) · 906 Bytes
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
import os, smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
import getpass
# host_name = 'smtp.gmail.com'
host_name = 'smtp.exmail.qq.com'
# host_name = 'smtp.163.com'
port = 465
sender = '发件人 email'
receiver = '收件人 email'
password = getpass.getpass()
text = MIMEMultipart()
text['Subject'] = 'Test Attachment'
text['From'] = sender
text['To'] = receiver
txt = MIMEText('Sending a sample image.')
text.attach(txt)
f_path = '文件路径'
with open(f_path, 'rb') as f:
img = MIMEImage(f.read())
img.add_header('Content-Disposition',
'attachment',
filename=os.path.basename(f_path))
text.attach(img)
s = smtplib.SMTP_SSL(host_name, port)
print("Attachment sent successfully !!")
s.login(sender, password)
s.sendmail(sender, receiver, text.as_string())
s.quit()