Sometimes I get a bounceback when sending emails in Python using SMTPLIB & DKIMPY - Error: 550 Messages should have one From header, not 0

Viewed 69

Error Message:

SMTP error from remote server for TEXT command, host: mx10.mailspamprotection.com (x.x.x.x) reason: 550 Messages should have one From header, not 0.

Below is the code that creates the message, and DKIM signature etc. Running dkim.verify() returns True. This means that the signature is fine, however I have received one or two bounce backs like the above (Both "mailspamprotection.com") out of 50 emails.

I appreciate this isn't a high percentage but I want to overcome this issue.

msg = MIMEMultipart("alternative")
msg.attach(MIMEText(self.email_text, "plain"))
msg.attach(MIMEText(self.email_html, "html"))

**msg["To"] = self.to_email
msg["From"] = f"{self.from_name} <{self.from_email}>" if self.from_name else self.from_email
msg["Subject"] = self.subject**
msg['Date'] = formatdate()
msg['Message-ID'] = make_msgid(domain=self.sender_domain)

headers = [b'to', b'from', b'subject']

sig = dkim.sign(msg.as_bytes(), self.dkim_selector.encode(), self.sender_domain.encode(), self.dkim_key.encode(), include_headers=headers)

sig = sig.decode()

msg['DKIM-Signature'] = sig[len("DKIM-Signature: "):]

self.msg_data = msg.as_bytes()

For more context, here is the source of the email below, with confidential parts replaced with x's

Content-Type: multipart/alternative;
boundary="===============7989252151031916932==" MIME-Version: 1.0; 
To: xxxx@xxxxxxxxxxxxxxxxxx.co.uk;
From: xxxxx xxxxxx <xxxxx.xxxxxx@xxxxxxxx.xxxx>;
Subject: Thanks for Engaging with Our Website;
Date: Thu, 4 Jul 2020 17:38:00 -0000 Message-ID: <165780643314.283332.1993923921194087211@xxxxxxx.xxxx>;
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple;
d=xxxxxxx.xxxx; i=@xxxxxxx.xxxx; q=dns/txt; s=xxxxxx;
t=1657806433; h=from : to : subject;
bh=4pT7hDvR8C3Img5078vwzvijt0e+7b0oKT8Lr4shal8=;
b=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;

As you can see in the source here, we pass the following:

To:

From:

Subject:

We also pass those as "included_headers" so they are part of the DKIM signature, you'll see:

h=from : to : subject;

The issue is apparently I have no (0) from headers- that's simply not the case.

What am i missing?

Thanks In Advance!

0 Answers
Related