簡體   English   中英

無法發送整個 email - smtp

[英]Not being able to send the whole email - smtp

sent_subject = "Warning"
sent_body = (
             "Hey"
             "For game ID: {} and game Name: {} \n\n you are going to die in {}sec"
             "\n\n"
             "Warning,\n"
             "Chixcy\n".format(game_id, game_name, secs))

smtp_server.sendmail(sender_add,receiver_add,sent_body)

我得到的 email 是you are going to die in 30sec Warning Chixcy

我想讓整個身體和主題一起

這是因為前幾行應該是 email 標頭。 您也永遠不會在任何地方使用您的主題 - 這可能是一個提示。

因此我們需要

from email.mime.text import MIMEText
message = MIMEText(sent_body, 'plain')
message["From"] = sender_add
message["To"] = receiver_add
message["Subject"] = sent_subject

然后當你在做.sendmail 時,你必須:

smtp_server.sendmail(sender_add, receiver_add, message.as_string())

A note: There are also more convenient (more human-friendly) classes to deal with emails (eg email.message.EmailMessage introduced in Python 3.6), but as I've been mostly dealing with multipart emails since Python 3.4, pure MIME messages是我的方式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM