SMTP and POP3 Example : SMTP « Network « Python Tutorial






from smtplib import SMTP
from poplib import POP3
from time import sleep

SMTPSVR = 'smtp.python.is.cool'
POP3SVR = 'pop.python.is.cool'

origHdrs = ['From: from@python.com',
     'To: to@python.com',
     'Subject: test msg']
origBody = ['xxx', 'yyy', 'zzz']
origMsg = '\r\n\r\n'.join(['\r\n'.join(origHdrs),'\r\n'.join(origBody)])

sendSvr = SMTP(SMTPSVR)
errs = sendSvr.sendmail('from@python.com',('to@python.com',), origMsg)
sendSvr.quit()
assert len(errs) == 0, errs
sleep(10)

recvSvr = POP3(POP3SVR)
recvSvr.user('wesley')
recvSvr.pass_('youllNeverGuess')
rsp, msg, siz = recvSvr.retr(recvSvr.stat()[0])

sep = msg.index('')
recvBody = msg[sep+1:]
assert origBody == recvBody








21.11.SMTP
21.11.1.Sending Email Using SMTP
21.11.2.Retrieving Email from a POP3 Server
21.11.3.SMTP and POP3 Example
21.11.4.SMTP transmission with authentication
21.11.5.Basic SMTP transmission
21.11.6.SMTP transmission with debugging
21.11.7.POP mailbox downloader
21.11.8.POP mailbox scanning
21.11.9.POP connection and authentication
21.11.10.POP connection and authentication with APOP