Guest User

Untitled

a guest
Sep 22nd, 2018
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. import smtplib
  2. import email.utils
  3. from email.mime.text import MIMEText
  4. import getpass
  5.  
  6. # Prompt the user for connection info
  7. to_email = raw_input('Recipient: ')
  8. servername = raw_input('Mail server name: ')
  9. username = raw_input('Mail user name: ')
  10. password = getpass.getpass("%s's password: " % username)
  11.  
  12. # Create the message
  13. msg = MIMEText('Test message from PyMOTW.')
  14. msg.set_unixfrom('author')
  15. msg['To'] = email.utils.formataddr(('Recipient', to_email))
  16. msg['From'] = email.utils.formataddr(('Author', 'author@example.com'))
  17. msg['Subject'] = 'Test from PyMOTW'
  18.  
  19. server = smtplib.SMTP(servername)
  20. try:
  21. server.set_debuglevel(True)
  22.  
  23. # identify ourselves, prompting server for supported features
  24. server.ehlo()
  25.  
  26. # If we can encrypt this session, do it
  27. if server.has_extn('STARTTLS'):
  28. server.starttls()
  29. server.ehlo() # re-identify ourselves over TLS connection
  30.  
  31. server.login(username, password)
  32. server.sendmail('author@example.com', [to_email], msg.as_string())
  33. finally:
  34. server.quit()
Add Comment
Please, Sign In to add comment