[Tutor] sending email via SMTP: code review requested

Steven D'Aprano steve at pearwood.info
Mon May 5 19:53:25 CEST 2014


On Sun, May 04, 2014 at 07:00:24PM -0400, Brian van den Broek wrote:
> Hi all,
> 
> I am playing with the smtp and email modules from the standard library
> of Python 2.7.3 (I also want it to run on 2.6.6). I've not found the
> going easy; the SMTP and RFC 2822 standards are not ones I have worked
> with before. 

Neither have I :-(

> I have something that works, but I am not confident I am
> doing the right thing. For that matter, I am not very confident that I
> am not doing the wrong thing.

The code seems nicely written, it's understandable and easy to read. I 
haven't tried running it yet, but nothing stands out as obviously wrong.


> I would very much appreciate some more experienced eyes on the code below.
> In addition to any outright errors concerning interaction with an SMTP
> server and constructing a MIME message, I would of course also welcome
> style comments. (Preemptively, I will note it isn't obvious I ought to
> have gone OOP with this.)

Having the SMTPSender object send a message automatically on 
instantiation strikes me as a bit wiffy. I'm not sure if it's a good 
design or not. But for a simple cron job, it may be fine.

[...]
> And, as I side note, could anyone explain why changing a first world
> of a body line 'From' to '>From' is the preferred standard?

Because it's a dirty, nasty hack invented by somebody who wasn't 
thinking very carefully at the time, and now everybody does it. Bleh.

> I
> understand what the problem is that is being solved, but as most email
> clients interpret a leading '>' as an indication of quoting, I would
> have thought ' From' or something like '-From' would have been better.
> If I have my own code deal with the problem in one of these ways, will
> I be breaking anything?

Yes. The idea is that your email client should recognise the hack when 
it sees a line ">From ..." and hide the leading ">". So if you use some 
other character, say, "!From ...", other people's mail clients won't 
know to hide the "!".


-- 
Steven


More information about the Tutor mailing list