SHARE
TWEET

Untitled

a guest Dec 31st, 2018 126 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # --------------------------------------------------
  2. import smtplib
  3.  
  4. class GMailServer:
  5.   def __init__(self, username, password):
  6.         self.server = smtplib.SMTP('smtp.gmail.com:587')  
  7.         self.server.starttls()  
  8.         self.server.login(username,password)
  9.        
  10.     def __enter__(self):
  11.         return self.server
  12.        
  13.     def __exit__(self, type, value, traceback):
  14.         self.server.quit()
  15.  
  16. # --------------------------------------------------
  17. from email.mime.text import MIMEText
  18.  
  19. def SendMail(server, fromaddr, toaddr, subject, body):
  20.  
  21.     msg = MIMEText(body)
  22.  
  23.     msg['Subject'] = subject
  24.     msg['From'] = fromaddr
  25.     msg['To'] = toaddr
  26.  
  27.     server.sendmail(fromaddr, [toaddr], msg.as_string())
  28.  
  29. # --------------------------------------------------
  30. '''
  31.  
  32. fromaddr = 'fromaddr@gmail.com'  
  33. toaddrs  = 'toaddrs@gmail.com'  
  34. subject  = 'subject'
  35. body = 'body text'    
  36.  
  37. # Credentials  
  38. username = 'fromaddr@gmail.com'  
  39. password = 'password'
  40.        
  41. with GMailServer(username, password) as server:
  42.     SendMail(server, fromaddr, toaddrs, subject, body)
  43.  
  44. '''
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top