2009-07-24 89 views
3

我正在製作一個django應用程序,它需要能夠發送電子郵件,然後在給定時間發送這些電子郵件。我在想我可以用django-mailer把東西放入並傳送出去。但即使是這個樣本案例列表,列出這是一個功能,我似乎不知道如何。如何使用django-mailer預定發送電子郵件

我需要的是能夠在django-mailer的消息模型中設置'when_to_send'字段,並且當cron作業觸發send_mail函數時,需要過濾掉具有'when_to_send'日期的字段比當前時間大...

def send_all(): 
    """ 
    Send all eligible messages in the queue. 
    """ 

    lock = FileLock("send_mail") 

    logging.debug("acquiring lock...") 
    try: 
     lock.acquire(LOCK_WAIT_TIMEOUT) 
    except AlreadyLocked: 
     logging.debug("lock already in place. quitting.") 
     return 
    except LockTimeout: 
     logging.debug("waiting for the lock timed out. quitting.") 
     return 
    logging.debug("acquired.") 

    start_time = time.time() 

    dont_send = 0 
    deferred = 0 
    sent = 0 

    try: 
     for message in prioritize(): 
      if DontSendEntry.objects.has_address(message.to_address): 
       logging.info("skipping email to %s as on don't send list " % message.to_address) 
       MessageLog.objects.log(message, 2) # @@@ avoid using literal result code 
       message.delete() 
       dont_send += 1 
      else: 
       try: 
        logging.info("sending message '%s' to %s" % (message.subject.encode("utf-8"), message.to_address.encode("utf-8"))) 
        core_send_mail(message.subject, message.message_body, message.from_address, [message.to_address]) 
        MessageLog.objects.log(message, 1) # @@@ avoid using literal result code 
        message.delete() 
        sent += 1 
       except (socket_error, smtplib.SMTPSenderRefused, smtplib.SMTPRecipientsRefused, smtplib.SMTPAuthenticationError), err: 
        message.defer() 
        logging.info("message deferred due to failure: %s" % err) 
        MessageLog.objects.log(message, 3, log_message=str(err)) # @@@ avoid using literal result code 
        deferred += 1 
    finally: 
     logging.debug("releasing lock...") 
     lock.release() 
     logging.debug("released.") 

    logging.info("") 
    logging.info("%s sent; %s deferred; %s don't send" % (sent, deferred, dont_send)) 
    logging.info("done in %.2f seconds" % (time.time() - start_time)) 

任何人看到如何自定義此功能在message.when_to_send場比當前時間大到不發送電子郵件的?

回答

0

你可以只增加一個條款的條件語句下您的消息處理循環(你還需要在你的文件的頂部導入日期時間):

  for message in prioritize(): 
      if DontSendEntry.objects.has_address(message.to_address): 
       logging.info("skipping email to %s as on don't send list " % message.to_address) 
       MessageLog.objects.log(message, 2) # @@@ avoid using literal result code 
       message.delete() 
       dont_send += 1 
      elif message.when_to_send > datetime.datetime.now(): 
       continue 
      else: 
       try: 
        ... the rest of your code ... 
+0

我試過這個,但是結果是優先級()函數的無限循環,那當然不好。 – espenhogbakk 2009-07-24 21:34:23

3

您需要實現django-mailer cron作業:

* * * * * (cd $PINAX; /usr/local/bin/python2.5 manage.py send_mail >> $PINAX/cron_mail.log 2>&1) 

然後在engine.py線96:

# Get rid of "while True:" 
    while not Message.objects.all(): 
     # Get rid of logging.debug("sleeping for %s seconds before checking queue again" % EMPTY_QUEUE_SLEEP) 
     # Get rid of sleep 
     send_all() 
相關問題