forked from rossgoodwin/ficgen
/
sendnovel.py
46 lines (37 loc) · 1.49 KB
/
sendnovel.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
import smtplib
import os
from sys import argv
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
from gmail_password import pw
script, to_address, filePath = argv
def send_mail(send_from, send_to, subject, text, files=[]):
assert isinstance(send_to, list)
assert isinstance(files, list)
msg = MIMEMultipart()
msg['From'] = send_from
msg['To'] = COMMASPACE.join(send_to)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach( MIMEText(text) )
for f in files:
part = MIMEBase('application', "octet-stream")
part.set_payload( open(f,"rb").read() )
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
msg.attach(part)
smtp = smtplib.SMTP('smtp.gmail.com', 587)
smtp.ehlo()
smtp.starttls()
smtp.login("ross.goodwin@gmail.com", pw)
smtp.sendmail(send_from, send_to, msg.as_string())
smtp.quit()
_from = "ross.goodwin@gmail.com"
_to = [to_address, "ross.goodwin@gmail.com"]
_subject = "Your Novel"
_text = "Attached is your novel, lovingly generated by Jane, my computer. (Yes, my computer has a name. Deal with it.)\n\nEnjoy!\n\n-Ross\n\n\n\nhttps://github.com/rossgoodwin/plotgen\nhttp://rossgoodwin.com/ficgen"
_files = [filePath]
send_mail(_from, _to, _subject, _text, files=_files)