-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsendmail.py
65 lines (51 loc) · 1.97 KB
/
sendmail.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
#!/usr/bin/env python
# coding=utf-8
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# from email.MIMEText import MIMEText
from email.mime.base import MIMEBase
# from email.MIMEBase import MIMEBase
from email import encoders
import glob
import click
import os
import json
import datetime
# filename = "output_Ctripplus_170614_1827_japan.xlsx"
sendmail_secret = None
with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'secrets.json')) as data_file:
sendmail_secret = (json.load(data_file))['sendmail']
@click.command()
@click.option('--filename', default='output_hotel_ref_*.csv')
@click.option('--title', default='Ctrip_hotel_ref')
# @click.option('--duration', default=3, type=int)
# @click.option('--days', default=1, type=int)
def sendmail(filename, title):
today_date = datetime.datetime.today().date()
fromaddr = "[email protected]"
# toaddr = "[email protected], l"
msg = MIMEMultipart()
msg['From'] = fromaddr
# msg['To'] = toaddr
msg['To'] = ", ".join(recipients)
msg['Subject'] = title + '_' + today_date.strftime('%m%d')
# body = "FYI\n\nThis email is auto generated by [email protected]."
body = "This email is auto generated by [email protected]."
msg.attach(MIMEText(body, 'plain'))
newest = max(glob.iglob(filename), key=os.path.getctime)
attachment = open(newest, "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % newest)
msg.attach(part)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, sendmail_secret['password'])
text = msg.as_string()
server.sendmail(fromaddr, recipients, text)
server.quit()
if __name__ == '__main__':
sendmail()