程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python implements sending mail (implements single / group mail verification code)

編輯:Python

Python smtplib course Shows how to use smtplib Modules in Python Send email in . To send an email , We use Python Development server ,Mailtrap Online services and shared network hosted mail servers .

smtplib library

python Sending mail requires smtplib library , Let's have a brief understanding of

SMTP

Simple mail transfer protocol (SMTP) Is a communication protocol for e-mail transmission . Is It's a Internet standard , This standard applies to 1982 Year by year RFC 821 First definition , And in 2008 Year by year RFC 5321 Update to extension SMTP add to . Mail servers and other mail transfer agents use SMTP Send and receive mail .

smtplib It's a Python library , Used to use the Simple Mail Transfer Protocol (SMTP) Send email . smtplib It's a built-in module ; We don't need to install it . It abstracts SMTP All the complexity of .

Mail server

To actually send an email , We need access to the mail server . Python With a simple development mail server . Mailslurper Is an easy-to-use local development server . The shared virtual hosting provider allows us to access the mail server . We can find details in the account .

smtp The basic commands of the protocol include :\

    HELO Identify the user to the server \
    MAIL Initialize mail transfer mail from:\
    RCPT Identify the individual mail recipient ; Often in MAIL Command behind , There can be multiple rcpt to:\
    DATA In one or more RCPT After the command , Indicates that all mail recipients have been identified , And initiate data transfer , With . end \
    VRFY Used to authenticate the specified user / Does the mailbox exist ; For security reasons , The server often forbids this command \
    EXPN Verify that the given mailbox list exists , Expand your mailbox list , It's often banned \
    HELP Query what commands the server supports \
    NOOP No operation , The server should respond to OK\
    QUIT End the conversation \
    RSET Reset session , The current transfer has been cancelled \
    MAIL FROM Specify sender address \
    RCPT TO Indicate the recipient's address 

actual combat

1.126 The mailbox is generally closed by default SMTP service , We have to open it first

2.Python The code is as follows

# smtplib It's used for sending mail
import smtplib
from email.mime.text import MIMEText
# email Used to build mail content
from email.header import Header
# Used to build headers
# Information from the sender : Email address ,126 Email authorization code
from_addr = '[email protected]'
password = 'POP3/SMTP Service authorization password , In the previous step, you can get '
# The recipient's mailbox
to_addr = '[email protected]'
# Sending server
smtp_server = 'smtp.126.com'
""" title """
head=" Email verification code "
""" Text """
text="【TRobot】 Your verification code 32123, This verification code 5 Valid in minutes , Don't let it out to others !"
# Email body content , The first parameter is the content , The second parameter is format (plain For plain text ), The third parameter is encoding
msg = MIMEText(text,'plain','utf-8')
# Header message
msg['From'] = Header(from_addr)
msg['To'] = Header(to_addr)
msg['Subject'] = Header(head)
# Turn on the messaging service , This is encrypted transmission
#server = smtplib.SMTP_SSL()
server=smtplib.SMTP_SSL(smtp_server)
server.connect(smtp_server,465)
# Log in to your email address
server.login(from_addr, password)
# Send E-mail
server.sendmail(from_addr, to_addr, msg.as_string())
# Shut down the server
server.quit()

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved