Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- from __future__ import unicode_literals
- import smtplib
- import mimetypes
- from email.mime.multipart import MIMEMultipart
- from email import encoders
- from email.message import Message
- from email.mime.audio import MIMEAudio
- from email.mime.base import MIMEBase
- from email.mime.image import MIMEImage
- from email.mime.text import MIMEText
- import re
- import sys
- from os.path import basename
- #This function seach for a key and replace it with the value
- #I send the same message to 2 person everyday, I want to type it only once and swap the names
- def YF(s):
- d = {'Fred':'Yan',
- 'Yan':'Fred',
- 'FRED':'YAN',
- 'YAN':'FRED',
- 'Yanick':'Frédéric',
- 'Frédéric':'Yanick'}
- pattern = re.compile(r'b(' + '|'.join(d.keys()) + r')b')
- return pattern.sub(lambda x: d[x.group()], s)
- #This function sends an email
- def sendEmail(sendTo, sendFile, sendSubject, sendMessage):
- emailfrom = "myEmail@gmail.com"
- emailto = sendTo
- fileToSend = sendFile
- theSubject = sendSubject
- username = "myUserName"
- password = "myPassword"
- msg = MIMEMultipart()
- msg["From"] = emailfrom
- msg["To"] = emailto
- msg["Subject"] = theSubject
- msg.preamble = theSubject
- ctype, encoding = mimetypes.guess_type(fileToSend)
- if ctype is None or encoding is not None:
- ctype = "application/octet-stream"
- maintype, subtype = ctype.split("/", 1)
- if maintype == "text":
- fp = open(fileToSend)
- # Note: we should handle calculating the charset
- attachment = MIMEText(fp.read(), _subtype=subtype)
- fp.close()
- elif maintype == "image":
- fp = open(fileToSend, "rb")
- attachment = MIMEImage(fp.read(), _subtype=subtype)
- fp.close()
- elif maintype == "audio":
- fp = open(fileToSend, "rb")
- attachment = MIMEAudio(fp.read(), _subtype=subtype)
- fp.close()
- else:
- fp = open(fileToSend, "rb")
- attachment = MIMEBase(maintype, subtype)
- attachment.set_payload(fp.read())
- fp.close()
- encoders.encode_base64(attachment)
- attachment.add_header("Content-Disposition", "attachment", filename=fileToSend) #I DON'T KNOW WHY ALL MY FILES' NAME IS "NONAME"
- #debug... I get the right file name...
- print fileToSend
- msg.attach(attachment)
- msg.attach(MIMEText(sendMessage, 'plain'))
- server = smtplib.SMTP("smtp.gmail.com:587")
- server.starttls()
- server.login(username,password)
- server.sendmail(emailfrom, emailto, msg.as_string())
- server.quit()
- #Here I type the full path to the file I want to send
- yanFile = raw_input("Quel fichier?").decode(sys.stdin.encoding)
- #if the file path has quotes, we remove them
- if yanFile.startswith('"') and yanFile.endswith('"'):
- yanFile = yanFile[1:-1]
- #we get the second file to send by swaping the name in the file name e.g. Yan.doc becomes Fred.doc
- fredFile = YF(yanFile)
- #Get the email's subject
- whatSubject = raw_input("Sujet du courriel?")
- #Get the first email's text
- yanMessage = raw_input("Quel est le message pour YAN?").decode(sys.stdin.encoding)
- #Swap the names in the text for the second email
- fredMessage = YF(yanMessage)
- #debugging
- print yanMessage
- print fredMessage
- print yanFile
- print fredFile
- #first email
- sendEmail("YansEmail@hotmail.com", yanFile, whatSubject, yanMessage)
- #second email
- sendEmail("FredsEmail@gmail.com", fredFile, whatSubject, fredMessage)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement