0

I'm setting up a python-based email server on Ubuntu 18 and I'm confused by the different DKIM behavior on the command line vs in python. When I use sendmail < message.tx recipient, DKIM signs it.

But in python...

 s = smtplib.SMTP('localhost') s.sendmail(sender, recipient, body) 

... DKIM doesn't sign it. I assumed smtplib.SMTP() would find everything in /etc/postfix and /etc/opendkim. How do I make have the same behavior as the command line?

postfix mail.cf:

smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu) biff = no append_dot_mydomain = no readme_directory = no compatibility_level = 2 smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key smtpd_use_tls=yes smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination myhostname = U18_Nginx alias_maps = hash:/etc/aliases alias_database = hash:/etc/aliases myorigin = /etc/mailname mydestination = $myhostname, example.org, localhost.localdomain, localhost relayhost = mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 mailbox_size_limit = 0 recipient_delimiter = + inet_interfaces = all inet_protocols = all # Milter configuration milter_default_action = accept milter_protocol = 6 smtpd_milters = local:opendkim/opendkim.sock non_smtpd_milters = $smtpd_milters 

master.cf is the default I believe:

smtpd_client_restrictions=$mua_client_restrictions smtpd_sender_restrictions=$mua_sender_restrictions smtpd_relay_restrictions=permit_sasl_authenticated,reject qmqpd pickup unix n - y 60 1 pickup cleanup unix n - y - 0 cleanup qmgr unix n - n 300 1 qmgr tlsmgr unix - - y 1000? 1 tlsmgr rewrite unix - - y - - trivial-rewrite bounce unix - - y - 0 bounce defer unix - - y - 0 bounce trace unix - - y - 0 bounce verify unix - - y - 1 verify flush unix n - y 1000? 0 flush proxymap unix - - n - - proxymap proxywrite unix - - n - 1 proxymap smtp unix - - y - - smtp relay unix - - y - - smtp -o syslog_name=postfix/$service_name smtp_connect_timeout=5 showq unix n - y - - showq error unix - - y - - error retry unix - - y - - error discard unix - - y - - discard local unix - n n - - local virtual unix - n n - - virtual lmtp unix - - y - - lmtp anvil unix - - y - 1 anvil scache unix - - y - 1 scache maildrop_destination_recipient_limit=1 maildrop unix - n n - - pipe flags=DRhu user=vmail argv=/usr/bin/maildrop -d ${recipient} pipe ${sender} -m ${extension} ${user} pipe ${extension} ${user} uucp unix - n n - - pipe flags=Fqhu user=uucp argv=uux -r -n -z -a$sender - $nexthop!rmail ($recipient) # # Other external delivery methods. # ifmail unix - n n - - pipe flags=F user=ftn argv=/usr/lib/ifmail/ifmail -r $nexthop ($recipient) bsmtp unix - n n - - pipe flags=Fq. user=bsmtp argv=/usr/lib/bsmtp/bsmtp -t$nexthop -f$sender $recipient scalemail-backend unix - n n - 2 pipe flags=R user=scalemail argv=/usr/lib/scalemail/bin/scalemail-store ${nexthop} ${user} ${extension} mailman unix - n n - - pipe flags=FR user=list argv=/usr/lib/mailman/bin/postfix-to-mailman.py ${nexthop} ${user} 
3
  • Check the versions. Ubuntu 18.04 would be the release that went EoL in June 2023. If that was really your version, there is a decent chance that the message you are sending is not quite equal between those two cases. In any case, we cannot tell, as you have not shared the message (try print(repr(body))) Commented Feb 6 at 2:26
  • 1
    smtplib.SMTP() doesn't find anything in these directories. It connects to the SMTPd server running on localhost, which in turn reads those directories. Share your postfix configuration, that should shed some light on the different behaviour. Commented Feb 6 at 9:25
  • Thanks I posted the config files Commented Feb 6 at 19:09

1 Answer 1

0

I found out from an earlier post how to do DKIM signing in python:

My private DKIM key was located at /etc/opendkim/keys/example.org/default.private

import email import dkim import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText domain = 'example.org' headers = ["To", "From", "Subject"] msg = MIMEMultipart() msg['From'] = sender msg['To'] = recipient msg['Subject'] = subject msg['Date'] = email.utils.formatdate() msg['Message-ID'] = email.utils.make_msgid(domain=domain) msg.attach(MIMEText(plain, "plain")) msg.attach(MIMEText(html, "html")) sig = dkim.sign( message=msg.as_string().encode("ascii"), selector=str("mail").encode("ascii"), domain=domain.encode("ascii"), privkey=private_key, include_headers=headers, ) msg["DKIM-Signature"] = sig.decode("ascii").lstrip("DKIM-Signature: ") s = smtplib.SMTP('mail.example.org') s.sendmail(sender, recipient, msg.as_string()) 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.