开发者

Quarantine mails before sending with smtplib

开发者 https://www.devze.com 2023-03-23 02:56 出处:网络
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 databa

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开发者_StackOverflow中文版, so just logging is not an option.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号