Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2015
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import smtplib
  4. import mimetypes
  5. from email.mime.multipart import MIMEMultipart
  6. from email import encoders
  7. from email.message import Message
  8. from email.mime.audio import MIMEAudio
  9. from email.mime.base import MIMEBase
  10. from email.mime.image import MIMEImage
  11. from email.mime.text import MIMEText
  12. import re
  13. import sys
  14. from os.path import basename
  15.  
  16. #This function seach for a key and replace it with the value
  17. #I send the same message to 2 person everyday, I want to type it only once and swap the names
  18. def YF(s):
  19. d = {'Fred':'Yan',
  20. 'Yan':'Fred',
  21. 'FRED':'YAN',
  22. 'YAN':'FRED',
  23. 'Yanick':'Frédéric',
  24. 'Frédéric':'Yanick'}
  25. pattern = re.compile(r'b(' + '|'.join(d.keys()) + r')b')
  26. return pattern.sub(lambda x: d[x.group()], s)
  27.  
  28. #This function sends an email
  29. def sendEmail(sendTo, sendFile, sendSubject, sendMessage):
  30. emailfrom = "myEmail@gmail.com"
  31. emailto = sendTo
  32. fileToSend = sendFile
  33. theSubject = sendSubject
  34. username = "myUserName"
  35. password = "myPassword"
  36.  
  37. msg = MIMEMultipart()
  38. msg["From"] = emailfrom
  39. msg["To"] = emailto
  40. msg["Subject"] = theSubject
  41. msg.preamble = theSubject
  42.  
  43. ctype, encoding = mimetypes.guess_type(fileToSend)
  44. if ctype is None or encoding is not None:
  45. ctype = "application/octet-stream"
  46.  
  47. maintype, subtype = ctype.split("/", 1)
  48.  
  49. if maintype == "text":
  50. fp = open(fileToSend)
  51. # Note: we should handle calculating the charset
  52. attachment = MIMEText(fp.read(), _subtype=subtype)
  53. fp.close()
  54. elif maintype == "image":
  55. fp = open(fileToSend, "rb")
  56. attachment = MIMEImage(fp.read(), _subtype=subtype)
  57. fp.close()
  58. elif maintype == "audio":
  59. fp = open(fileToSend, "rb")
  60. attachment = MIMEAudio(fp.read(), _subtype=subtype)
  61. fp.close()
  62. else:
  63. fp = open(fileToSend, "rb")
  64. attachment = MIMEBase(maintype, subtype)
  65. attachment.set_payload(fp.read())
  66. fp.close()
  67. encoders.encode_base64(attachment)
  68.  
  69. attachment.add_header("Content-Disposition", "attachment", filename=fileToSend) #I DON'T KNOW WHY ALL MY FILES' NAME IS "NONAME"
  70.  
  71. #debug... I get the right file name...
  72. print fileToSend
  73.  
  74. msg.attach(attachment)
  75.  
  76. msg.attach(MIMEText(sendMessage, 'plain'))
  77.  
  78. server = smtplib.SMTP("smtp.gmail.com:587")
  79. server.starttls()
  80. server.login(username,password)
  81. server.sendmail(emailfrom, emailto, msg.as_string())
  82. server.quit()
  83.  
  84. #Here I type the full path to the file I want to send
  85. yanFile = raw_input("Quel fichier?").decode(sys.stdin.encoding)
  86.  
  87. #if the file path has quotes, we remove them
  88. if yanFile.startswith('"') and yanFile.endswith('"'):
  89. yanFile = yanFile[1:-1]
  90.  
  91. #we get the second file to send by swaping the name in the file name e.g. Yan.doc becomes Fred.doc
  92. fredFile = YF(yanFile)
  93.  
  94. #Get the email's subject
  95. whatSubject = raw_input("Sujet du courriel?")
  96.  
  97. #Get the first email's text
  98. yanMessage = raw_input("Quel est le message pour YAN?").decode(sys.stdin.encoding)
  99.  
  100. #Swap the names in the text for the second email
  101. fredMessage = YF(yanMessage)
  102.  
  103. #debugging
  104. print yanMessage
  105. print fredMessage
  106. print yanFile
  107. print fredFile
  108.  
  109. #first email
  110. sendEmail("YansEmail@hotmail.com", yanFile, whatSubject, yanMessage)
  111. #second email
  112. sendEmail("FredsEmail@gmail.com", fredFile, whatSubject, fredMessage)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement