All Products
Search
Document Center

Direct Mail:SMTP call example for Python 2

Last Updated:Oct 29, 2025

This topic provides an example of an SMTP call in Python 2.

Send emails using Python over the SMTP protocol

# -*- coding:utf-8 -*-
import smtplib
import email
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.base import MIMEBase
from email.mime.application import MIMEApplication
from email.header import Header

# The sender address. This address is created in the console.
username = '***'

# The sender password. This password is created in the console.
password = '***'

# The custom reply-to address.
replyto = '***'

# The recipient address or a list of recipient addresses. Multiple recipients are supported.
# For the specific number of recipients allowed, see the product specifications.
#receivers = ['address1@example.com', 'address2@example.com']
#rcptto = ','.join(rcptto)
rcptto = '***'

# Build the alternative structure.
msg = MIMEMultipart('alternative')
msg['Subject'] = Header('Custom email subject'.decode('utf-8')).encode()
msg['From'] = '%s <%s>' % (Header('Custom sender nickname'.decode('utf-8')).encode(), username)
msg['To'] = rcptto
msg['Reply-to'] = replyto
msg['Message-id'] = email.utils.make_msgid()
msg['Date'] = email.utils.formatdate() 

# Build the text/plain part of the alternative structure.
textplain = MIMEText('Custom plain text content', _subtype='plain', _charset='UTF-8')
msg.attach(textplain)

# Build the text/html part of the alternative structure.
texthtml = MIMEText('Custom HTML content', _subtype='html', _charset='UTF-8')
msg.attach(texthtml)

# Send the email.
try:
    client = smtplib.SMTP()
    # For Python 2.7 or later, create the client this way to use SSL.
    #client = smtplib.SMTP_SSL()

    # The standard SMTP port is 25 or 80.
    client.connect('smtpdm.aliyun.com', 25)

    # Enable DEBUG mode.
    client.set_debuglevel(0)

    client.login(username, password)

    # The sender address and the authentication address must be the same.
    # Note: To get the return value of the DATA command, use the 
    # SMTP.mail, SMTP.rcpt, and SMTP.data methods directly.
    client.sendmail(username, rcptto, msg.as_string())
    # To send to multiple recipients:
    #client.sendmail(username, receivers, msg.as_string())

    client.quit()
    print 'Email sent successfully!'
except smtplib.SMTPConnectError, e:
    print 'Failed to send the email. Connection error: ', e.smtp_code, e.smtp_error
except smtplib.SMTPAuthenticationError, e:
    print 'Failed to send the email. Authentication error: ', e.smtp_code, e.smtp_error
except smtplib.SMTPSenderRefused, e:
    print 'Failed to send the email. Sender refused: ', e.smtp_code, e.smtp_error
except smtplib.SMTPRecipientsRefused, e:
    print 'Failed to send the email. Recipient refused: ', e.smtp_code, e.smtp_error
except smtplib.SMTPDataError, e:
    print 'Failed to send the email. Data error: ', e.smtp_code, e.smtp_error
except smtplib.SMTPException, e:
    print 'Failed to send the email. ', e.message
except Exception, e:
    print 'An exception occurred while sending the email. ', str(e)