SMTP
Sending an Email Message:
import smtplib
import email.utils
from email.mime.text import MIMEText
# Create the message
msg = MIMEText('This is the body of the message.')
msg['To'] = email.utils.formataddr(('Recipient', '[email protected]'))
msg['From'] = email.utils.formataddr(('Author', '[email protected]'))
msg['Subject'] = 'Simple test message'
server = smtplib.SMTP('mail')
server.set_debuglevel(True) # show communication with the server
try:
server.sendmail('[email protected]', ['[email protected]'], msg.as_string())
finally:
server.quit()
Authentication and Encryption:
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', '[email protected]'))
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('[email protected]', [to_email], msg.as_string())
finally:
server.quit()