def send_emails(self): """send emails to subscribers""" if self.doc.email_sent: webnotes.msgprint("""Blog Subscribers already updated""", raise_exception=1) from webnotes.utils.email_lib.bulk import send import webnotes.utils # get leads that are subscribed to the blog recipients = [ e[0] for e in webnotes.conn.sql("""select distinct email_id from tabLead where ifnull(blog_subscriber,0)=1""") ] # make heading as link content = '<h2><a href="%s/%s.html">%s</a></h2>\n\n%s' % ( webnotes.utils.get_request_site_address(), self.doc.page_name, self.doc.title, self.doc.content) # send the blog send(recipients=recipients, doctype='Lead', email_field='email_id', subject=self.doc.title, message=content) webnotes.conn.set(self.doc, 'email_sent', 1) webnotes.msgprint("""Scheduled to send to %s subscribers""" % len(recipients))
def send_daily_summary(for_date=None, event_date=None): if not for_date: for_date = add_days(today(), days=-1) if not event_date: event_date = today() formatted_date = getdate(for_date).strftime("%a, %d %B, %Y") formatted_event_date = getdate(event_date).strftime("%a, %d %B, %Y") subject = "[AAP Ka Manch] Updates for {formatted_date}".format(formatted_date=formatted_date) unit_post_map = get_unit_post_map(for_date, event_date) if not unit_post_map: # no updates! return for user in webnotes.conn.sql_list("""select name from `tabProfile` where user_type='Website User' and enabled=1 and name not in ('Administrator', 'Guest')"""): summary = prepare_daily_summary(user, unit_post_map, {"subject": subject, "formatted_date": formatted_date, "formatted_event_date": formatted_event_date}) if not summary: # no access! continue send(recipients=[user], subject=subject, message=summary, # to allow unsubscribe doctype='Profile', email_field='name', # for tracking sent status ref_doctype="Profile", ref_docname=user)
def add_comment(args=None): """ args = { 'comment': '', 'comment_by': '', 'comment_by_fullname': '', 'comment_doctype': '', 'comment_docname': '', 'page_name': '', } """ import webnotes import webnotes.utils, markdown2 if not args: args = webnotes.form_dict args['comment'] = unicode(markdown2.markdown(args.get('comment') or '')) args['doctype'] = "Comment" page_name = args.get("page_name") if "page_name" in args: del args["page_name"] if "cmd" in args: del args["cmd"] comment = webnotes.bean(args) comment.ignore_permissions = True comment.insert() # since comments are embedded in the page, clear the web cache webnotes.webutils.clear_cache(page_name) args['comment_date'] = webnotes.utils.global_date_format(comment.doc.creation) template_args = { 'comment_list': [args]} # get html of comment row from jinja2 import Environment, FileSystemLoader jenv = Environment(loader = FileSystemLoader(webnotes.utils.get_base_path())) template = jenv.get_template("lib/website/doctype/blog_post/templates/includes/comment.html") comment_html = template.render(template_args) # notify commentors commentors = [d[0] for d in webnotes.conn.sql("""select comment_by from tabComment where comment_doctype='Blog Post' and comment_docname=%s and ifnull(unsubscribed, 0)=0""", args.get('comment_docname'))] blog = webnotes.doc("Blog Post", args.get("comment_docname")) blogger_profile = webnotes.conn.get_value("Blogger", blog.blogger, "profile") blogger_email = webnotes.conn.get_value("Profile", blogger_profile, "email") from webnotes.utils.email_lib.bulk import send send(recipients=list(set(commentors + [blogger_email])), doctype='Comment', email_field='comment_by', subject='New Comment on Blog: ' + blog.title, message='%(comment)s<p>By %(comment_by_fullname)s</p>' % args, ref_doctype='Blog Post', ref_docname=blog.name) return comment_html.replace("\n", "")
def add_comment(args=None): """ args = { 'comment': '', 'comment_by': '', 'comment_by_fullname': '', 'comment_doctype': '', 'comment_docname': '', 'page_name': '', } """ import webnotes import webnotes.utils, markdown2 import webnotes.widgets.form.comments import website.web_cache if not args: args = webnotes.form_dict args['comment'] = unicode(markdown2.markdown(args.get('comment') or '')) comment = webnotes.widgets.form.comments.add_comment(args) # since comments are embedded in the page, clear the web cache website.web_cache.clear_cache(args.get('page_name'), args.get('comment_doctype'), args.get('comment_docname')) comment['comment_date'] = webnotes.utils.global_date_format( comment['creation']) template_args = { 'comment_list': [comment], 'template': 'html/comment.html' } # get html of comment row comment_html = website.web_cache.build_html(template_args) # notify commentors commentors = [ d[0] for d in webnotes.conn.sql( """select comment_by from tabComment where comment_doctype='Blog' and comment_docname=%s and ifnull(unsubscribed, 0)=0""", args.get('comment_docname')) ] blog = webnotes.conn.sql("""select * from tabBlog where name=%s""", args.get('comment_docname'), as_dict=1)[0] from webnotes.utils.email_lib.bulk import send send(recipients=list(set(commentors + [blog['owner']])), doctype='Comment', email_field='comment_by', first_name_field="comment_by_fullname", last_name_field="NA", subject='New Comment on Blog: ' + blog['title'], message='%(comment)s<p>By %(comment_by_fullname)s</p>' % args) return comment_html
def test_send(self, doctype="Lead"): args = self.dt_map[doctype] sender = webnotes.utils.get_email_id(self.doc.owner) recipients = self.doc.test_email_id.split(",") from webnotes.utils.email_lib.bulk import send send(recipients = recipients, sender = sender, subject = self.doc.subject, message = self.doc.message, doctype = doctype, email_field = args["email_field"]) webnotes.msgprint("""Scheduled to send to %s""" % self.doc.test_email_id)
def add_comment(args=None): """ args = { 'comment': '', 'comment_by': '', 'comment_by_fullname': '', 'comment_doctype': '', 'comment_docname': '', 'page_name': '', } """ if not args: args = webnotes.local.form_dict args['doctype'] = "Comment" page_name = args.get("page_name") if "page_name" in args: del args["page_name"] if "cmd" in args: del args["cmd"] comment = webnotes.bean(args) comment.ignore_permissions = True comment.insert() # since comments are embedded in the page, clear the web cache webnotes.webutils.clear_cache(page_name) # notify commentors commentors = [ d[0] for d in webnotes.conn.sql( """select comment_by from tabComment where comment_doctype=%s and comment_docname=%s and ifnull(unsubscribed, 0)=0""", (comment.doc.comment_doctype, comment.doc.comment_docname)) ] owner = webnotes.conn.get_value(comment.doc.comment_doctype, comment.doc.comment_docname, "owner") from webnotes.utils.email_lib.bulk import send send(recipients=list(set(commentors + [owner])), doctype='Comment', email_field='comment_by', subject='New Comment on %s: %s' % (comment.doc.comment_doctype, comment.doc.title or comment.doc.comment_docname), message='%(comment)s<p>By %(comment_by_fullname)s</p>' % args, ref_doctype=comment.doc.comment_doctype, ref_docname=comment.doc.comment_docname) template = webnotes.get_template( "lib/website/templates/includes/comment.html") return template.render({"comment": comment.doc.fields})
def test_send(self, doctype="Lead"): args = self.dt_map[doctype] sender = webnotes.utils.get_email_id(self.doc.owner) recipients = self.doc.test_email_id.split(",") from webnotes.utils.email_lib.bulk import send send(recipients = recipients, sender = sender, subject = self.doc.subject, message = self.get_message(), doctype = doctype, email_field = args["email_field"], first_name_field = args["first_name_field"], last_name_field = "") webnotes.msgprint("""Scheduled to send to %s""" % self.doc.test_email_id)
def test_bulk(self): from webnotes.utils.email_lib.bulk import send send(recipients = ['*****@*****.**', '*****@*****.**'], doctype='Profile', email_field='email', subject='Testing Bulk', message='This is a bulk mail!') bulk = webnotes.conn.sql("""select * from `tabBulk Email` where status='Not Sent'""", as_dict=1) self.assertEquals(len(bulk), 2) self.assertTrue('*****@*****.**' in [d['recipient'] for d in bulk]) self.assertTrue('*****@*****.**' in [d['recipient'] for d in bulk]) self.assertTrue('Unsubscribe' in bulk[0]['message'])
def test_bulk(self): from webnotes.utils.email_lib.bulk import send send(recipients = ['*****@*****.**', '*****@*****.**'], doctype='Lead', email_field='email_id', first_name_field='lead_name', last_name_field=None, subject='Testing Bulk', message='This is a bulk mail!') bulk = webnotes.conn.sql("""select * from `tabBulk Email` where status='Not Sent'""", as_dict=1) self.assertEquals(len(bulk), 2) self.assertTrue('*****@*****.**' in [d['recipient'] for d in bulk]) self.assertTrue('*****@*****.**' in [d['recipient'] for d in bulk]) self.assertTrue('Unsubscribe' in bulk[0]['message'])
def add_comment(args=None): """ args = { 'comment': '', 'comment_by': '', 'comment_by_fullname': '', 'comment_doctype': '', 'comment_docname': '', 'page_name': '', } """ import webnotes import webnotes.utils, markdown2 if not args: args = webnotes.form_dict args['comment'] = unicode(markdown2.markdown(args.get('comment') or '')) args['doctype'] = "Comment" page_name = args.get("page_name") if "page_name" in args: del args["page_name"] if "cmd" in args: del args["cmd"] comment = webnotes.bean(args) comment.ignore_permissions = True comment.insert() # since comments are embedded in the page, clear the web cache webnotes.webutils.clear_cache(page_name) args['comment_date'] = webnotes.utils.global_date_format(comment.doc.creation) template_args = { 'comment_list': [args], 'template': 'app/website/templates/html/comment.html' } # get html of comment row comment_html = webnotes.webutils.build_html(template_args) # notify commentors commentors = [d[0] for d in webnotes.conn.sql("""select comment_by from tabComment where comment_doctype='Blog Post' and comment_docname=%s and ifnull(unsubscribed, 0)=0""", args.get('comment_docname'))] blog = webnotes.doc("Blog Post", args.get("comment_docname")) blogger_profile = webnotes.conn.get_value("Blogger", blog.blogger, "profile") blogger_email = webnotes.conn.get_value("Profile", blogger_profile, "email") from webnotes.utils.email_lib.bulk import send send(recipients=list(set(commentors + [blogger_email])), doctype='Comment', email_field='comment_by', subject='New Comment on Blog: ' + blog.title, message='%(comment)s<p>By %(comment_by_fullname)s</p>' % args) return comment_html.replace("\n", "")
def send(self, query_key, doctype): recipients = self.get_recipients(query_key) sender = webnotes.utils.get_email_id(self.doc.owner) args = self.dt_map[doctype] self.send_count[doctype] = self.send_count.setdefault(doctype, 0) + len(recipients) from webnotes.utils.email_lib.bulk import send send(recipients = recipients, sender = sender, subject = self.doc.subject, message = self.get_message(), doctype = doctype, email_field = args["email_field"], first_name_field = args["first_name_field"], last_name_field = "")
def add_comment(args=None): """ args = { 'comment': '', 'comment_by': '', 'comment_by_fullname': '', 'comment_doctype': '', 'comment_docname': '', 'page_name': '', } """ import webnotes import webnotes.utils, markdown2 if not args: args = webnotes.form_dict args['comment'] = unicode(markdown2.markdown(args.get('comment') or '')) args['doctype'] = "Comment" page_name = args.get("page_name") if "page_name" in args: del args["page_name"] if "cmd" in args: del args["cmd"] comment = webnotes.bean(args) comment.ignore_permissions = True comment.insert() # since comments are embedded in the page, clear the web cache webnotes.webutils.clear_cache(page_name) args['comment_date'] = webnotes.utils.global_date_format(comment.doc.creation) template_args = { 'comment_list': [args], 'template': 'app/website/templates/html/comment.html' } # get html of comment row comment_html = webnotes.webutils.build_html(template_args) # notify commentors commentors = [d[0] for d in webnotes.conn.sql("""select comment_by from tabComment where comment_doctype='Blog Post' and comment_docname=%s and ifnull(unsubscribed, 0)=0""", args.get('comment_docname'))] blog = webnotes.conn.sql("""select * from `tabBlog Post` where name=%s""", args.get('comment_docname'), as_dict=1)[0] from webnotes.utils.email_lib.bulk import send send(recipients=list(set(commentors + [blog['owner']])), doctype='Comment', email_field='comment_by', subject='New Comment on Blog: ' + blog['title'], message='%(comment)s<p>By %(comment_by_fullname)s</p>' % args) return comment_html.replace("\n", "")
def send(self, query_key, doctype): webnotes.conn.auto_commit_on_many_writes = True recipients = self.get_recipients(query_key) sender = webnotes.utils.get_email_id(self.doc.owner) args = self.dt_map[doctype] self.send_count[doctype] = self.send_count.setdefault(doctype, 0) + len(recipients) from webnotes.utils.email_lib.bulk import send send(recipients = recipients, sender = sender, subject = self.doc.subject, message = self.get_message(), doctype = doctype, email_field = args["email_field"], first_name_field = args["first_name_field"], last_name_field = "")
def test_bulk(self): from webnotes.utils.email_lib.bulk import send send(recipients = ['*****@*****.**', '*****@*****.**'], sender="*****@*****.**", doctype='Profile', email_field='email', subject='Testing Bulk', message='This is a bulk mail!') bulk = webnotes.conn.sql("""select * from `tabBulk Email` where status='Not Sent'""", as_dict=1) self.assertEquals(len(bulk), 2) self.assertTrue('*****@*****.**' in [d['recipient'] for d in bulk]) self.assertTrue('*****@*****.**' in [d['recipient'] for d in bulk]) self.assertTrue('Unsubscribe' in bulk[0]['message'])
def send_bulk(self): self.validate_send() sender = self.doc.send_from or webnotes.utils.get_formatted_email(self.doc.owner) from webnotes.utils.email_lib.bulk import send webnotes.conn.auto_commit_on_many_writes = True send(recipients = self.recipients, sender = sender, subject = self.doc.subject, message = self.doc.message, doctype = self.send_to_doctype, email_field = "email_id") webnotes.conn.auto_commit_on_many_writes = False
def add_comment(args=None): """ args = { 'comment': '', 'comment_by': '', 'comment_by_fullname': '', 'comment_doctype': '', 'comment_docname': '', 'page_name': '', } """ import webnotes import webnotes.utils, markdown2 import webnotes.widgets.form.comments import website.web_cache if not args: args = webnotes.form_dict args['comment'] = unicode(markdown2.markdown(args.get('comment') or '')) comment = webnotes.widgets.form.comments.add_comment(args) # since comments are embedded in the page, clear the web cache website.web_cache.clear_cache(args.get('page_name'), args.get('comment_doctype'), args.get('comment_docname')) comment['comment_date'] = webnotes.utils.global_date_format(comment['creation']) template_args = { 'comment_list': [comment], 'template': 'html/comment.html' } # get html of comment row comment_html = website.web_cache.build_html(template_args) # notify commentors commentors = [d[0] for d in webnotes.conn.sql("""select comment_by from tabComment where comment_doctype='Blog' and comment_docname=%s and ifnull(unsubscribed, 0)=0""", args.get('comment_docname'))] blog = webnotes.conn.sql("""select * from tabBlog where name=%s""", args.get('comment_docname'), as_dict=1)[0] from webnotes.utils.email_lib.bulk import send send(recipients=list(set(commentors + [blog['owner']])), doctype='Comment', email_field='comment_by', first_name_field="comment_by_fullname", last_name_field="NA", subject='New Comment on Blog: ' + blog['title'], message='%(comment)s<p>By %(comment_by_fullname)s</p>' % args) return comment_html
def add_comment(args=None): """ args = { 'comment': '', 'comment_by': '', 'comment_by_fullname': '', 'comment_doctype': '', 'comment_docname': '', 'page_name': '', } """ if not args: args = webnotes.local.form_dict args['doctype'] = "Comment" page_name = args.get("page_name") if "page_name" in args: del args["page_name"] if "cmd" in args: del args["cmd"] comment = webnotes.bean(args) comment.ignore_permissions = True comment.insert() # since comments are embedded in the page, clear the web cache webnotes.webutils.clear_cache(page_name) # notify commentors commentors = [d[0] for d in webnotes.conn.sql("""select comment_by from tabComment where comment_doctype=%s and comment_docname=%s and ifnull(unsubscribed, 0)=0""", (comment.doc.comment_doctype, comment.doc.comment_docname))] owner = webnotes.conn.get_value(comment.doc.comment_doctype, comment.doc.comment_docname, "owner") from webnotes.utils.email_lib.bulk import send send(recipients=list(set(commentors + [owner])), doctype='Comment', email_field='comment_by', subject='New Comment on %s: %s' % (comment.doc.comment_doctype, comment.doc.title or comment.doc.comment_docname), message='%(comment)s<p>By %(comment_by_fullname)s</p>' % args, ref_doctype=comment.doc.comment_doctype, ref_docname=comment.doc.comment_docname) template = webnotes.get_template("lib/website/templates/includes/comment.html") return template.render({"comment": comment.doc.fields})
def send(self, query_key, doctype): webnotes.conn.auto_commit_on_many_writes = True if isinstance(query_key, basestring) and self.query_map.has_key(query_key): recipients = self.get_recipients(query_key) else: recipients = query_key sender = webnotes.utils.get_email_id(self.doc.owner) args = self.dt_map[doctype] self.send_count[doctype] = self.send_count.setdefault(doctype, 0) + \ len(recipients) from webnotes.utils.email_lib.bulk import send send(recipients = recipients, sender = sender, subject = self.doc.subject, message = self.doc.message, doctype = doctype, email_field = args["email_field"])
def test_bulk(self): from webnotes.utils.email_lib.bulk import send send( recipients=["*****@*****.**", "*****@*****.**"], doctype="Lead", email_field="email_id", subject="Testing Bulk", message="This is a bulk mail!", ) bulk = webnotes.conn.sql("""select * from `tabBulk Email` where status='Not Sent'""", as_dict=1) self.assertEquals(len(bulk), 2) self.assertTrue("*****@*****.**" in [d["recipient"] for d in bulk]) self.assertTrue("*****@*****.**" in [d["recipient"] for d in bulk]) self.assertTrue("Unsubscribe" in bulk[0]["message"])
def test_send(self, doctype="Lead"): self.validate_send() args = self.dt_map[doctype] sender = self.doc.send_from or webnotes.utils.get_formatted_email( self.doc.owner) recipients = self.doc.test_email_id.split(",") from webnotes.utils.email_lib.bulk import send send(recipients=recipients, sender=sender, subject=self.doc.subject, message=self.doc.message, doctype=doctype, email_field=args["email_field"]) webnotes.msgprint("""Scheduled to send to %s""" % self.doc.test_email_id)
def update_permission(sitemap_page, profile, perm, value): if not get_access(sitemap_page).get("admin"): raise webnotes.PermissionError permission = webnotes.bean("Website Sitemap Permission", {"website_sitemap": sitemap_page, "profile": profile}) permission.doc.fields[perm] = int(value) permission.save(ignore_permissions=True) # send email if perm=="admin" and int(value): group_title = webnotes.conn.get_value("Website Sitemap", sitemap_page, "page_title") subject = "You have been made Administrator of Group " + group_title send(recipients=[profile], subject= subject, add_unsubscribe_link=False, message="""<h3>Group Notification<h3>\ <p>%s</p>\ <p style="color: #888">This is just for your information.</p>""" % subject)
def test_unsubscribe(self): from webnotes.utils.email_lib.bulk import unsubscribe, send webnotes.local.form_dict = { 'email':'*****@*****.**', 'type':'Profile', 'email_field':'email', "from_test": True } unsubscribe() send(recipients = ['*****@*****.**', '*****@*****.**'], sender="*****@*****.**", doctype='Profile', email_field='email', subject='Testing Bulk', message='This is a bulk mail!') bulk = webnotes.conn.sql("""select * from `tabBulk Email` where status='Not Sent'""", as_dict=1) self.assertEquals(len(bulk), 1) self.assertFalse('*****@*****.**' in [d['recipient'] for d in bulk]) self.assertTrue('*****@*****.**' in [d['recipient'] for d in bulk]) self.assertTrue('Unsubscribe' in bulk[0]['message'])
def send_emails(self): """send emails to subscribers""" if self.doc.email_sent: webnotes.msgprint("""Blog Subscribers already updated""", raise_exception=1) from webnotes.utils.email_lib.bulk import send import webnotes.utils # get leads that are subscribed to the blog recipients = [e[0] for e in webnotes.conn.sql("""select distinct email_id from tabLead where ifnull(blog_subscriber,0)=1""")] # make heading as link content = '<h2><a href="%s/%s.html">%s</a></h2>\n\n%s' % (webnotes.utils.get_request_site_address(), self.doc.page_name, self.doc.title, self.doc.content) # send the blog send(recipients = recipients, doctype='Lead', email_field='email_id', subject=self.doc.title, message = content) webnotes.conn.set(self.doc, 'email_sent', 1) webnotes.msgprint("""Scheduled to send to %s subscribers""" % len(recipients))
def test_unsubscribe(self): from webnotes.utils.email_lib.bulk import unsubscribe, send webnotes.form_dict = { 'email': '*****@*****.**', 'type': 'Profile', 'email_field': 'email', "from_test": True } unsubscribe() send(recipients=['*****@*****.**', '*****@*****.**'], doctype='Profile', email_field='email', subject='Testing Bulk', message='This is a bulk mail!') bulk = webnotes.conn.sql( """select * from `tabBulk Email` where status='Not Sent'""", as_dict=1) self.assertEquals(len(bulk), 1) self.assertFalse('*****@*****.**' in [d['recipient'] for d in bulk]) self.assertTrue('*****@*****.**' in [d['recipient'] for d in bulk]) self.assertTrue('Unsubscribe' in bulk[0]['message'])
def send_email_on_reply(self): owner_fullname = get_fullname(self.doc.owner) parent_post = webnotes.bean("Post", self.doc.parent_post).doc message = self.get_reply_email_message(owner_fullname) # send email to the owner of the post, if he/she is different if parent_post.owner != self.doc.owner: send(recipients=[parent_post.owner], subject="{someone} replied to your post".format(someone=owner_fullname), message=message, # to allow unsubscribe doctype='Profile', email_field='name', # for tracking sent status ref_doctype=self.doc.doctype, ref_docname=self.doc.name) # send email to members who part of the conversation recipients = webnotes.conn.sql_list("""select owner from `tabPost` where parent_post=%s and owner not in (%s, %s)""", (self.doc.parent_post, parent_post.owner, self.doc.owner)) send(recipients=recipients, subject="{someone} replied to a post by {other}".format(someone=owner_fullname, other=get_fullname(parent_post.owner)), message=message, # to allow unsubscribe doctype='Profile', email_field='name', # for tracking sent status ref_doctype=self.doc.doctype, ref_docname=self.doc.name)
def send(self, query_key, doctype): self.validate_send() webnotes.conn.auto_commit_on_many_writes = True if isinstance(query_key, basestring) and self.query_map.has_key(query_key): recipients = self.get_recipients(query_key) else: recipients = query_key sender = self.doc.send_from or webnotes.utils.get_formatted_email( self.doc.owner) args = self.dt_map[doctype] self.send_count[doctype] = self.send_count.setdefault(doctype, 0) + \ len(recipients) from webnotes.utils.email_lib.bulk import send send(recipients=recipients, sender=sender, subject=self.doc.subject, message=self.doc.message, doctype=doctype, email_field=args["email_field"]) webnotes.conn.set(self.doc, "email_sent", 1)