#!/usr/bin/env python # encoding: utf-8 # # Copyright (c) 2008 Doug Hellmann All rights reserved. # """ """ __version__ = "$Id$" #end_pymotw_header 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()