Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #wno2l3szymon123@gmail.com szymon123
- import smtplib
- import imaplib
- from email.mime.text import MIMEText
- from email.mime.image import MIMEImage
- from email.mime.multipart import MIMEMultipart
- import os
- import email
- class Email(object):
- mail = imaplib.IMAP4_SSL('imap.gmail.com')
- def __init__(self):
- self.mail.login('wno2l3szymon123@gmail.com', 'szymon123')
- self.mail.list()
- self.mail.select("inbox") # connect to inbox.
- # Out: list of "folders" aka labels in gmail.
- def sendEmail(self):
- fromaddr = 'wno2l3szymon123@gmail.com'
- toaddrs = 'wno2l3szymon123@gmail.com'
- msg = "\r\n".join([
- "From: wno2l3szymon123@gmail.com",
- "To: wno2l3szymon123@gmail.com",
- "Subject: WNO czwartek",
- "",
- "Wiadomosc"
- ])
- username = fromaddr
- password = 'szymon123'
- server = smtplib.SMTP('smtp.gmail.com:587')
- server.ehlo()
- server.starttls()
- server.login(username, password)
- server.sendmail(fromaddr, toaddrs, msg)
- server.quit()
- def sendEmailWithAttachment(self):
- img_data = open('canny2.bmp', 'rb').read()
- msg = MIMEMultipart()
- msg['Subject'] = 'subject'
- msg['From'] = 'wno2l3szymon123@gmail.com'
- msg['To'] = 'wno2l3szymon123@gmail.com'
- text = MIMEText("mail.txt")
- msg.attach(text)
- image = MIMEImage(img_data, name=os.path.basename('canny2.bmp'))
- msg.attach(image)
- s = smtplib.SMTP('smtp.gmail.com:587')
- s.ehlo()
- s.starttls()
- s.ehlo()
- s.login('wno2l3szymon123@gmail.com', 'szymon123')
- s.sendmail('wno2l3szymon123@gmail.com', 'wno2l3szymon123@gmail.com', msg.as_string())
- s.quit()
- def getLastMail(self):
- result, data = self.mail.search(None, "ALL")
- ids = data[0]
- id_list = ids.split()
- latest_email_id = id_list[-1]
- result, data = self.mail.fetch(latest_email_id, "(RFC822)")
- return data[0][1]
- def save_attachment(self, msg, download_folder="/tmp"):
- att_path = "No attachment found."
- for part in msg.walk():
- if part.get_content_maintype() == 'multipart':
- continue
- if part.get('Content-Disposition') is None:
- continue
- filename = part.get_filename()
- att_path = os.path.join(download_folder, filename)
- if not os.path.isfile(att_path):
- fp = open(att_path, 'wb')
- fp.write(part.get_payload(decode=True))
- fp.close()
- return att_path
- def get_attachments(self):
- self.imap.select()
- typ, data = self.imap.uid('SEARCH', 'ALL')
- msgs = data[0].split()
- print
- ("Found {0} msgs".format(len(msgs)))
- for uid in msgs:
- typ, s = self.imap.uid('FETCH', uid, '(RFC822)')
- mail = email.message_from_string(s[0][1])
- print
- ("From: {0}, Subject: {1}, Date: {2}\n".format(mail["From"], mail["Subject"], mail["Date"]))
- if mail.is_multipart():
- print
- ('multipart')
- for part in mail.walk():
- ctype = part.get_content_type()
- if ctype in ['image/jpeg', 'image/png']:
- open(part.get_filename(), 'wb').write(part.get_payload(decode=True))
- def getMultipleMails(self):
- result, data = self.mail.search(None, "ALL")
- ids = data[0]
- id_list = ids.split()
- latest_email_id = id_list[-1]
- for i in range(-1, 100):
- try:
- latest_email_id = id_list[i]
- result, data = self.mail.fetch(latest_email_id, "(RFC822)")
- print(data[0][1])
- except:
- return None
- def main():
- gmailObject = Email()
- #gmailObject.sendEmailWithAttachment()
- #gmailObject.sendEmail()
- #print(gmailObject.getLastMail())
- #gmailObject.get_attachments()
- (gmailObject.getMultipleMails())
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement