diff --git a/frappe/email/bulk.py b/frappe/email/bulk.py index b252d8069f..3258cb5793 100644 --- a/frappe/email/bulk.py +++ b/frappe/email/bulk.py @@ -9,7 +9,7 @@ from frappe.email.smtp import SMTPServer, get_outgoing_email_account from frappe.email.email_body import get_email, get_formatted_html from frappe.utils.verified_command import get_signed_params, verify_request from html2text import html2text -from frappe.utils import get_url, nowdate, encode, now_datetime, add_days +from frappe.utils import get_url, nowdate, encode, now_datetime, add_days, time_diff_in_seconds class BulkLimitCrossedError(frappe.ValidationError): pass @@ -186,19 +186,24 @@ def flush(from_test=False): else: break - frappe.db.sql("""update `tabBulk Email` set status='Sending' where name=%s""", - (email["name"],), auto_commit=auto_commit) - try: - if not from_test: - smtpserver.setup_email_account(email.reference_doctype) - smtpserver.sess.sendmail(email["sender"], email["recipient"], encode(email["message"])) - - frappe.db.sql("""update `tabBulk Email` set status='Sent' where name=%s""", + if time_diff_in_seconds(None, email["creation"]) > 259200: + # expire mails older than 3 days + frappe.db.sql("""update `tabBulk Email` set status='Expired' where name=%s""", (email["name"],), auto_commit=auto_commit) + else: + frappe.db.sql("""update `tabBulk Email` set status='Sending' where name=%s""", + (email["name"],), auto_commit=auto_commit) + try: + if not from_test: + smtpserver.setup_email_account(email.reference_doctype) + smtpserver.sess.sendmail(email["sender"], email["recipient"], encode(email["message"])) - except Exception, e: - frappe.db.sql("""update `tabBulk Email` set status='Error', error=%s - where name=%s""", (unicode(e), email["name"]), auto_commit=auto_commit) + frappe.db.sql("""update `tabBulk Email` set status='Sent' where name=%s""", + (email["name"],), auto_commit=auto_commit) + + except Exception, e: + frappe.db.sql("""update `tabBulk Email` set status='Error', error=%s + where name=%s""", (unicode(e), email["name"]), auto_commit=auto_commit) def clear_outbox(): """remove mails older than 30 days in Outbox""" diff --git a/frappe/tests/test_email.py b/frappe/tests/test_email.py index 25f42e743d..0b4f1e77fe 100644 --- a/frappe/tests/test_email.py +++ b/frappe/tests/test_email.py @@ -47,6 +47,15 @@ class TestEmail(unittest.TestCase): self.assertTrue('test@example.com' in [d['recipient'] for d in bulk]) self.assertTrue('test1@example.com' in [d['recipient'] for d in bulk]) + def test_expired(self): + self.test_bulk() + frappe.db.sql("update `tabBulk Email` set creation='2010-01-01 12:00:00'") + from frappe.email.bulk import flush + flush(from_test=True) + bulk = frappe.db.sql("""select * from `tabBulk Email` where status='Expired'""", as_dict=1) + self.assertEquals(len(bulk), 2) + self.assertTrue('test@example.com' in [d['recipient'] for d in bulk]) + self.assertTrue('test1@example.com' in [d['recipient'] for d in bulk]) def test_unsubscribe(self): from frappe.email.bulk import unsubscribe, send