SoFunction
Updated on 2024-11-10

A simple tutorial on sending SMTP emails in Python.

I. Two modules

Python uses SMTP to send mail two modules: smtplib module, email module.

  • smtplib: responsible for sending mail
  • email: responsible for building emails

II. SMTP ports

1) Unencrypted Port, Interface, Port: 25
2) Use SSL encryption, smtplib.SMTP_SSL interface, port: 465
3) Use TLS encryption, port: 587

III. Four major steps

1、Construct the content of the mail

# Plain text
msg = MIMEText(content)
 
# Annexes
msg = MIMEMultipart()

2、Connect to mail server

s = ("", 25)

3、Login mail server

(msg_from, passwd) 

msg_from: refers to the sender's mailbox

passwd: refers to the sender's password, this password is not your QQ login password, but an authorization code after you open SMTP in QQ mailbox setting

4、Send mail

(msg_from, msg_to, msg.as_string())

msg_from: sender
msg_to: the recipient of the message
msg.as_string(): the message to send

IV. Common scenarios

1. Plain text mail

import smtplib
from  import MIMEText
from  import Header
 
 
# Sender
msg_from = "xxxxx@"
 
# The password here is not the password of QQ mailbox, but the authorization code after enabling the SMTP server in the settings.
passwd = "xxxxx"
 
# Recipient
msg_to = "xxxx@"
 
# Text of the message
content = 'Python mail delivery test...'
 
# Subject of the e-mail
subject = "test"
 
# Generate a MIMEText object (and some other parameters)
msg = MIMEText(content)
 
# Put in the subject of the email
msg['Subject'] = Header(subject, 'utf-8')
 
# Put in sender
msg['From'] = msg_from
 
try:
    # Connect to the mail server
    s = ("", 25)
 
    # Login to Mailbox
    (msg_from, passwd)
 
    # Send mail: sender, recipient, message to be sent
    (msg_from, msg_to, msg.as_string())
    print('Success')
except  as e:
    print(e)
finally:
    ()

2、Send html text

import smtplib
from  import MIMEText
from  import Header
 
 
# Sender
msg_from = "xxxx@"
 
# The password here is not the password of QQ mailbox, but the authorization code after enabling the SMTP server in the settings.
passwd = "xxxx"
 
# Recipient
msg_to = "xxxx@"
 
# Text of the message
content = """
<p>Python Mail delivery test...</p>
<p><a href="" rel="external nofollow" >Here's a link</a></p>
"""
 
# Subject of the e-mail
subject = "test"
 
# Generate a MIMEText object (
msg = MIMEText(content, 'html', 'utf-8')
 
# Put in the subject of the email
msg['Subject'] = Header(subject, 'utf-8')
 
# Put in sender
msg['From'] = msg_from
 
try:
    # Connect to the mail server
    s = ("", 25)
 
    # Login to Mailbox
    (msg_from, passwd)
 
    # Send mail: sender, recipient, message to be sent
    (msg_from, msg_to, msg.as_string())
    print('Success')
except  as e:
    print(e)
finally:
    ()

3. Sending attachments

import smtplib
from  import MIMEText
from  import MIMEMultipart
from  import Header
 
 
# Sender
msg_from = "xxxx@"
 
# The password here is not the password of QQ mailbox, but the authorization code after enabling the SMTP server in the settings.
passwd = "xxxx"
 
# Recipient
msg_to = "xxxx@"
 
# Subject of the e-mail
subject = "test"
 
# Generate a MIMEMultipart object (
msg = message = MIMEMultipart()
 
# Text of the message
(MIMEText('This is the Python mail delivery test ......', 'plain', 'utf-8'))
 
# Put in the subject of the email
msg['Subject'] = Header(subject, 'utf-8')
 
# Put in sender
msg['From'] = msg_from
 
# Add attachments
att1 = MIMEText(open('./wordcloud_singer.py', 'rb').read(), 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
att1["Content-Disposition"] = 'attachment; filename=""'
(att1)
 
try:
    # Connect to the mail server
    s = ("", 25)
 
    # Login to Mailbox
    (msg_from, passwd)
 
    # Send mail: sender, recipient, message to be sent
    (msg_from, msg_to, msg.as_string())
    print('Success')
except  as e:
    print(e)
finally:
    ()

Above is Python send SMTP mail simple tutorial details, more information about Python send mail please pay attention to my other related articles!