Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import email
- import smtplib
- import imaplib
- from email.MIMEText import MIMEText
- from email.MIMEMultipart import MIMEMultipart
- # Drop 'GMAIL_' prefix: mail provider may change.
- GMAIL_SMTP = "smtp.gmail.com"
- GMAIL_IMAP = "imap.gmail.com"
- # These are all global vars. If they are really do need to be
- # global, they all should be uppercased.
- # Avoid globals at all costs. See for ex.: https://stackoverflow.com/questions/19158339/why-are-global-variables-evil
- # Length >= 3
- l = 'login@gmail.com'
- # Mixed case
- passwORD = 'qwerty'
- subject = 'Subject'
- recipients = ['vasya@email.com', 'petya@email.com']
- message = 'Message'
- header = None
- # def send(...):
- #send message
- msg = MIMEMultipart()
- # msg.update({
- # 'From': ...,
- # ...: ...})
- msg['From'] = l
- msg['To'] = ', '.join(recipients)
- msg['Subject'] = subject
- msg.attach(MIMEText(message))
- # More descriptive var name. I suggest `conn`. Define port as `SMTPS_PORT = 587`
- ms = smtplib.SMTP(GMAIL_SMTP, 587)
- # identify ourselves to smtp gmail client
- ms.ehlo()
- # secure our email with tls encryption
- ms.starttls()
- # re-identify ourselves as an encrypted connection
- ms.ehlo()
- ms.login(l, passwORD)
- # ms.sendmail(
- # l,
- # ms,
- # msg.as_string())
- # -- or --
- # ms.sendmail(l, ms, msg.as_string)
- ms.sendmail(l,
- ms, msg.as_string())
- ms.quit()
- #send end
- # def read(...):
- # Naming is broken. This is not an email "reception". We (or, rather MTA) receive using SMTP.
- # I believe "read" would sound closer to the point.
- #recieve
- # rename mail -> conn. Why: scope for "mail" is way too broad. Conn is much more precise.
- mail = imaplib.IMAP4_SSL(GMAIL_IMAP)
- mail.login(l, passwORD)
- mail.list()
- # All hardcoded strings must be either defined as "constants" or be passed as an argument.
- mail.select("inbox")
- # This is definitely needs to be passed as an argument.
- criterion = '(HEADER Subject "%s")' % header if header else 'ALL'
- result, data = mail.uid('search', None, criterion)
- # Assertion errors are to dirty. Not ok for prod environment. `raise` is a preferrable way of signalling an error condition.
- assert data[0], 'There are no letters with current header'
- latest_email_uid = data[0].split()[-1]
- # The last arg should be in func params.
- result, data = mail.uid('fetch', latest_email_uid, '(RFC822)')
- raw_email = data[0][1]
- email_message = email.message_from_string(raw_email)
- mail.logout()
- #end recieve
- # No print, no file was written, where does all the output goes to?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement