def send(recipients=None, sender=None, doctype='Profile', email_field='email', subject='[No Subject]', message='[No Content]', ref_doctype=None, ref_docname=None): """send bulk mail if not unsubscribed and within conf.bulk_mail_limit""" import webnotes def is_unsubscribed(rdata): if not rdata: return 1 return cint(rdata.unsubscribed) def check_bulk_limit(new_mails): from webnotes import conf from webnotes.utils import nowdate this_month = webnotes.conn.sql("""select count(*) from `tabBulk Email` where month(creation)=month(%s)""" % nowdate())[0][0] monthly_bulk_mail_limit = conf.get('monthly_bulk_mail_limit') or 500 if this_month + len(recipients) > monthly_bulk_mail_limit: webnotes.msgprint("""Monthly Bulk Mail Limit (%s) Crossed""" % monthly_bulk_mail_limit, raise_exception=BulkLimitCrossedError) def update_message(doc): from webnotes.utils import get_url import urllib updated = message + """<div style="padding: 7px; border-top: 1px solid #aaa; margin-top: 17px;"> <small><a href="%s/?%s"> Unsubscribe</a> from this list.</small></div>""" % (get_url(), urllib.urlencode({ "cmd": "webnotes.utils.email_lib.bulk.unsubscribe", "email": doc.get(email_field), "type": doctype, "email_field": email_field })) return updated if not recipients: recipients = [] if not sender or sender == "Administrator": sender = webnotes.conn.get_value('Email Settings', None, 'auto_email_id') check_bulk_limit(len(recipients)) import HTMLParser from webnotes.utils.email_lib.html2text import html2text try: text_content = html2text(message) except HTMLParser.HTMLParseError: text_content = "[See html attachment]" for r in filter(None, list(set(recipients))): rdata = webnotes.conn.sql("""select * from `tab%s` where %s=%s""" % (doctype, email_field, '%s'), r, as_dict=1) doc = rdata and rdata[0] or {} if not is_unsubscribed(doc): # add to queue add(r, sender, subject, update_message(doc), text_content, ref_doctype, ref_docname)
def sendmail(recipients, sender='', msg='', subject='[No Subject]', parts=[], cc=[], attach=[], send_now=1, reply_to=None, template=None): """ send an html email as multipart with attachments and all """ from webnotes.utils.email_lib.html2text import html2text from webnotes.utils.email_lib.send import EMail email = EMail(sender, recipients, subject, reply_to=reply_to) email.cc = cc if msg: if template: msg = make_html_body(msg, template).encode('utf-8') else: # if not html, then lets put some whitespace if (not '<br>' in msg) or (not '<p>' in msg): msg = msg.replace('\n','<br>') footer = get_footer() msg = msg + (footer or '') email.set_text(html2text(msg)) email.set_html(msg) for p in parts: email.set_message(p[1]) for a in attach: email.attach(a) email.send(send_now)
def send(recipients=None, sender=None, doctype='Profile', email_field='email', subject='[No Subject]', message='[No Content]'): """send bulk mail if not unsubscribed and within conf.bulk_mail_limit""" import webnotes def is_unsubscribed(rdata): if not rdata: return 1 return rdata[0]['unsubscribed'] def check_bulk_limit(new_mails): import conf, startup from webnotes.utils import nowdate this_month = webnotes.conn.sql("""select count(*) from `tabBulk Email` where month(creation)=month(%s)""" % nowdate())[0][0] if hasattr(startup, 'get_monthly_bulk_mail_limit'): monthly_bulk_mail_limit = startup.get_monthly_bulk_mail_limit() else: monthly_bulk_mail_limit = getattr(conf, 'monthly_bulk_mail_limit', 500) if this_month + len(recipients) > monthly_bulk_mail_limit: webnotes.msgprint("""Monthly Bulk Mail Limit (%s) Crossed""" % monthly_bulk_mail_limit, raise_exception=BulkLimitCrossedError) def add_unsubscribe_link(email): from webnotes.utils import get_request_site_address import urllib return message + """<div style="padding: 7px; border-top: 1px solid #aaa; margin-top: 17px;"> <small><a href="%s/server.py?%s"> Unsubscribe</a> from this list.</small></div>""" % (get_request_site_address(), urllib.urlencode({ "cmd": "webnotes.utils.email_lib.bulk.unsubscribe", "email": email, "type": doctype, "email_field": email_field })) if not recipients: recipients = [] if not sender or sender == "Administrator": sender = webnotes.conn.get_value('Email Settings', None, 'auto_mail_id') check_bulk_limit(len(recipients)) import HTMLParser from webnotes.utils.email_lib.html2text import html2text try: text_content = html2text(message) except HTMLParser.HTMLParseError: text_content = "[See html attachment]" for r in list(set(recipients)): rdata = webnotes.conn.sql("""select * from `tab%s` where %s=%s""" % (doctype, email_field, '%s'), r, as_dict=1) if not is_unsubscribed(rdata): # add to queue add(r, sender, subject, add_unsubscribe_link(r), text_content)
def set_html_as_text(self, html): """return html2text""" import HTMLParser from webnotes.utils.email_lib.html2text import html2text try: self.set_text(html2text(html)) except HTMLParser.HTMLParseError: pass
def sendmail( recipients, sender="", msg="", subject="[No Subject]", parts=[], cc=[], attach=[], send_now=1, reply_to=None, template=None, from_defs=0, ): """ send an html email as multipart with attachments and all """ from webnotes.utils.email_lib.html2text import html2text from webnotes.utils.email_lib.send import EMail email = EMail(sender, recipients, subject, reply_to=reply_to, from_defs=from_defs) email.cc = cc if msg: if template: msg = make_html_body(msg, template).encode("utf-8") else: # if not html, then lets put some whitespace if (not "<br>" in msg) or (not "<p>" in msg): msg = msg.replace("\n", "<br>") footer = get_footer() msg = msg + (footer or "") email.set_text(html2text(msg)) email.set_html(msg) for p in parts: email.set_message(p[1]) for a in attach: email.attach(a) try: email.send(send_now) except smtplib.SMTPAuthenticationError: msgprint("Authentication at the mail server has failed. Please check your SMTP credentials and try again.") except: msgprint( "Mail was not sent. It could be a connection problem or you have entered the wrong data. If your data is \ correct please try after a couple of minutes." )
def sendmail(recipients, sender='', msg='', subject='[No Subject]', txt=None, \ parts=[], cc=[], attach=[], send_now=1, reply_to=None, template=None, from_defs=0): """ send an html email as multipart with attachments and all """ from webnotes.utils.email_lib.html2text import html2text from webnotes.utils.email_lib.send import EMail import HTMLParser email = EMail(sender, recipients, subject, reply_to=reply_to, from_defs=from_defs) email.cc = cc if msg: if template: msg = make_html_body(msg, template) else: # if not html, then lets put some whitespace if (not '<br>' in msg) and (not '<p>' in msg): msg = msg.replace('\n','<br>') footer = get_footer() # encode using utf-8 footer = footer.encode('utf-8', 'ignore') msg = msg + (footer or '') if txt: email.set_text(txt) else: try: msg_unicode = msg if isinstance(msg, str): msg_unicode = unicode(msg, 'utf-8', 'ignore') email.set_text(html2text(msg_unicode)) except HTMLParser.HTMLParseError: pass email.set_html(msg) for p in parts: email.set_message(p[1]) for a in attach: email.attach(a) email.send(send_now)
def sendmail(recipients, sender='', msg='', subject='[No Subject]', parts=[], cc=[], attach=[], send_now=1, reply_to=None, template=None, from_defs=0): """ send an html email as multipart with attachments and all """ from webnotes.utils.email_lib.html2text import html2text from webnotes.utils.email_lib.send import EMail email = EMail(sender, recipients, subject, reply_to=reply_to, from_defs=from_defs) email.cc = cc if msg: if template: msg = make_html_body(msg, template).encode('utf-8') else: # if not html, then lets put some whitespace if (not '<br>' in msg) or (not '<p>' in msg): msg = msg.replace('\n', '<br>') footer = get_footer() msg = msg + (footer or '') email.set_text(html2text(msg)) email.set_html(msg) for p in parts: email.set_message(p[1]) for a in attach: email.attach(a) email.send(send_now)
def send(recipients=None, sender=None, doctype='Profile', email_field='email', subject='[No Subject]', message='[No Content]', ref_doctype=None, ref_docname=None): """send bulk mail if not unsubscribed and within conf.bulk_mail_limit""" import webnotes def is_unsubscribed(rdata): if not rdata: return 1 return cint(rdata.unsubscribed) def check_bulk_limit(new_mails): from webnotes import conf from webnotes.utils import nowdate this_month = webnotes.conn.sql( """select count(*) from `tabBulk Email` where month(creation)=month(%s)""" % nowdate())[0][0] monthly_bulk_mail_limit = conf.get('monthly_bulk_mail_limit') or 500 if this_month + len(recipients) > monthly_bulk_mail_limit: webnotes.msgprint("""Monthly Bulk Mail Limit (%s) Crossed""" % monthly_bulk_mail_limit, raise_exception=BulkLimitCrossedError) def update_message(doc): from webnotes.utils import get_url import urllib updated = message + """<div style="padding: 7px; border-top: 1px solid #aaa; margin-top: 17px;"> <small><a href="%s/?%s"> Unsubscribe</a> from this list.</small></div>""" % ( get_url(), urllib.urlencode({ "cmd": "webnotes.utils.email_lib.bulk.unsubscribe", "email": doc.get(email_field), "type": doctype, "email_field": email_field })) return updated if not recipients: recipients = [] if not sender or sender == "Administrator": sender = webnotes.conn.get_value('Email Settings', None, 'auto_email_id') check_bulk_limit(len(recipients)) import HTMLParser from webnotes.utils.email_lib.html2text import html2text try: text_content = html2text(message) except HTMLParser.HTMLParseError: text_content = "[See html attachment]" for r in filter(None, list(set(recipients))): rdata = webnotes.conn.sql("""select * from `tab%s` where %s=%s""" % (doctype, email_field, '%s'), r, as_dict=1) doc = rdata and rdata[0] or {} if not is_unsubscribed(doc): # add to queue add(r, sender, subject, update_message(doc), text_content, ref_doctype, ref_docname)
def send(recipients=None, sender=None, doctype='Profile', email_field='email', first_name_field="first_name", last_name_field="last_name", subject='[No Subject]', message='[No Content]'): """send bulk mail if not unsubscribed and within conf.bulk_mail_limit""" import webnotes def is_unsubscribed(rdata): if not rdata: return 1 return rdata[0]['unsubscribed'] def check_bulk_limit(new_mails): import conf, startup from webnotes.utils import nowdate this_month = webnotes.conn.sql("""select count(*) from `tabBulk Email` where month(creation)=month(%s)""" % nowdate())[0][0] if hasattr(startup, 'get_monthly_bulk_mail_limit'): monthly_bulk_mail_limit = startup.get_monthly_bulk_mail_limit() else: monthly_bulk_mail_limit = getattr(conf, 'monthly_bulk_mail_limit', 500) if this_month + len(recipients) > monthly_bulk_mail_limit: webnotes.msgprint("""Monthly Bulk Mail Limit (%s) Crossed""" % monthly_bulk_mail_limit, raise_exception=BulkLimitCrossedError) def add_unsubscribe_link(email): from webnotes.utils import get_request_site_address import urllib return message + """<div style="padding: 7px; border-top: 1px solid #aaa; margin-top: 17px;"> <small><a href="%s/server.py?%s"> Unsubscribe</a> from this list.</small></div>""" % (get_request_site_address(), urllib.urlencode({ "cmd": "webnotes.utils.email_lib.bulk.unsubscribe", "email": email, "type": doctype, "email_field": email_field })) def full_name(rdata): fname = rdata[0].get(first_name_field, '') lname = rdata[0].get(last_name_field, '') if fname and not lname: return fname elif lname and not fname: return lname elif fname and lname: return fname + ' ' + lname else: return rdata[0][email_field].split('@')[0].title() if not recipients: recipients = [] if not sender or sender == "Administrator": sender = webnotes.conn.get_value('Email Settings', None, 'auto_mail_id') check_bulk_limit(len(recipients)) import HTMLParser from webnotes.utils.email_lib.html2text import html2text try: text_content = html2text(message) except HTMLParser.HTMLParseError: text_content = "[See html attachment]" for r in recipients: rdata = webnotes.conn.sql("""select * from `tab%s` where %s=%s""" % (doctype, email_field, '%s'), r, as_dict=1) if not is_unsubscribed(rdata): # add to queue add(r, sender, subject, add_unsubscribe_link(r), text_content)
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 = """<div style="padding: 7px; border-top: 1px solid #aaa; margin-top: 17px;"> <small><a href="%s/?%s"> Unsubscribe</a> from this list.</small></div>""" % (get_url(), urllib.urlencode({ "cmd": "webnotes.utils.email_lib.bulk.unsubscribe", "email": doc.get(email_field), "type": doctype, "email_field": email_field })) updated = updated.replace("<!--unsubscribe link here-->", unsubscribe_link) return updated if not recipients: recipients = [] if not sender or sender == "Administrator": sender = webnotes.conn.get_value('Email Settings', None, 'auto_email_id') check_bulk_limit(len(recipients)) try: text_content = html2text(message) except HTMLParser.HTMLParseError: text_content = "[See html attachment]" formatted = get_formatted_html(subject, message) for r in filter(None, list(set(recipients))): rdata = webnotes.conn.sql("""select * from `tab%s` where %s=%s""" % (doctype, email_field, '%s'), (r,), as_dict=1) doc = rdata and rdata[0] or {} if not is_unsubscribed(doc): # add to queue add(r, sender, subject, update_message(formatted, doc, add_unsubscribe_link), text_content, ref_doctype, ref_docname)