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
|
import sys import os import smtplib from email import encoders from email.header import Header from email.mime.text import MIMEText from email.utils import parseaddr, formataddr from email.mime.multipart import MIMEMultipart from email.mime.base import MIMEBase
selected_file = sys.argv[1] basename = os.path.basename(selected_file)
def sendtokindle(): smtp_obj = smtplib.SMTP('#SMTP_SERVER', 587) smtp_obj.starttls() smtp_obj.login('#YOUR_EMAIL_ADDRESS', '#PASSWORD')
to_addr = '#KINDLE_EMAIL_ADDRESS'
def _format_addr(s): name, addr = parseaddr(s) return formataddr((Header(name, 'utf-8').encode(), addr))
msg = MIMEMultipart() msg['From'] = _format_addr(smtp_obj.user) msg['To'] = _format_addr(to_addr) msg['Subject'] = Header('Sent to Kindle', 'utf-8').encode() msg.attach( MIMEText('A Voyage to Kindle Powered by Python.', 'plain', 'utf-8')) with open(sys.argv[1], 'rb') as f: mime = MIMEBase('document', 'mobi', filename=basename) mime.add_header('Content-Disposition', 'attachment', filename=basename) mime.add_header('Content-ID', '<0>') mime.add_header('X-Attachment-Id', '0') mime.set_payload(f.read()) encoders.encode_base64(mime) msg.attach(mime)
smtp_obj.sendmail(smtp_obj.user, to_addr, msg.as_string())
def notify(text, title): os.system(""" osascript -e 'display notification "{}" with title "{}"' """.format(text, title))
def main(): sendtokindle() notify("Sent to Kindle: " + basename, "Powered by Python")
if __name__ == '__main__': main()
|