gpt4 book ai didi

python - 用Python发送邮件主题

转载 作者:太空宇宙 更新时间:2023-11-03 19:17:48 27 4
gpt4 key购买 nike

当我的程序发送电子邮件时,我无法显示电子邮件的主题部分。我以为我遵循了 SMTP 的 RFC 规范..但我似乎无法弄清楚我做错了什么。非常感谢任何帮助。

def email():

sender = 'username@domain.com'
receivers = ['username@domain.com']

message = """From: From Admin <admin@domain.com>
To:To Person <user@domain.com>
Subject: Important Information

This is a test email message.
"""
try:
smtpObj = smtplib.SMTP('domain.com', 25)
smtpObj.sendmail(sender, receivers, message)
print "Successfully sent email"
except smtplib.SMTPException:
print('Error: unable to send email')

最佳答案

不确定您的代码有什么问题。

FWIW,我过去使用 string.join 创建消息正文:

def send_email():
import string,smtplib

SMTPserver = "smtp.com"
# To is a comma-separated list
To = "sender@domain.com"
From = "receipient@domain.com"
Subj = "test subject"
Text = """test email.
Not sure what the problem is
Multi-line anyway."""

Body = string.join((
"From: %s" % From,
"To: %s" % To,
"Subject: %s" % Subj,
"",
Text,
), "\r\n")

s = smtplib.SMTP(SMTPserver)
s.sendmail(From,[To],Body)

s.quit()

-J

关于python - 用Python发送邮件主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10774795/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com