Python USES smtp and pop to send and receive mail

  • 2020-06-23 01:06:41
  • OfStack

SMTP

SMTP is the email protocol. Python has built-in support for SMTP to send plain text, HTML and email with attachments.

Python supports SMTP with two modules, smtplib and email. email constructs mail and smtplib sends mail.

pop

To receive mail is to write an MUA as a client to get mail from MDA to the user's computer or mobile phone. The most commonly used protocol for receiving mail is POP protocol. The current version number is 3, commonly known as POP3.

Python has a built-in poplib module, which implements POP3 protocol and can be used to receive mail directly.

Note that the POP3 protocol does not receive a readable message itself, but rather the original text of the message, much like the SMTP protocol, which sends one encoded block of text.

To turn the text from POP3 into a readable message, you need to parse the original text into a readable message object using the various classes provided by the email module.

So, there are two steps to getting email:

Step 1: Use poplib to download the original text of the message locally.

Part 2: Parsing the original text with email and restoring it to a mail object.

email System Components:

MTA message transfer agent, responsible for routing, queuing and sending messages

SMTP Simple Mail Transfer Protocol

Connect to the server

2 landing

Issue a service request

4 out of

POP: Post office protocol

The purpose of the RFC918" post office protocol is to allow a user's workstation to access mail from a mailbox server.

The mail should be able to be sent from the workstation to the mail server via the simple Mail transfer protocol SMTP"

Use of POP:

Connect to the server

2 landing

Issue a service request

4 out of


#coding:utf8 
#python2.7 mailtest.py 
''''' 
 use smtp and pop3  Protocol to send and receive qq Email experiment  
 The user name and password need to be filled out by yourself  
''' 
 
from smtplib import SMTP 
from smtplib import SMTPRecipientsRefused 
from poplib import POP3 
from time import sleep 
import sys 
 
smtpserver = 'smtp.qq.com' 
pop3server = 'pop.qq.com' 
emailaddr = '847915049@qq.com' 
username = 'XXX' 
password = 'XXX' 
 
# Combined message format  
origHeaders = ['From: 847915049@qq.com', 
 'To: 847915049@qq.com', 
 'Subject: test msg'] 
origBody = ['nihao ','yaan','sichuan'] 
origMsg = '\r\n\r\n'.join(['\r\n'.join(origHeaders),'\r\n'.join(origBody)]) 
 
# Send mail section  
sendSer = SMTP(smtpserver) 
sendSer.set_debuglevel(1) 
print sendSer.ehlo()[0] # Server properties, etc.  
sendSer.login(username,password) #qq The mailbox needs to be verified.  
try: 
 errs = sendSer.sendmail(emailaddr,emailaddr,origMsg) 
except SMTPRecipientsRefused: 
 print 'server refused....' 
 sys.exit(1) 
sendSer.quit() 
assert len(errs) == 0,errs 
 
 
print '\n\n\nsend a mail ....OK!' 
sleep(10) # Waiting for the 10 seconds  
print 'Now get the mail .....\n\n\n' 
 
 
 
# Start receiving mail  
revcSer = POP3(pop3server) 
revcSer.user(username) 
revcSer.pass_(password) 
 
rsp,msg,siz = revcSer.retr(revcSer.stat()[0]) 
sep = msg.index('') 
if msg: 
 for i in msg: 
  print i 
revcBody = msg[sep+1:] 
assert origBody == revcBody 
print 'successful get ....' 

Results:


send: 'ehlo [169.254.114.107]\r\n' 
reply: '250-smtp.qq.com\r\n' 
reply: '250-PIPELINING\r\n' 
reply: '250-SIZE 52428800\r\n' 
reply: '250-AUTH LOGIN PLAIN\r\n' 
reply: '250-AUTH=LOGIN\r\n' 
reply: '250-MAILCOMPRESS\r\n' 
reply: '250 8BITMIME\r\n' 
reply: retcode (250); Msg: smtp.qq.com 
PIPELINING 
SIZE 52428800 
AUTH LOGIN PLAIN 
AUTH=LOGIN 
MAILCOMPRESS 
8BITMIME 
250 
send: 'AUTH PLAIN ADg0NzkxNTA0OQA0OTMzODQ4MTIzNA==\r\n' 
reply: '235 Authentication successful\r\n' 
reply: retcode (235); Msg: Authentication successful 
send: 'mail FROM:<847915049@qq.com> size=88\r\n' 
reply: '250 Ok\r\n' 
reply: retcode (250); Msg: Ok 
send: 'rcpt TO:<847915049@qq.com>\r\n' 
reply: '250 Ok\r\n' 
reply: retcode (250); Msg: Ok 
send: 'data\r\n' 
reply: '354 End data with <CR><LF>.<CR><LF>\r\n' 
reply: retcode (354); Msg: End data with <CR><LF>.<CR><LF> 
data: (354, 'End data with <CR><LF>.<CR><LF>') 
send: 'From: 847915049@qq.com\r\nTo: 847915049@qq.com\r\nSubject: test msg\r\n\r\nnihao \r\nyaan\r\nsichuan\r\n.\r\n' 
reply: '250 Ok: queued as \r\n' 
reply: retcode (250); Msg: Ok: queued as 
data: (250, 'Ok: queued as') 
send: 'quit\r\n' 
reply: '221 Bye\r\n' 
reply: retcode (221); Msg: Bye 
 
 
 
send a mail ....OK! 
Now get the mail ..... 
 
 
 
Date: Mon, 22 Apr 2013 16:22:01 +0800 
X-QQ-mid: esmtp26t1366618921t440t12695 
Received: from [169.254.114.107] (unknown [120.210.224.173]) 
 by esmtp4.qq.com (ESMTP) with SMTP id 0 
 for <847915049@qq.com>; Mon, 22 Apr 2013 16:22:01 +0800 (CST) 
X-QQ-SSF: B101000000000050321003000000000 
From: 847915049@qq.com 
To: 847915049@qq.com 
Subject: test msg 
 
nihao 
yaan 
sichuan 
successful get .... 

conclusion

That's all for this article about the complete example of Python using smtp and pop to send and receive simple emails. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!


Related articles: