The Smtplib Module Defines An SMTP Client Session Object That Can Be Used To Send Mail To Any Internet Machine With An SMTP or ESMTP Listener Daemon
The Smtplib Module Defines An SMTP Client Session Object That Can Be Used To Send Mail To Any Internet Machine With An SMTP or ESMTP Listener Daemon
SMTP stands for Simple Mail Transfer Protocol. The smtplib modules is
useful for communicating with mail servers to send mail.
smtplib Usage
This example is taken from this post at wikibooks.org
import smtplib
server = smtplib.SMTP('smtp.gmail.com', 587)
fromaddr = "[email protected]"
toaddr = "[email protected]"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Python email"
Next, we attach the body of the email to the MIME message:
import smtplib
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login("youremailusername", "password")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
Verify an email address
The SMTP protocol includes a command to ask a server whether an
address is valid. Usually VRFY is disabled to prevent spammers from
finding legitimate email addresses, but if it is enabled you can ask the
server about an address and receive a status code indicating validity along
with the user’s full name.
server = smtplib.SMTP('mail')
server.set_debuglevel(True) # show communication with
the server
try:
dhellmann_result = server.verify('dhellmann')
notthere_result = server.verify('notthere')
finally:
server.quit()