發送smtp電子郵件,當我收到電子郵件時,它會顯示純文本版本和html版本。這一點是從Sendgrid接收電子郵件字典,然後將它們發送給另一個用戶。代碼中引用的'message'對象是字典Sendgrid發佈到我的端點。顯示普通版本和HTML版本的Python SMTP電子郵件
這是我所看到的:
test
Me
Signature
test
Me
Signature
這裏是我發送到郵件服務器字符串:
Content-Type: multipart/mixed; boundary="===============5453410005537724489=="
MIME-Version: 1.0
To: [email protected]
From: Me <[email protected]>
Subject: test
reply-to: Original Sender <[email protected]>
--===============5453410005537724489==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
test
Me
Signature
--===============5453410005537724489==
Content-Type: text/html; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><span class="Apple-tab-span" style="white-space:pre"> </span>test<br class=""><div apple-content-edited="true" class="">
<span>Me</span><br><span>Signature</span>
</div>
<br class=""></body></html>
--===============5453410005537724489==--
最後,這裏是Python我使用發送電子郵件:
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
subject = message.get('subject', 'No Subject')
text = message.get('text', None)
html = message.get('html', None)
to = message.get('to')
cc = message.get('cc', None)
reply_to = message.get('from')
msg = MIMEMultipart()
msg['To'] = '[email protected]'
msg['From'] = '[email protected]'
msg['Subject'] = subject
msg.add_header('reply-to', reply_to)
toaddrs = msg['To']
if cc is not None:
msg['CC'] = ', '.join(cc)
toaddrs += ', ' + msg['CC']
if text is not None:
msg.attach(MIMEText(text[0].encode('ascii', 'ignore'), 'plain'))
else:
msg.attach(MIMEText('No plain text for this email', 'plain'))
if html is not None:
msg.attach(MIMEText(html[0].encode('ascii', 'ignore'), 'html'))
mailServer = smtplib.SMTP("smtp.gmail.com", 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(GMAIL_USERNAME, GMAIL_PASSWORD)
mailServer.sendmail(GMAIL_USERNAME, toaddrs, msg.as_string())
mailServer.quit()
我在這裏錯過了什麼?
驚人的解釋,謝謝。 – Crowson 2014-11-21 02:16:13