Advertisement
Guest User

Untitled

a guest
Jun 30th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. #! /usr/bin/python
  2. import smtplib
  3. import mimetypes
  4. from email.mime.multipart import MIMEMultipart
  5. from email import encoders
  6. from email.message import Message
  7. from email.mime.audio import MIMEAudio
  8. from email.mime.base import MIMEBase
  9. from email.mime.image import MIMEImage
  10. from email.mime.text import MIMEText
  11.  
  12. def send(fileToSend = "sendBook.py"):
  13.  
  14. emailfrom = "jun.xiong1981@gmail.com"
  15. emailto = "jun.xiong1981@free.kindle.com"
  16. #emailto = "jun.xiong1981_2@kindle.com"
  17.  
  18. username = "jun.xiong1981"
  19. password = "rebornJun@0303"
  20.  
  21. msg = MIMEMultipart()
  22. msg["From"] = emailfrom
  23. msg["To"] = emailto
  24. msg["Subject"] = "Ebook to Kindle"
  25. msg.preamble = "Ebook to Kindle"
  26.  
  27. ctype, encoding = mimetypes.guess_type(fileToSend)
  28. if ctype is None or encoding is not None:
  29. ctype = "application/octet-stream"
  30.  
  31. maintype, subtype = ctype.split("/", 1)
  32.  
  33.  
  34. fp = open(fileToSend, "rb")
  35. attachment = MIMEBase(maintype, subtype)
  36. attachment.set_payload(fp.read())
  37. fp.close()
  38. encoders.encode_base64(attachment)
  39. attachment.add_header("Content-Disposition", "attachment", filename=fileToSend)
  40. msg.attach(attachment)
  41.  
  42. server = smtplib.SMTP("smtp.gmail.com:587")
  43. server.starttls()
  44. server.ehlo()
  45. server.login(username,password)
  46. print 'SendToKindle:', fileToSend
  47. server.sendmail(emailfrom, emailto, msg.as_string())
  48. server.quit()
  49.  
  50. import sys, os
  51. from os import listdir
  52. from os.path import isfile, join
  53. mypath = '/Users/jxiong1/Downloads/'
  54. files = [f for f in listdir(mypath) if isfile(join(mypath, f))]
  55. files = [join(mypath, f) for f in files if f.endswith('.epub') or f.endswith('.mobi') or f.endswith('.txt')]
  56. for f in files:
  57. # print f
  58. file = f
  59. if f.endswith('epub'):
  60. file = f.replace('.epub','.mobi')
  61. cmd = './kindlegen "%s"' % f
  62. # print cmd
  63. os.system(cmd)
  64. os.system('rm "%s"' % f)
  65. if f.endswith('txt'):
  66. file = f.replace('.txt','.mobi')
  67. cmd = './kindlegen "%s"' % f
  68. # print cmd
  69. os.system(cmd)
  70. os.system('rm "%s"' % f)
  71. try:
  72. send(fileToSend=file)
  73. os.system('rm "%s"' % file)
  74. except:
  75. pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement