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

1

4

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.

Catharinecatharsis answered 1/3, 2018 at 22:20 Comment(0)
B
2

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.)

Baptlsta answered 6/3, 2018 at 9:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.