def make_search_email(search_bookmark): msg = make_email_with_campaign(search_bookmark, 'analyse-alerts') html_email = get_template('bookmarks/email_for_searches.html') parsed_url = urlparse.urlparse(search_bookmark.dashboard_url()) if parsed_url.query: qs = '?' + parsed_url.query + '&' + msg.qs else: qs = '?' + msg.qs dashboard_uri = (settings.GRAB_HOST + parsed_url.path + qs + '#' + parsed_url.fragment) with NamedTemporaryFile(suffix='.png') as graph_file: graph = attach_image(msg, search_bookmark.dashboard_url(), graph_file.name, '#results .tab-pane.active') unsubscribe_link = settings.GRAB_HOST + reverse( 'bookmark-login', kwargs={'key': search_bookmark.user.profile.key}) html = html_email.render( context={ 'bookmark': search_bookmark, 'domain': settings.GRAB_HOST, 'graph': graph, 'dashboard_uri': mark_safe(dashboard_uri), 'unsubscribe_link': unsubscribe_link }) html = Premailer(html, cssutils_logging_level=logging.ERROR).transform() html = unescape_href(html) text = email_as_text(html) msg.body = text msg.attach_alternative(html, "text/html") msg.extra_headers['list-unsubscribe'] = "<%s>" % unsubscribe_link msg.tags = ["monthly_update", "analyse"] return msg
def finalise_email(msg, template_name, context, tags): """Set message body, add HTML alternative, and add some headers.""" template = get_template(template_name) html = template.render(context) html = Premailer(html, cssutils_logging_level=logging.ERROR).transform() html = unescape_href(html) text = email_as_text(html) msg.body = text msg.attach_alternative(html, "text/html") msg.extra_headers["list-unsubscribe"] = "<%s>" % context["unsubscribe_link"] msg.tags = ["monthly_update"] + tags return msg
def send_email_message(msg_path, recipient=None, dry_run=False): email_path = os.path.join(msg_path, "email.html") with open(email_path, "r") as body_f: body = body_f.read() intervention = get_intervention_from_path(email_path) if not intervention: return if intervention.sent: logger.info("Refusing to resend %s", intervention) return logger.info("Sending message to %s", intervention) if settings.DEBUG: # Belt-and-braces to ensure we don't accidentally send to # real users to = settings.TEST_EMAIL_TO else: to = intervention.contact.email if recipient: # Always allow overriding the test fax recipient to = recipient subject = ( "Information about your nimodipine prescribing from OpenPrescribing.net" ) msg = EmailMultiAlternatives( subject=subject, from_email=settings.DEFAULT_FROM_EMAIL, to=[to], reply_to=[settings.DEFAULT_FROM_EMAIL], ) msg = inline_images(msg, body) msg.tags = ["nimodipine"] msg.body = email_as_text(msg.alternatives[0][0]) msg.track_clicks = True if not dry_run: msg.send() intervention.sent = True intervention.save()
def make_org_email(org_bookmark, stats): msg = make_email_with_campaign(org_bookmark, 'dashboard-alerts') dashboard_uri = (settings.GRAB_HOST + org_bookmark.dashboard_url() + '?' + msg.qs) html_email = get_template('bookmarks/email_for_measures.html') with NamedTemporaryFile(suffix='.png') as getting_worse_file, \ NamedTemporaryFile(suffix='.png') as still_bad_file, \ NamedTemporaryFile(suffix='.png') as interesting_file: most_changing = stats['most_changing'] getting_worse_img = still_bad_img = interesting_img = None if most_changing['declines']: getting_worse_img = attach_image( msg, org_bookmark.dashboard_url(), getting_worse_file.name, '#' + most_changing['declines'][0][0].id) if stats['worst']: still_bad_img = attach_image(msg, org_bookmark.dashboard_url(), still_bad_file.name, '#' + stats['worst'][0].id) if stats['interesting']: interesting_img = attach_image(msg, org_bookmark.dashboard_url(), interesting_file.name, '#' + stats['interesting'][0].id) unsubscribe_link = settings.GRAB_HOST + reverse( 'bookmark-login', kwargs={'key': org_bookmark.user.profile.key}) html = html_email.render( context={ 'intro_text': getIntroText(stats, org_bookmark.org_type()), 'total_possible_savings': sum([x[1] for x in stats['top_savings']['possible_savings']]), 'has_stats': _hasStats(stats), 'domain': settings.GRAB_HOST, 'measures_count': Measure.objects.count(), 'getting_worse_image': getting_worse_img, 'still_bad_image': still_bad_img, 'interesting_image': interesting_img, 'bookmark': org_bookmark, 'dashboard_uri': mark_safe(dashboard_uri), 'qs': mark_safe(msg.qs), 'stats': stats, 'unsubscribe_link': unsubscribe_link }) html = Premailer(html, cssutils_logging_level=logging.ERROR).transform() html = unescape_href(html) text = email_as_text(html) msg.body = text msg.attach_alternative(html, "text/html") msg.extra_headers['list-unsubscribe'] = "<%s>" % unsubscribe_link msg.tags = ["monthly_update", "measures"] return msg