Script to send emails for updates

Started by bartjsmit, January 20, 2016, 02:02:16 PM

Previous topic - Next topic
RFC 2822 deals with message content. Can you try and edit the script to say 'text/plain' instead of 'text/html' or even remark out these two lines completely:

#message += 'MIME-Version: 1.0\n'
#message += 'Content-type: text/html\n'

Bart...

IMF is Internet Message Format, a.k.a. RFC 2822 :)

May 02, 2016, 08:05:54 AM #17 Last Edit: May 02, 2016, 08:07:55 AM by franco
I don't think the content headers matter, and I don't recall those being used by Email (MIME-Version and Content-type). Instead, maybe adhering to "\r\n" line breaks will help...

(And the last line dealing with reboot should probably also receive a newline.)

I tried using 'text/plain' instead of 'text/html', replacing all \n entries with \r\n and also tried remarking out both of these lines:
#message += 'MIME-Version: 1.0\n'
#message += 'Content-type: text/html\n'

but the script still comes back with "smtplib.SMTPDataError: (550, '5.7.1 Delivery not authorized, message refused: Message is not RFC 2822 compliant')"

This might be helpful.  On the network where I'm able to use the script without the RFC 2822 compliant (it relays through a Linux Postfix system, not an OpenBSD SMTP server), I noticed this in my Amavis quarantine this morning even though the email from the script was delivered:

X-Amavis-Alert: BAD HEADER SECTION, Missing required header field: "Date"

Could a missing Date header field be what's upsetting the SMTP daemon on the OpenBSD SMTP server who refuses to accept the email from the script due to the RFC 2822 compliant?

Just a thought.

Thanks,
ObecalpEffect.

The RFC states "The only required header fields are the origination date field and the originator address field(s).  All other header fields are syntactically optional.". You may be right. :)

I'll have a dig through the smtplib docs to see if there is an option to add the timestamp to the email headers.

Bart...

I wish I could help, but I couldn't program my way out of a wet paper bag.   :-\

That said though, I was playing with getting a properly formatted date string with python while trying to get the script working and I think this would suffice and possibly be RFC 2822 compliant.  I hope it's helpful.  I'll try to poke through the smtplib docs too and see if I can find a way to add the date header.  Hopefully it is the date header that is causing OpenBSD's SMTP daemon to complain about RFC 2822 compliancy and we're not chasing our tails here.

#!/usr/local/bin/python
# import libraries
from email.Utils import formatdate
print formatdate(localtime = 6)


May 06, 2016, 08:33:47 PM #23 Last Edit: May 06, 2016, 08:46:23 PM by ObecalpEffect
Hmm, not sure if I'm dreaming this or not, but I think I fixed it with the addition of these three lines in the script:

from email.Utils import formatdate
and
dateheader = 'formatdate(localtime = 6)\r\n'
and
message += '\r\ndateheader\r\n

So the entire top section of the script now looks like this (and it actually worked!  :)   )

#!/usr/local/bin/python
# import libraries
import json
import requests
import smtplib
from email.Utils import formatdate
api_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
api_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
host       = 'localhost'
recipients = 'me@there.edu'
rcpt_name  = 'First Last'
dateheader = 'formatdate(localtime = 6)\r\n'
url      = 'https://' + host + '/api/core/firmware/status'
sender   = 'emailrelay@there.edu'
message  = 'From: emailrelay@there.edu\r\n'
message += 'To: me@there.edu>\r\n'
message += 'MIME-Version: 1.0\r\n'
message += 'Content-type: text/html\r\n'
message += 'Subject: Updates Available for OPNsense\r\n'
message += '\r\ndateheader\r\n'
# request and process data


Thanks for that - looks to me like a pretty sturdy paper bag ;-)

I'll do some regression testing with postfix and attach a new version to this thread.

Bart...

You could also use a public git repository

Also published on https://github.com/bartsmit/opnsense-update-email

ObecalpEffect, I've credited you with your forum handle.

Bart...

Hi Bart,

Sorry to be a pain, but I noticed that the latest version of the script still isn't working with SMTP on my OpenBSD server.  SMTP on OpenBSD still complains "Message is not RFC 2822 compliant".

I was however able to get it working by replacing this line:
message += formatdate(localtime=True) + '\r\n'

With this line:
message += 'Date: = formatdate(localtime=True)'

*Please also note that it doesn't work when the line is appended with + '\r\n'

Thanks,
Obecalp.

Thanks Obecalp,

I will do some regression testing and post an updated script.

Bart...

The JSON response from OPNsense looks a bit different now. I have updated the script accordingly and made it python3 compatible.