从电子邮件中的标题添加

2024-04-20 08:31:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我做了一个简单的程序,自动为我发送电子邮件

然而,当我从riseup.net或protonmail发送电子邮件时,我有时无法发送,因为gmail需要“发件人”标题

这就是错误的样子

This is the mail system at host mx1.riseup.net.

I'm sorry to have to inform you that your message could not be delivered to one or more recipients. It's attached below.

For further assistance, please send mail to postmaster.

If you do so, please include this problem report. You can delete your own text from the attached returned message.

The mail system

: host aspmx.l.google.com[74.125.142.27] said: 550-5.7.1 [198.252.153.129 11] Our system has detected that this message is 550-5.7.1 not RFC 5322 compliant: 550-5.7.1 'From' header is missing. 550-5.7.1 To reduce the amount of spam sent to Gmail, this message has been 550-5.7.1 blocked. Please visit 550-5.7.1 https://support.google.com/mail/?p=RfcMessageNonCompliant 550 5.7.1 and review RFC 5322 specifications for more information. 62si8602934pjo.111 - gsmtp (in reply to end of DATA command)

我查看了如何添加from标题,但由于某些原因,我无法使其工作

这是我的密码

def send_mail():
    context = ssl.create_default_context()
    with smtplib.SMTP(smtp_server, 587) as server:
        server.starttls(context=context)
        server.login(susername, spassword)
        server.sendmail(semail, recipient, message)
        print("Email has been sent!")

编辑:当我从浏览器中发送电子邮件时,Riseup会自动添加此标题,但当我使用python登录到他们的SMTP服务器时,不会自动添加此标题


Tags: thetoyouhost标题messagenetserver
1条回答
网友
1楼 · 发布于 2024-04-20 08:31:27

我也面临同样的问题。我可以通过在消息体中显式添加From:字段来解决这个问题。例如,下面的消息正文将起作用

message = """From: <your email address>
To: <receiver's email address>\n
Subject: <your subject>\n

Hi, this is a test mail! """

从你评论中的链接中得到了这个想法。 Add Subject header to server.sendmail() in Python

相关问题 更多 >