Question

I'm sending mails form a Python script with smtplib. I'm wary that there may be unforeseen bugs in my script that would cause it to erroneously send mails to all customers from the production database. Is there any way to temporarily store the mails before sending them? Another mail library maybe?

I still want to send the mails if they are ok, so just logging is not an option.

Was it helpful?

Solution

Some suggestions:

1: Set up a local Postfix server and configure it to put all e-mails on hold. Then manually inspect the mail queues after running your script. Release the e-mails when you see that everything is good.

I am no postfix expert, so I'm unsure about the details. Here's a man page with some info: http://www.postfix.org/access.5.html (look for the HOLD action).

2: Use a command-line option to your script:

def sendmail(args, recipient):
    if args.dryrun:
        print "Would send to " + recipient
    else:
        print "Sending to " + recipient
        smtp.sendmail(...)

.

$ send_emails.py --dryrun
Would send to aa@aa.com
...
Would send to bb@bb.com

$ send_emails.py
Sending to aa@aa.com
Sending to bb@bb.com
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top