Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import smtplib
- import email.utils
- from email.mime.text import MIMEText
- import getpass
- # Prompt the user for connection info
- to_email = raw_input('Recipient: ')
- servername = raw_input('Mail server name: ')
- username = raw_input('Mail user name: ')
- password = getpass.getpass("%s's password: " % username)
- # Create the message
- msg = MIMEText('Test message from PyMOTW.')
- msg.set_unixfrom('author')
- msg['To'] = email.utils.formataddr(('Recipient', to_email))
- msg['From'] = email.utils.formataddr(('Author', 'author@example.com'))
- msg['Subject'] = 'Test from PyMOTW'
- server = smtplib.SMTP(servername)
- try:
- server.set_debuglevel(True)
- # identify ourselves, prompting server for supported features
- server.ehlo()
- # If we can encrypt this session, do it
- if server.has_extn('STARTTLS'):
- server.starttls()
- server.ehlo() # re-identify ourselves over TLS connection
- server.login(username, password)
- server.sendmail('author@example.com', [to_email], msg.as_string())
- finally:
- server.quit()
Add Comment
Please, Sign In to add comment