SMTP 請求方式

最新更新:2023-03-14

為開發者提供 SMTP 協定的投遞管道,以下是會話過程。

S: 220 EngageLab Inbound Server ESMTP Haraka 2.2.4 ready C: ehlo ifaxin.com S: 250-EngageLab Inbound Server Hello, Haraka is at your service. S: 250-PIPELINING S: 250-8BITMIME S: 250-SIZE 16000000 S: 250 AUTH LOGIN C: AUTH LOGIN base64(api_user) S: 334 UGFzc3dvcmQ6 C: base64(api_key) S: 235 Authentication successful C: mail FROM:<support@engagelab.com> S: 250 sender <support@engagelab.com> OK C: rcpt TO:<ben@ifaxin.com> S: 250 recipient <ben@ifaxin.com> OK C: data S: 354 go ahead, make my day C: ... ... C: . S: 250 #1426390015358_15_6484_8661.sc-10_10_127_51-inbound#Queued C: quit S: 221 EngageLab Inbound Server closing connection. Have a jolly good day
          S: 220 EngageLab Inbound Server ESMTP Haraka 2.2.4 ready

C: ehlo ifaxin.com

S: 250-EngageLab Inbound Server Hello, Haraka is at your service.
S: 250-PIPELINING
S: 250-8BITMIME
S: 250-SIZE 16000000
S: 250 AUTH LOGIN

C: AUTH LOGIN base64(api_user)

S: 334 UGFzc3dvcmQ6

C: base64(api_key)

S: 235 Authentication successful

C: mail FROM:<support@engagelab.com>

S: 250 sender <support@engagelab.com> OK

C: rcpt TO:<ben@ifaxin.com>

S: 250 recipient <ben@ifaxin.com> OK

C: data

S: 354 go ahead, make my day

C: ... ...
C: .

S: 250 #1426390015358_15_6484_8661.sc-10_10_127_51-inbound#Queued

C: quit

S: 221 EngageLab Inbound Server closing connection. Have a jolly good day

        
此代碼塊在浮窗中顯示

注意 SMTP 調用返回 536 Frequency limited,意味著每個 apiuser 每分鐘,api 和 smtp 總共調用不能超過 3 萬次

SMTP 方式接入使用標籤功能

驗證 API_USER 時將標籤 ID(label_id)拼接在後面即可,拼接規則:

api_user + '#' + label_id

不使用標籤接入:

S: 250-EngageLab Inbound Server Hello, Haraka is at your service. S: 250-PIPELINING S: 250-8BITMIME S: 250-SIZE 16000000 S: 250-STARTTLS S: 250 AUTH LOGIN C: AUTH LOGIN base64(api_user)
          S: 250-EngageLab Inbound Server Hello, Haraka is at your service.
S: 250-PIPELINING
S: 250-8BITMIME
S: 250-SIZE 16000000
S: 250-STARTTLS
S: 250 AUTH LOGIN

C: AUTH LOGIN base64(api_user)

        
此代碼塊在浮窗中顯示

使用標籤接入:

S: 250-EngageLab Inbound Server Hello, Haraka is at your service. S: 250-PIPELINING S: 250-8BITMIME S: 250-SIZE 16000000 S: 250-STARTTLS S: 250 AUTH LOGIN C: AUTH LOGIN base64(api_user + '#' + label_id)
          S: 250-EngageLab Inbound Server Hello, Haraka is at your service.
S: 250-PIPELINING
S: 250-8BITMIME
S: 250-SIZE 16000000
S: 250-STARTTLS
S: 250 AUTH LOGIN

C: AUTH LOGIN base64(api_user + '#' + label_id)

        
此代碼塊在浮窗中顯示

python 代碼片段:

# 不使用標籤接入: s = SMTP('%s:%d' % (HOST, PORT)) s.set_debuglevel(DEBUG_MODE) if USE_SSL: s.starttls() s.login(API_USER, API_KEY) # 使用標籤接入: s = SMTP('%s:%d' % (HOST, PORT)) s.set_debuglevel(DEBUG_MODE) if USE_SSL: s.starttls() s.login(API_USER + '#' + label_id, API_KEY)
          # 不使用標籤接入:
s = SMTP('%s:%d' % (HOST, PORT))
s.set_debuglevel(DEBUG_MODE)
if USE_SSL:
    s.starttls()
s.login(API_USER, API_KEY)

# 使用標籤接入:
s = SMTP('%s:%d' % (HOST, PORT))
s.set_debuglevel(DEBUG_MODE)
if USE_SSL:
    s.starttls()
s.login(API_USER + '#' + label_id, API_KEY)

        
此代碼塊在浮窗中顯示

SMTP 郵件發送內嵌圖片

程式碼示例如下:

from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.image import MIMEImage import smtplib class MyEmail: def __init__(self): self.smtp = smtplib.SMTP() self.login_username = 'postmaster@testdomain' self.login_password = '*****' self.sender = 'postmaster@testdomain' # same as login_username self.receiver = 'test123@qq.com' self.host = 'smtp.engagelab.net' self.port = 25 def connect(self): self.smtp.connect(self.host) def login(self): self.smtp.login(self.login_username, self.login_password) def send(self): msg = MIMEMultipart('related') msg['From'] = self.sender msg['To'] = self.receiver email_title = 'python test inline image' msg['Subject'] = email_title content = MIMEText('test image content <img src="cid:image1" alt="xxxxx">', 'html', 'utf-8') msg.attach(content) fp = open('./test.png','rb') img = MIMEImage(fp.read()) img.add_header('Content-ID','image1') #the value of content-id is the cid in html msg.attach(img) self.smtp.sendmail(self.sender, self.receiver, msg.as_string()) def quit(self): self.smtp.quit() def send(): myemail = MyEmail() myemail.connect() myemail.login() myemail.send() myemail.quit() if __name__ == "__main__": send()
          from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
import smtplib

class MyEmail:
    def __init__(self):
        self.smtp = smtplib.SMTP()
        self.login_username = 'postmaster@testdomain'
        self.login_password = '*****'
        self.sender = 'postmaster@testdomain'    # same as login_username
        self.receiver = 'test123@qq.com'
        self.host = 'smtp.engagelab.net'
        self.port = 25

    def connect(self):
        self.smtp.connect(self.host)

    def login(self):
        self.smtp.login(self.login_username, self.login_password)

    def send(self):
        msg = MIMEMultipart('related')
        msg['From'] = self.sender
        msg['To'] = self.receiver
        email_title = 'python test inline image'
        msg['Subject'] = email_title
        content = MIMEText('test image content <img src="cid:image1" alt="xxxxx">', 'html', 'utf-8')
        msg.attach(content)

        fp = open('./test.png','rb')
        img = MIMEImage(fp.read())
        img.add_header('Content-ID','image1') #the value of content-id is the cid in html
        msg.attach(img)

        self.smtp.sendmail(self.sender, self.receiver, msg.as_string())

    def quit(self):
        self.smtp.quit()

def send():
    myemail = MyEmail()
    myemail.connect()
    myemail.login()
    myemail.send()
    myemail.quit()

if __name__ == "__main__":
    send()

        
此代碼塊在浮窗中顯示

SMTP 協定請求發送郵件加密

POODLE 漏洞表明 SSL v3.0 現在是完全不安全的,故用戶通過 SMTP 接入加密協定支持 Tls-v1.0-v1.2,不支持 ssl3.0 及之前版本。

端口 25 支持 STARTTLS 加密,需要用戶在程式碼添加一段加密程式碼,如 java 語言添加:

props.setProperty(“mail.smtp.starttls.enable”,“true”);

在文档中心打开
icon
聯繫銷售