-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathsend.py
More file actions
59 lines (53 loc) · 1.46 KB
/
send.py
File metadata and controls
59 lines (53 loc) · 1.46 KB
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
# coding: UTF-8
import smtplib
import os
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def send_mail(mail_host, mail_port, mail_from, mail_to, msg):
# connect smtp server, set debug to see reply information
smtp = smtplib.SMTP()
smtp.connect(mail_host, mail_port)
smtp.set_debuglevel(1)
# send mail
smtp.sendmail(mail_from, mail_to, msg.as_string())
# quit
smtp.quit()
def get_massage(mail_from, mail_to):
msg = MIMEMultipart('alternative')
# from, to, subject
msg["From"] = mail_from
msg["To"] = ";".join(mail_to)
msg['Subject'] = r"Oooooooooooooooops"
# html
html = """\
<html>
<head><meta charset="UTF-8"></head>
<body>
<p>Hello, World!</p>
<p>The attach is the script to send this email.</p>
</body>
</html>
"""
content = MIMEText(html, 'html', 'UTF-8')
msg.attach(content)
# attach
dirname = os.path.dirname(__file__)
filename = "send.py"
full_filename = os.path.join(dirname, filename)
attach = MIMEText(open(full_filename, "rb").read(), "base64", "UTF-8")
attach['Content-Type'] = 'application/octet-stream'
attach['Content-Disposition'] = 'attachment; filename=%s' % filename
msg.attach(attach)
# end
return msg
if __name__ == "__main__":
# host:port
mail_host = "xxx.xxx"
mail_port = 25
# mail from and mail to
mail_from = "master@example.com.cn"
mail_to = ["xxx@xxx.xxx"]
# massage
msg = get_massage(mail_from, mail_to)
# send
send_mail(mail_host, mail_port, mail_from, mail_to, msg)