Python Note 3-mail send (smtplib)

Source: Internet
Author: User
Tags base64

Objective

This article summarizes the QQ mailbox and 163 e-mail messages, the mail contains HTML Chinese and attachments, can be sent to multiple recipients, all kinds of non-tyranny, anyway, after reading this article hemp no longer need not worry about my mail received.

The following code is compatible with Python2 and Python3, running without exception, feel free to eat boldly.

One, 163 mailbox

1. Import smtplib library to send mail, import mimetext Library to make plain text mail template

3. First to prepare several email related parameters, each mailbox of the outgoing server is not the same, take 163 as an example, Baidu search to send a server: smtp.163.com

4. The next step is to write the subject and text of the message, the text here in HTML format

5. Last call to the outgoing sender service

6. Reference Code:

# Coding:utf-8
Import Smtplib
From Email.mime.text import Mimetext

#----------1. Parameters related to the sender------
SmtpServer = "smtp.163.com" # Outgoing server
Port = 0 # ports
sender = "[Email protected]" # Account
PSW = "**************" # Password
Receiver = "[email protected]" # Receiver


#----------2. Edit the contents of the message------
Subject = "This is Theme 163"
BODY = ' <p> This is sending 163 mail </p> ' # defines the message body as HTML format
msg = Mimetext (body, "html", "Utf-8")
msg[' from ' = Sender
msg[' to ' = "[email protected]"
msg[' subject '] = subject

#----------3. Send mail------
SMTP = Smtplib. SMTP ()
Smtp.connect (smtpserver) # Connection Server
Smtp.login (sender, PSW) # Login
Smtp.sendmail (sender, receiver, msg.as_string ()) # Send
Smtp.quit () # Close

Second, QQ Mail

1.QQ mailbox is required SSL authentication, this mailbox with the above is a little different

2. Find QQ Email Authorization code, open QQ mailbox-set-account-POP3 open service-Open

(If it is already open, do not know the authorization code, just click on the "Generate authorization code" inside the hint)

3. Send verification SMS to get authorization code, follow the prompts to send a text message, how to point I have been sent, you will receive the authorization code

4. After receiving the authorization code copy, save it, this can be the password of the QQ mailbox

5.QQ email message code, with 163 is a little different, such as the red box:

6. Reference Code:

# Coding:utf-8
Import Smtplib
From Email.mime.text import Mimetext

#----------1. Parameters related to the sender------
# smtpserver = "smtp.163.com" # Outgoing server
SmtpServer = "Smtp.qq.com"
Port = 465 # Ports
Sender = "283340479@qq.com" # account number
PSW = "**************" # Password
Receiver = "283340479@qq.com" # Receiver

#----------2. Edit the contents of the message------
Subject = "This is the theme of QQ"
BODY = ' <p> This is sent by QQ Mail </p> ' # defines the message body as HTML format
msg = Mimetext (body, "html", "Utf-8")
msg[' from ' = Sender
Msg[' to '] = "283340479@qq.com"
msg[' subject '] = subject

#----------3. Send mail------
# SMTP = Smtplib. SMTP ()
# Smtp.connect (smtpserver) # Connection Server
SMTP = Smtplib. Smtp_ssl (SmtpServer, Port)
Smtp.login (sender, PSW) # Login
Smtp.sendmail (sender, receiver, msg.as_string ()) # Send
Smtp.quit () # Close

Third, compatible with 163 and QQ mailbox

1. If you want to be compatible with the above two ways to send mail, just change the third piece of content slightly, as shown below

Four, send with accessories

1. The above mimetext can only send the body, can not bring attachments, send with attachments need to import another module Mimemultipart

2. First read the contents of the file to be sent, File_path is the parameter name of the path

3. Red box file_name parameter is the sent attachment rename

4. Reference code:

# Coding:utf-8
Import Smtplib
From Email.mime.text import Mimetext
From Email.mime.multipart import Mimemultipart

#----------1. Parameters related to the sender------

SmtpServer = "smtp.163.com" # Outgoing server
Port = 0 # ports
Sender = "Yoyo@163.com" # account number
PSW = "***********" # Password
Receiver = "283340479@qq.com" # Receiver

#----------2. Edit the contents of the message------
# Read files
File_path = "Result.html"
With open (File_path, "RB") as FP:
Mail_body = Fp.read ()

msg = Mimemultipart ()
Msg["from" = Sender # Sender
msg["to" = Receiver # Recipient
msg["subject"] = "This my theme" # theme

# body
BODY = Mimetext (mail_body, "html", "Utf-8")
Msg.attach (body)

# Accessories
att = mimetext (mail_body, "base64", "Utf-8")
att["Content-type"] = "Application/octet-stream"
att["content-disposition"] = ' attachment; Filename= "Test_report.html"
Msg.attach (ATT)

#----------3. Send mail------
Try
SMTP = Smtplib. SMTP ()
Smtp.connect (smtpserver) # Connection Server
Smtp.login (sender, PSW)
Except
SMTP = Smtplib. Smtp_ssl (SmtpServer, Port)
Smtp.login (sender, PSW) # Login
Smtp.sendmail (sender, receiver, msg.as_string ()) # Send
Smtp.quit ()

5. The final result, there is a picture of the truth

Five, send to multiple recipients

1. The above is sent to a recipient, so how to send to multiple recipients at once? Just change the two small places.

2. Change the receiver parameter to a list object, a single number can be received

3.msg["to"] This parameter can not be used in the list, you have to first convert the receiver parameters into a string, as shown in

4. Reference code:

# Coding:utf-8
Import Smtplib
From Email.mime.text import Mimetext
From Email.mime.multipart import Mimemultipart

#----------1. Parameters related to the sender------

SmtpServer = "smtp.163.com" # Outgoing server
Port = 0 # ports
Sender = "Yoyo@163.com" # account number
PSW = "*********" # Password
# receiver = ["xxxx@qq.com"] # A single recipient can also be a list
Receiver = ["xxxx@qq.com", "yoyo@qq.com"] # Multiple Recipients List Object

#----------2. Edit the contents of the message------
# Read files
File_path = "Result.html"
With open (File_path, "RB") as FP:
Mail_body = Fp.read ()

msg = Mimemultipart ()
Msg["from" = Sender # Sender
Msg["to"] = ";". Join (Receiver) # Multiple recipients List to STR
msg["subject"] = "This my theme 999" # Theme

# body
BODY = Mimetext (mail_body, "html", "Utf-8")
Msg.attach (body)

# Accessories
att = mimetext (mail_body, "base64", "Utf-8")
att["Content-type"] = "Application/octet-stream"
att["content-disposition"] = ' attachment; Filename= "Test_report.html"
Msg.attach (ATT)

#----------3. Send mail------
Try
SMTP = Smtplib. SMTP ()
Smtp.connect (smtpserver) # Connection Server
Smtp.login (sender, PSW)
Except
SMTP = Smtplib. Smtp_ssl (SmtpServer, Port)
Smtp.login (sender, PSW) # Login
Smtp.sendmail (sender, receiver, msg.as_string ()) # Send
Smtp.quit () # Close

Six: The mail does not receive several reasons:

1.Subject and text content do not use Hello, hehe, test and other words

2.from (Sender) and to (recipient) do not empty,

(otherwise it would be considered spam)

3. If you can't find it, check out the Junk mail box and go to the dumpster.

4. If the previous several times can be received, and then not receive, you need to change the subject content

(Because each time is a subject, the system will also be rejected, the subject content is set to dynamic is the best)

5. Some mailboxes are SSL encrypted, so cannot send, such as: QQ mailbox

(Use authorization code to log in)

6. If you follow the above steps to correct the error, the code is wrong, check the number of times.

(The above code has been tested on both Python2 and Python3)

Python Note 3-mail send (smtplib)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.