Python3 SMTP send mail

In this article, mainly introduce the use of smtplib for text format, HTML format and mail sending processing with attachments.

Python3 SMTP send mail

In this article, mainly introduce the use of smtplib for text format, HTML format and mail sending processing with attachments.

  • Import smtplib module
import smtplib
  • Key function description
# Create smtp object
smtp = smtplib . SMTP ( [ host [ , port [ , localhost ] ] ]  )

# Parameter Description
# Host: smtp service address, for example, 126 mailboxes are: the SMTP .126 . COM
# port: smtp service port
# localhost: If your smtp service is on this machine, you only need to specify localhost


# Send mail function
SMTP . Sendmail ( from_addr , to_addrs , msg [ , mail_options , rcpt_options ] )

# Parameter Description
# from_addr: Email address
# to_addrs: Mail receiving address list
# msg: Email content
# mail_options , rcpt_options optional parameters, don’t need to know for now

Text mail example

The following demonstrates how to send a plain text mail through 126 mailbox.

# - * - coding : utf -8  - * -

__author__ =  'Bitter Leaf' 
import smtplib
 from email . mime . text import MIMEText
 from email . header import Header

if __name__ ==  "__main__" : 
    print ( "Send text mail example" )

    # Mail sender
    sender =  "deep_test@126.com"

    # Mail receiving address list
    # Please change xxx to your 126 mailbox name or change the whole to your target receiving email address
    receivers =  "xxx@126.com"

    # Send content build
    # textIdentifies the content to be sent in text format
    msg =  MIMEText ( " WeChat Official Account : Open Source Excellent Test" ,  "plain" ,  "utf-8" ) 
    msg [ "From" ]  =  "deep_test@126.com" 
    msg [ "To" ]  = receivers

    # Build the mail header
    msg [ "Subject" ]  =  Header ( "Open Source Excellent Test_DeepTest" ,  "utf-8" )

    # smtp service
    smtpserver =  "smtp.126.com" 
    smtpport =  25

    # The sender's email username or the username dedicated to smtp account
    username =  "deep_test"

    # The sender's email password or the password dedicated to the smtp account
    password =  "123456a"

    # Build smtp object
    smtp = smtplib . SMTP ( )

    # Connect to smtp service
    con = smtp . connect ( smtpserver , smtpport )     
    print ( "Connection result: " , con )

    # Log in to smtp service
    log = smtp . login ( username , password )     
    print ( "Login result:" , log )

    # send email
    print ( receivers ) 
    res = smtp . sendmail ( sender , receivers , msg . as_string ( ) )     
    print ( "Mail sending result: " , res )

    # drop out
    smtp . quit ( )     
    print ( "send email finish" )

Run the above code, and your target mailbox will receive a test email.

Note: It’s best to change to your own email smtp account, after some time I will turn off my smtp account activation service

HTML format mail

Next we try to send an email in html format.

Replace the following parts in the text format mail code as follows:

# Send content build

# html identifies the content to be sent in text format
msg =  MIMEText ( "

WeChat Official Account : Open Source Excellent Test

Open Source Excellent Test Community>" , 
         "html" ,  
         "utf-8" )

The complete code example is as follows:

# - * - coding : utf -8  - * -

__author__ =  'Bitter leaves'

import smtplib

from email . mime . text import MIMEText
 from email . header import Header

if __name__ ==  "__main__" :     
    print ( "Example of sending HTML mail" )

    # Mail sender
    sender =  "deep_test@126.com"

    # Mail receiving address list
    # Please change xxx to your 126 mailbox name or change the whole to your target receiving email address
    receivers =  "xxx@126.com"

    # Send content build
    # html identifies the content to be sent in text format
    msg =  MIMEText ( "

WeChat Official Account : Open Source Excellent Test

Open Source Excellent Test Community>" ,  
        "html" ,  
        "utf-8" ) 
    msg [ "From" ]  =  "deep_test@126.com" 
    msg [ "To" ]  = receivers

    # Build the mail header
    msg [ "Subject" ]  =  Header ( "Open Source Excellent Test_DeepTest" ,  "utf-8" )

    # smtp service
    smtpserver =  "smtp.126.com" 
    smtpport =  25

    # The sender's email username or the username dedicated to smtp account
    username =  "deep_test"

    # The sender's email password or the password dedicated to the smtp account
    password =  "123456a"

    # Build smtp object
    smtp = smtplib . SMTP ( )

    # Connect to smtp service
    con = smtp . connect ( smtpserver , smtpport )     
    print ( "Connection result: " , con )

    # Log in to smtp service
    log = smtp . login ( username , password ) 
    print ( "Login result:" , log )

    # send email
    print ( receivers ) 
    res = smtp . sendmail ( sender , receivers , msg . as_string ( ) )     
    print ( "Mail sending result: " , res )

    # drop out
    smtp . quit ( )     print ( "send email finish" )

Note:

  • Change plain to html to identify the email content as html format
  • The email content is formatted in html language

Attachment format mail

Let's take a look at how to send emails with attachments. Need to import new classes, as follows:

from email . mime . multipart import MIMEMultipart

Need to use MIMEMultipart to build the content structure, the key code is as follows:

# html identifies the content to be sent in text format
msg =  MIMEMultipart ( ) 
msg [ "From" ]  =  "deep_test@126.com" 
msg [ "To" ]  = receivers

# Build the mail header
msg [ "Subject" ]  =  Header ( "Open Source Excellent Test_DeepTest" ,  "utf-8" )

# Build the content of the message body
msg . attach ( MIMEText ( " WeChat Official Account : Open Source Test" ,  "plain" ,  "utf-8" ) ) 
# Construct attachments , the same for multiple attachments
attach1 =  MIMEText ( open ( "Send attached mail.py " ,  'rb' ) . read ( ) ,  "base64" ,  "utf-8" ) 
attach1 [ "Content-Type" ]  =  "application/octet-stream"

# Write the filename here, it will be displayed in the mail
attach1 [ "Content-Disposition" ]  =  "attrachment;filename=code.py"

# Associate the attachment to the email msg . attach ( attach1 )

The complete code example is as follows:

# - * - coding : utf -8  - * -

__author__ =  'Bitter leaves'

import smtplib
 from email . mime . text import MIMEText
 from email . header import Header
 from email . mime . multipart import MIMEMultipart

if __name__ ==  "__main__" :     
    print ( "Example of sending HTML mail" )    
    # Mail sender
    sender =  "deep_test@126.com"

    # Mail receiving address list
    # Please change xxx to your 126 mailbox name or change the whole to your target receiving email address
    receivers =  "xxx@126.com"

    # Send content build
    # html identifies the content to be sent in text format
    msg =  MIMEMultipart ( ) 
    msg [ "From" ]  =  "deep_test@126.com" 
    msg [ "To" ]  = receivers    
    
    # Build the mail header
    msg [ "Subject" ]  =  Header ( "Open Source Excellent Test_DeepTest" ,  "utf-8" )    
    
    # Build the content of the message body
    msg . attach ( MIMEText ( " WeChat Official Account : Open Source Test" ,  "plain" ,  "utf-8" ) )    

    # Construct attachments , the same is true for multiple attachments
    attach1 =  MIMEText ( open ( "Send attached mail.py " ,  'rb' ) . read ( ) ,  "base64" ,  "utf-8" ) 
    attach1 [ "Content-Type" ]  =  "application/octet-stream"

    # Write the filename here, it will be displayed in the mail
    attach1 [ "Content-Disposition" ]  =  "attrachment;filename=code.py"

    # Associate attachments to emails
    msg . attach ( attach1 )    
    
    # smtp service
    smtpserver =  "smtp.126.com" 
    smtpport =  25

    # The sender's email username or the username dedicated to smtp account
    username =  "deep_test"

    # The sender's email password or the password dedicated to the smtp account
    password =  "123456a"

    # Build smtp object
    smtp = smtplib . SMTP ( )    
    
    # Connect to smtp service
    con = smtp . connect ( smtpserver , smtpport )     
    print ( "Connection result: " , con )    
    
    # Log in to smtp service
    log = smtp . login ( username , password )    
    
    print ( "Login result:" , log )    
    
    # send email
    print ( receivers ) 
    res = smtp . sendmail ( sender , receivers , msg . as_string ( ) )    
    
    print ( "Email sending result: " , res )    
    
    # drop out
    smtp . quit ( )    
    
    print ( "send email finish" )

summary

  • To construct text and html format mails, use MIMEText to construct, use plain to identify the text content format, and html to identify the html content format
  • For the attachment format, you need to use MIMEMultipart

What's Your Reaction?

like
0
dislike
0
love
0
funny
0
angry
0
sad
0
wow
0