我正在撰寫一個 python 腳本,該腳本每天都會向多個 Kindle 帳戶(已將此 Outlook 帳戶列入白名單)發送一封帶有新聞網站正文的批量電子郵件(連接到 Outlook.com 帳戶)作為電子書。
到目前為止,這是有效的,但從昨天開始,kindle 設備無法獲取電子書。我已經嘗試了很多,我發現,問題是因為所有發送的電子郵件在電子郵件的密件抄送部分都有接收者。由于某種原因,從昨天開始,如果收件人在密件抄送上,kindle 不會收到電子郵件(或處理它)。
所以我在問,我應該對我的代碼進行什么更改,以便我可以在電子郵件的 [TO] 部分而不是 [BCC] 上使用收件人地址。我不介意它是具有多個地址的單個電子郵件還是多個單個電子郵件,只要收件人不在密件抄送上。
receiver_email = ["[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]"]
subj = "Issue - " fullDate
msg = MIMEMultipart('alternative')
msg['Subject'] = subj
msg['From'] = sender_email
msg.attach(MIMEText('Forward this email to your kindle account'))
# Attach the .mobi file
print("Attaching " epubname)
fp = open(epubname, "rb")
epub_file = MIMEApplication(fp.read())
fp.close()
encoders.encode_base64(epub_file)
epub_file.add_header('Content-Disposition', 'attachment', filename=epubname)
msg.attach(epub_file)
debug = False
if debug:
print(msg.as_string())
else:
server = smtplib.SMTP('smtp.office365.com',587)
server.ehlo()
server.starttls()
server.login("##EMAIL##", "##PASSWORD##")
text = msg.as_string() "\r\n" message_text
for x in range(len(receiver_email)):
email_to = receiver_email[x]
msg['To'] = email_to #msg['To'] = ", ".join(email_to)
server.sendmail(sender_email, email_to, text.encode('utf-8'))
server.quit()
我找到了這個問題,但不幸的是沒有具體的答案。
uj5u.com熱心網友回復:
您的代碼似乎是為 Python 3.5 或更早版本撰寫的。該email庫在 3.6 中進行了大修,現在更加通用和合乎邏輯。可能會扔掉你所擁有的,然后從檔案中的示例重新開始。email
簡單而明顯的解決方案是將所有收件人放入msg["to"]而不是msg["bcc"].
這是針對 Python 3.3 的重構(新 API 已在 3.3 中非正式地引入)。
from email.message import EmailMessage
receiver_email = ["[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]"]
subj = "Issue - " fullDate
msg = EmailMessage()
msg['Subject'] = subj
msg['From'] = sender_email
msg['To'] = ", ".join(receiver_email)
msg.set_content('Forward this email to your Kindle account')
with open(epubname, "rb") as fp:
msg.add_attachment(fp.read()) # XXX TODO: add proper MIME type?
debug = False
if debug:
print(msg.as_string())
else:
with smtplib.SMTP('smtp.office365.com',587) as server:
server.ehlo()
server.starttls()
server.login("##EMAIL##", "##PASSWORD##")
server.send_message(msg)
這會向所有收件人發送一條訊息;這意味著如果他們查看原始訊息,他們可以看到彼此的電子郵件地址。您可以通過回傳回圈遍歷收件人并為每個收件人發送一條訊息來避免這種情況,但如果可以的話,您真的想避免這種情況。
順便說一句,range如果您不在乎您在串列中的位置,則不需要。在 Python 中回圈遍歷串列的慣用方法很簡單
for email_to in receiver_email:
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/527323.html
