1. 程式人生 > >python 利用 smtplib 傳送郵件方法

python 利用 smtplib 傳送郵件方法

說明

python 自帶了 smtplib 庫
可以直接呼叫並進行郵件傳送
預設狀態下, python 利用 base64 進行使用者名稱密碼傳遞
測試期間, 可以開啟 debug 功能, 方便進行排錯

測試程式碼

import smtplib

subject = 'message subject'
to = '收郵件的郵箱ex: [email protected]'
sender = '發郵件郵箱 ex: [email protected]'
user = '賬號'
password = '密碼'
mailserver = '郵件伺服器地址 ex: smtp.163.com'
mailport = '郵件伺服器埠 ex:25' try: smtpserver = smtplib.SMTP(mailserver, mailport) smtpserver.set_debuglevel(1) # 這裡可以開啟除錯模式, 除錯模式可以閱讀當前郵件伺服器支援哪些認證方法這個很重要 smtpserver.ehlo() smtpserver.starttls() # 啟用 tls 驗證方法, 如果伺服器需要提供 KEY, 那麼把 keyfile 放到函式中 smtpserver.ehlo() user = user password = password smtpserver.login(user, password) header = 'To:'
+ to + '\n' + 'From: ' + sender + '\n' + 'Subject:' + subject + '\n' message = header + '\n This is my message' smtpserver.sendmail(sender, to, message) smtpserver.close() except Exception as e: print e finally: print "done"

提示

錯誤資訊如下

smtplib.SMTPAuthenticationError: (535, '5.7.3 Authentication unsuccessful'

描述

常見情況是, 當前使用者名稱與密碼填寫有問題
user 部分不需要填寫整個郵件地址, 即不需要帶 @163.com 的域名

debug 資訊

常見 debug 資訊如下, 參考一下

send: 'ehlo this-is-my-hostname\r\n'
reply: '250-mail.server.repose Hello [xx.xxx.xxx.xx]\r\n'
reply: '250-SIZE 36700160\r\n'
reply: '250-PIPELINING\r\n'
reply: '250-DSN\r\n'
reply: '250-ENHANCEDSTATUSCODES\r\n'
reply: '250-STARTTLS\r\n'
reply: '250-AUTH NTLM LOGIN\r\n'
reply: '250-8BITMIME\r\n'
reply: '250-BINARYMIME\r\n'
reply: '250 CHUNKING\r\n'
reply: retcode (250); Msg: mail.server.repose Hello [10.xxx.xx.xx]
SIZE 36700160
PIPELINING
DSN
ENHANCEDSTATUSCODES
STARTTLS
AUTH NTLM LOGIN
8BITMIME
BINARYMIME
CHUNKING
send: 'ehlo pre-pinyun.localdomain\r\n'
reply: 'this-is-my-hostname Hello [x.x.x.x]\r\n'
reply: '250-SIZE 36700160\r\n'
reply: '250-PIPELINING\r\n'
reply: '250-DSN\r\n'
reply: '250-ENHANCEDSTATUSCODES\r\n'
reply: '250-STARTTLS\r\n'
reply: '250-AUTH NTLM LOGIN\r\n'
reply: '250-8BITMIME\r\n'
reply: '250-BINARYMIME\r\n'
reply: '250 CHUNKING\r\n'
reply: retcode (250); Msg: this-is-my-hostname Hello [x.x.x.x]
SIZE 36700160
PIPELINING
DSN
ENHANCEDSTATUSCODES
STARTTLS
AUTH NTLM LOGIN
8BITMIME
BINARYMIME
CHUNKING
send: 'AUTH LOGIN c2FuLnpoYW5n\r\n'
reply: '334 UGFzc3dvcmQ6\r\n'
reply: retcode (334); Msg: UGFzc3dvcmQ6
send: 'YWJjLjEyMzQ=\r\n'
reply: '235 2.7.0 Authentication successful\r\n'
reply: retcode (235); Msg: 2.7.0 Authentication successful
send: 'mail FROM:<[email protected]> size=97\r\n'
reply: '250 2.1.0 Sender OK\r\n'
reply: retcode (250); Msg: 2.1.0 Sender OK
send: 'rcpt TO:<[email protected]>\r\n'
reply: '250 2.1.5 Recipient OK\r\n'
reply: retcode (250); Msg: 2.1.5 Recipient OK
send: 'data\r\n'
reply: '354 Start mail input; end with <CRLF>.<CRLF>\r\n'
reply: retcode (354); Msg: Start mail input; end with <CRLF>.<CRLF>
data: (354, 'Start mail input; end with <CRLF>.<CRLF>')
send: 'To:[email protected]\r\nFrom: [email protected]\r\nSubject:message subject\r\n\r\n This is my message\r\n.\r\n'
reply: '250 2.6.0 <[email protected]> [InternalId=14018773254225, Hostname=this-is-my-hostname] Queued mail for delivery\r\n'
reply: retcode (250); Msg: 2.6.0 <[email protected]> [InternalId=14018773254225, Hostname=this-is-my-hostname] Queued mail for delivery
data: (250, '2.6.0 <[email protected]> [InternalId=14018773254225, Hostname=this-is-my-hostname] Queued mail for delivery')
done

留意這裡就是登陸驗證的過程, 資料經過了 base64 加密

send: 'AUTH LOGIN cs2FuLnxxxxxpoYW5n\r\n'
reply: '334 UGF3zcxxxxx3dvcmQ6\r\n'         <- 其中 334 就是伺服器正常應答的程式碼
reply: retcode (334); Msg: UGFzc3xxxxxdvcmQ6
send: 'YWsJwjLjxxxxxEyMzQ=\r\n'             <- 這裡傳送的就是你的密碼加密資訊
reply: '235 2.7.0 Authentication successful\r\n'    <- 這裡確認了驗證是否正確