tencent cloud

Feedback

Sample Call for Python

Last updated: 2024-12-23 14:32:54
python version >= 3.6
# -*- coding:utf-8 -*-
import smtplib
import ssl
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import formataddr


def send_mail(sender, sender_alias, sender_pwd, recipient_list, subject, body, host="sg-smtp.qcloudmail.com", port=465,
is_use_ssl=True):
try:
message = MIMEMultipart('alternative')
message['Subject'] = Header(subject, 'UTF-8')
message['From'] = formataddr([sender_alias, sender])
message['To'] = ",".join(recipient_list)
to_addr_list = recipient_list

mime_text = MIMEText(body, _subtype='html', _charset='UTF-8')
message.attach(mime_text)

if is_use_ssl:
context = ssl.create_default_context()
context.set_ciphers('DEFAULT')
client = smtplib.SMTP_SSL(host, port, context=context)
else:
client = smtplib.SMTP(host, port)

client.login(sender, sender_pwd)
client.sendmail(sender, to_addr_list, message.as_string())
client.quit()

print('Send email success!')
except smtplib.SMTPConnectError as e:
print('Send email failed,connection error:', e.smtp_code, e.smtp_error)
except smtplib.SMTPAuthenticationError as e:
print('Send email failed,smtp authentication error:', e.smtp_code, e.smtp_error)
except smtplib.SMTPSenderRefused as e:
print('Send email failed,sender refused:', e.smtp_code, e.smtp_error)
except smtplib.SMTPRecipientsRefused as e:
print('Send email failed,recipients refused:', e.recipients)
except smtplib.SMTPDataError as e:
print('Send email failed,smtp data error:', e.smtp_code, e.smtp_error)
except smtplib.SMTPException as e:
print('Send email failed,smtp exception:', str(e))
except Exception as e:
print('Send email failed,other error:', str(e))


if __name__ == '__main__':
# Sender address created in the console
from_email = "xxx@xxxx"
# SMTP password set in the console
from_email_pwd = "xxx"
# Recipient email address list
to_email_list = ["xxx@xxx1", "xxx@xxx2"]

# Sender email alias
from_alias = "Test Alias"
# Email subject
subject_txt = "[Test Subject]"
# Email content
body_content = (
"<!DOCTYPE html>\\n<html>\\n<head>\\n<meta charset=\\"utf-8\\">\\n<title>hello world</title>\\n</head>\\n<body>\\n "
"<h1>My First Heading</h1>\\n <p>My First Paragraph.</p>\\n</body>\\n</html>")

# Use SSL to send emails by default; the port can be 465 or 58
is_using_ssl = True
# SMTP service address, Hong Kong=smtp.qcloudmail.com, Singapore=sg-smtp.qcloudmail.com, Guangzhou=gz-smtp.qcloudmail.com
smtp_host = "sg-smtp.qcloudmail.com"
# SMTP port number, 465 and 587 use SSL encryption, 25 uses TLS
smtp_port = 465

# Use port 25 to send emails
# is_using_ssl = False
# smtp_host = "sg-smtp.qcloudmail.com"
# smtp_port = 25
send_mail(from_email, from_alias, from_email_pwd, to_email_list, subject_txt, body_content,
smtp_host, smtp_port, is_using_ssl)

Contact Us

Contact our sales team or business advisors to help your business.

Technical Support

Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

7x24 Phone Support