Bu örnek, düz metin, HTML ve birden çok alıcı desteğiyle Python 2 ve SMTP protokolü kullanılarak Direct Mail üzerinden e-postanın nasıl gönderileceğini gösterir.
Ön koşullar
Başlamadan önce aşağıdakilere sahip olduğunuzdan emin olun:
Direct Mail konsolunda oluşturulmuş bir gönderen adresi (bu adres,
usernamedeğeri olarak kullanılır)İlgili gönderen adresine ait SMTP parolası (bu parola, Direct Mail konsolundaki gönderen adresi ayarları altında oluşturulur ve
passworddeğeri olarak kullanılır)Bir veya daha fazla alıcı adresi
(İsteğe bağlı) Yanıtla adresi
SMTP kullanarak e-posta gönderme
Aşağıdaki örnek, hem düz metin hem de HTML gövde bölümlerini içeren çok parçalı bir e-posta oluşturur ve bunu smtpdm.aliyun.com adresindeki Direct Mail SMTP sunucusu üzerinden gönderir. Çalıştırmadan önce tüm '***' yer tutucuları gerçek değerlerinizle değiştirin.
# -*- 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)
Parametreler
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Bağlantı seçenekleri
Örnek, 25 numaralı bağlantı noktasında şifrelenmemiş bir bağlantı kullanır. SSL kullanmak için smtplib.SMTP() ifadesini smtplib.SMTP_SSL() ile değiştirin. SSL bağlantı noktası ve diğer bağlantı gereksinimleri için Direct Mail belgelerine bakın.
Üretim ortamı kullanımında SSL kullanın. 25 numaralı bağlantı noktasındaki şifrelenmemiş bağlantı geliştirme ve hata ayıklama için çalışsa da kimlik bilgilerinizi veya mesaj içeriğinizi aktarım sırasında şifrelemez.
Hata işleme
Her özel durum bloğu, teslimat hatalarını teşhis etmek için kullanabileceğiniz SMTP yanıt kodunu ve hata mesajını yazdırır:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|