Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #login: wnownowno2@wp.pl
- #haslo: laborkawno2
- import imaplib
- import email
- import smtplib
- import os
- from email.mime.image import MIMEImage
- from email.mime.multipart import MIMEMultipart
- from PIL import Image
- class MailMan:
- def __init__(self,server,login,password):
- self.login = login#"wno2lab4@gmail.com"
- self.password = password#"LABLABLAB4"
- self.server = server#"imap.gmail.com"
- def sendMail(self,subject, contents):
- msg = "From: {}\nTo: {}\nSubject: {}\n{}"\
- .format(self.login, self.login, subject, contents)
- s = self.smtpConnect()
- s.sendmail(self.login,self.login,msg)
- print("Mail sent")
- s.quit()
- def smtpConnect(self):
- print("Connecting to ", self.server)
- s = smtplib.SMTP(self.server, 587)
- s.ehlo()
- s.starttls()
- print("TLS started")
- s.login(self.login, self.password)
- print("Logged in")
- return s
- def sendImage(self,subject, contents, iname):
- s = self.smtpConnect()
- img_data = open(iname,'rb').read()
- msg = MIMEMultipart()
- msg['Subject'] = subject
- msg['From'] = self.login
- msg['To'] = self.login
- #text = MIMEText(contents)
- #msg.attach(text)
- image = MIMEImage(img_data, name=os.path.basename(iname))
- msg.attach(image)
- s.sendmail(self.login,self.login,msg.as_string())
- s.quit()
- pass
- def processPart(self,part):
- if part.get_content_maintype() == 'multipart':
- print(part.as_string())
- pass
- if part.get('Content-Disposition') is None:
- return
- fileName = part.get_filename()
- dlName = "dl_" + fileName
- print("Opening image")
- filePath = os.path.join('.', 'attachments', fileName)
- # if not os.path.isfile(filePath):
- print("Downloading file: ", fileName)
- fp = open(dlName, 'wb')
- fp.write(part.get_payload(decode=True))
- fp.close()
- im = Image.open(dlName)
- im.show()
- def processEmail(self,msg):
- print("Do: ", msg['To'])
- print("Od: ", email.utils.parseaddr(msg['From']))
- print("Temat: ", msg['Subject'])
- print(msg.items())
- for part in msg.walk():
- self.processPart(part)
- def queryMail(self):
- M = imaplib.IMAP4_SSL(self.server)
- response = M.login(self.login, self.password)
- print(response)
- print("Logged in")
- typ, list = M.list()
- print(typ)
- print(list)
- print(M.select("INBOX"))
- result, data = M.search(None, 'ALL')
- for id in data[0].split():
- mailres, maildata = M.fetch(id, "(RFC822)")
- msg = email.message_from_string(maildata[0][1].decode())
- self.processEmail(msg)
- M.logout()
- login = "wnownowno2@gmail.com"
- password = "laborkawno2"
- server = "imap.gmail.com"
- mailman = MailMan(server=server,login=login,password=password)
- mailman.sendMail("Email", "Hello!!!")
- mailman.sendImage("Email - sending image","Hello Image!","duck.bmp")
- mailman.queryMail()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement