# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors # MIT License. See license.txt from __future__ import unicode_literals import webnotes import HTMLParser import urllib from webnotes import msgprint, throw, _ from webnotes.utils.email_lib.smtp import SMTPServer, send from webnotes.utils.email_lib.email_body import get_email, get_formatted_html from webnotes.utils.email_lib.html2text import html2text from webnotes.utils import cint, get_url, nowdate class BulkLimitCrossedError(webnotes.ValidationError): pass def send(recipients=None, sender=None, doctype='Profile', email_field='email', subject='[No Subject]', message='[No Content]', ref_doctype=None, ref_docname=None, add_unsubscribe_link=True): def is_unsubscribed(rdata): if not rdata: return 1 return cint(rdata.unsubscribed) def check_bulk_limit(new_mails): this_month = webnotes.conn.sql("""select count(*) from `tabBulk Email` where month(creation)=month(%s)""" % nowdate())[0][0] monthly_bulk_mail_limit = webnotes.conf.get('monthly_bulk_mail_limit') or 500 if this_month + len(recipients) > monthly_bulk_mail_limit: throw("{bulk} ({limit}) {cross}".format(**{ "bulk": _("Monthly Bulk Mail Limit"), "limit": monthly_bulk_mail_limit, "cross": _("crossed") }), exc=BulkLimitCrossedError) def update_message(formatted, doc, add_unsubscribe_link): updated = formatted if add_unsubscribe_link: unsubscribe_link = """
%s has been successfully unsubscribed.
" % email webnotes.response['type'] = 'page' webnotes.response['page_name'] = 'message.html' def flush(from_test=False): """flush email queue, every time: called from scheduler""" smtpserver = SMTPServer() auto_commit = not from_test if webnotes.flags.mute_emails or webnotes.conf.get("mute_emails") or False: msgprint(_("Emails are muted")) from_test = True for i in xrange(500): email = webnotes.conn.sql("""select * from `tabBulk Email` where status='Not Sent' limit 1 for update""", as_dict=1) if email: email = email[0] else: break webnotes.conn.sql("""update `tabBulk Email` set status='Sending' where name=%s""", (email["name"],), auto_commit=auto_commit) try: if not from_test: smtpserver.sess.sendmail(email["sender"], email["recipient"], email["message"]) webnotes.conn.sql("""update `tabBulk Email` set status='Sent' where name=%s""", (email["name"],), auto_commit=auto_commit) except Exception, e: webnotes.conn.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""" webnotes.conn.sql("""delete from `tabBulk Email` where datediff(now(), creation) > 30""")