very small python smtpish send

Tim Williams listserver at tdw.net
Sat Oct 9 13:39:18 EDT 2004


> On Fri, 8 Oct 2004, Brad Tilley wrote:
>
>Anyone know of a small Python script that acts as a slimmed down smtp
>server (just sends from the local machine)? I currently use a smtp
>server for sending email reports from machines, but as machines travel
>outside of our lan, the smtp server refuses to relay their messages.

OK,  so the clients are sending email to addresses that are not local to
your lan's server (otherwise there would be no relaying attempt)

Have you considered turning on authentication on the lan server,  you can
then allow relaying only from an authenticated "client".

>I thought it would be better if each machine had its own little mail
>sender this way, it could continue reporting no matter where it was
located.

Sending directly from the client to the recipient address's server  is
valid, doesn't need authentication, and relatively simple to do  using
smtplib.py .  However,  nowadays this may be problematic because of
increasing anti-spam measures that *might* cause the email to be refused on
a variety of criteria (IP address (RBL) ,  IP address reverse lookup,
possibly SPF, etc etc ).

import smtplib
try:
    s = smtplib.SMTP(outgoing-server)
    failed = s.sendmail(out_from, to_list , msg)
except smtplib.SMTPRecipientsRefused, x :
    do something  # for recip in x.recipients:  info = x.recipients[recip]
except smtplib.SMTPDataError, x:
    do something  # x[0], x[1]
except smtplib.SMTPSenderRefused, x :
    do something  # x.sender, x.smtp_code, x.smtp_error
except:
    do something

(failed contains a list of failed recipients if *some* but not all failed,
if all failed then use
except smtplib.SMTPRecipientsRefused, x :)

Alternatively there are some free mail SPs out there that provide
authenticated outgoing servers that  you could use if  you can't turn on
authentication on your lan server.





More information about the Python-list mailing list