-
Notifications
You must be signed in to change notification settings - Fork 6
/
mail.py
80 lines (65 loc) · 2.23 KB
/
mail.py
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
from flask import *
from flask_mail import Mail, Message
app = Flask(__name__)
app.config.update(
DEBUG=False,
# EMAIL SETTINGS
MAIL_SERVER='smtp.gmail.com',
MAIL_PORT=465,
MAIL_USE_SSL=True,
MAIL_DEFAULT_SENDER=('admin', 'xxxxxxxx@gmail.com'),
MAIL_MAX_EMAILS=10,
MAIL_USERNAME='xxxxxxxx@gmail.com',
MAIL_PASSWORD='xxxxxxxxxxx'
)
mail = Mail(app)
@app.route("/index")
@app.route("/")
def index():
return render_template('index.html')
@app.route("/api/send_mail", methods=['POST'])
def send_mail():
email = request.form['email'].strip()
subject = 'Hello'
message = '這是 flask-mail example <br> <br>' \
'附上一張圖片 <br> <br>' \
'<b style="color:#FF4E4E" >新垣結衣</b>'
msg = Message(
subject=subject,
recipients=[email],
html=message
)
# msg.body = '純文字'
with app.open_resource("static/images/image.jpg") as fp:
msg.attach("image.jpg", "image/jpg", fp.read())
mail.send(msg)
# return "請到你的信箱收信~ ^0^"
return render_template("thank.html")
@app.route("/Bulk_emails")
def Bulk_emails():
"""批次寄信"""
users = [
{"name": "xxxxxxxxxx", "email": "xxxxxxxxxx"},
{"name": "xxxxxxxxxx", "email": "xxxxxxxxxx"},
{"name": "xxxxxxxxxx", "email": "xxxxxxxxxx"},
{"name": "xxxxxxxxxx", "email": "xxxxxxxxxx"},
{"name": "xxxxxxxxxx", "email": "xxxxxxxxxx"},
{"name": "xxxxxxxxxx", "email": "xxxxxxxxxx"},
]
with mail.connect() as conn:
for user in users:
subject = 'hello, {}'.format(user['name'])
message = '這是 flask-mail example <br> <br>' \
'附上一張圖片 <br> <br>' \
'<b style="color:#FF4E4E" >新垣結衣</b>'
msg = Message(subject=subject,
recipients=[user['email']],
html=message,
)
# msg.body = '純文字'
with app.open_resource("static/image.jpg") as fp:
msg.attach("image.jpg", "image/jpg", fp.read())
conn.send(msg)
return "請到你的信箱收信~ ^0^"
if __name__ == '__main__':
app.run()