Why is smtplib.SMTP().sendmail not sending a DKIM signed message

社会主义新天地 提交于 2021-02-20 19:14:30

问题


I have set up postfix on a server, along with openDKIM.

When I run:

echo "Testing setup" | mail -s "Postfix test" {my_email_address}

I get the email, and in the mail headers there is a DKIM-Signature header.

When, however I write a python script to send an email, using smtplib:

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.utils import make_msgid

msg = MIMEMultipart('alternative')
part1 = MIMEText('Hello, world', 'plain')
msg.attach(part1)
msg['From'] = 'alert@{my_domain}'
msg['To'] = '{my_email_address}'
msg['Subject'] = 'Test Email'
msg['Message-ID'] = make_msgid()

mailer = smtplib.SMTP('localhost')
mailer.sendmail('alert@{my_domain}', '{my_email_address}', msg.as_string())
mailer.quit()

The email that arrives in my inbox is missing the DKIM-Signature header, and in the Authentication-Results I see dkim=none (no signatures found);

So my question is: Do I need to sign my email manually (eg. with dkimpy), or is there some setting I can enable to have it signed for me?

Let me know if there is any extra information you want/need.


回答1:


It's hard to say for sure since you haven't included any configuration or log information. But the OpenDKIM information has this to say:

A message will be verified unless it conforms to the signing criteria, which are: (1) the domain on the From: address (if present) must be listed by the −d command line switch or the Domain configuration file setting, and (2) (a) the client connecting to the MTA must have authenticated, or (b) the client connecting to the MTA must be listed in the file referenced by the InternalHosts configuration file setting (or be in the default list for that option), or (c) the client must be connected to a daemon port named by the MTAs configuration file setting, or (d) the MTA must have set one or more macros matching the criteria set by the MacroList configuration file setting.

In your SMTP code, you have clearly not authenticated to the MTA. So then the question becomes, does your configuration allow OpenDKIM to notice your desire in any of the other ways, such as having localhost in InternalHosts? (I think that is the default, but maybe you've overridden it.)



来源:https://stackoverflow.com/questions/49059606/why-is-smtplib-smtp-sendmail-not-sending-a-dkim-signed-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!