def clear_outbox():
 frappe.db.sql(u'delete q, r from `tabEmail Queue` as q, `tabEmail Queue Recipient` as r where q.name = r.parent and q.priority=0 and\n DCTB  DCTB datediff(now(), q.modified) > 31')
 frappe.db.sql(u"update `tabEmail Queue` as q, `tabEmail Queue Recipient` as r set q.status='Expired', r.status='Expired'\n DCTB  DCTB where q.name = r.parent and datediff(curdate(), q.modified) > 7 and q.status='Not Sent' and r.status='Not Sent'")

def clear_outbox ( ) : frappe . db . sql ( u'delete q, r from `tabEmail Queue` as q, `tabEmail Queue Recipient` as r where q.name = r.parent and q.priority=0 and\n DCTB  DCTB datediff(now(), q.modified) > 31' ) frappe . db . sql ( u"update `tabEmail Queue` as q, `tabEmail Queue Recipient` as r set q.status='Expired', r.status='Expired'\n DCTB  DCTB where q.name = r.parent and datediff(curdate(), q.modified) > 7 and q.status='Not Sent' and r.status='Not Sent'" )
remove low priority older than 31 days in outbox and expire mails not sent for 7 days .