def activation_24hr_reminder_email(): """ Reminds inactive users registered 24 hrs ago to activate their account. """ request_reminders = RegistrationRequest.get_requests_24hrs_ago() DNS_name = get_site_domain() for request in request_reminders: user = WebUser.get_by_username(request.new_user_username) registration_link = 'http://' + DNS_name + reverse( 'registration_confirm_domain') + request.activation_guid + '/' email_context = { "domain": request.domain, "registration_link": registration_link, "full_name": user.full_name, "first_name": user.first_name, 'url_prefix': '' if settings.STATIC_CDN else 'http://' + DNS_name, } message_plaintext = render_to_string( 'registration/email/confirm_account_reminder.txt', email_context) message_html = render_to_string( 'registration/email/confirm_account.html', email_context) subject = ugettext('Reminder to Activate your CommCare project') send_html_email_async.delay( subject, request.new_user_username, message_html, text_content=message_plaintext, email_from=settings.DEFAULT_FROM_EMAIL )
def send_mobile_experience_reminder(recipient, full_name): url = absolute_reverse("login") params = { "full_name": full_name, "url": url, 'url_prefix': '' if settings.STATIC_CDN else 'http://' + get_site_domain(), } message_plaintext = render_to_string( 'registration/email/mobile_signup_reminder.txt', params) message_html = render_to_string( 'registration/email/mobile_signup_reminder.html', params) subject = ugettext('Visit CommCareHQ on your computer!') try: send_html_email_async.delay(subject, recipient, message_html, text_content=message_plaintext, email_from=settings.DEFAULT_FROM_EMAIL) except Exception: logging.warning("Can't send email, but the message was:\n%s" % message_plaintext) raise
def activation_24hr_reminder_email(): """ Reminds inactive users registered 24 hrs ago to activate their account. """ request_reminders = RegistrationRequest.get_requests_24hrs_ago() DNS_name = get_site_domain() for request in request_reminders: user = WebUser.get_by_username(request.new_user_username) registration_link = 'http://' + DNS_name + reverse( 'registration_confirm_domain') + request.activation_guid + '/' email_context = { "domain": request.domain, "registration_link": registration_link, "full_name": user.full_name, "first_name": user.first_name, 'url_prefix': '' if settings.STATIC_CDN else 'http://' + DNS_name, } message_plaintext = render_to_string( 'registration/email/confirm_account_reminder.txt', email_context) message_html = render_to_string( 'registration/email/confirm_account.html', email_context) subject = ugettext('Reminder to Activate your CommCare project') send_html_email_async.delay(subject, request.new_user_username, message_html, text_content=message_plaintext, email_from=settings.DEFAULT_FROM_EMAIL)
def send_domain_registration_email(recipient, domain_name, guid, full_name, first_name): registration_link = 'http://' + get_site_domain() + reverse( 'registration_confirm_domain') + guid + '/' params = { "domain": domain_name, "pricing_link": PRICING_LINK, "registration_link": registration_link, "full_name": full_name, "first_name": first_name, "forum_link": FORUM_LINK, "wiki_link": WIKI_LINK, 'url_prefix': get_static_url_prefix(), } message_plaintext = render_to_string( 'registration/email/confirm_account.txt', params) message_html = render_to_string('registration/email/confirm_account.html', params) subject = gettext('Activate your CommCare project') try: send_html_email_async.delay(subject, recipient, message_html, text_content=message_plaintext, email_from=settings.DEFAULT_FROM_EMAIL) except Exception: logging.warning("Can't send email, but the message was:\n%s" % message_plaintext)
def send_domain_registration_email(recipient, domain_name, guid, full_name): DNS_name = get_site_domain() registration_link = 'http://' + DNS_name + reverse( 'registration_confirm_domain') + guid + '/' params = { "domain": domain_name, "pricing_link": PRICING_LINK, "registration_link": registration_link, "full_name": full_name, "users_link": USERS_LINK, "wiki_link": WIKI_LINK, 'url_prefix': '' if settings.STATIC_CDN else 'http://' + DNS_name, } message_plaintext = render_to_string( 'registration/email/confirm_account.txt', params) message_html = render_to_string('registration/email/confirm_account.html', params) subject = ugettext('Activate your CommCare project') try: send_html_email_async.delay(subject, recipient, message_html, text_content=message_plaintext, email_from=settings.DEFAULT_FROM_EMAIL, ga_track=True) except Exception: logging.warning("Can't send email, but the message was:\n%s" % message_plaintext)
def yui_crossdomain(req): x_domain = """<?xml version="1.0"?> <!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"> <cross-domain-policy> <allow-access-from domain="yui.yahooapis.com"/> <allow-access-from domain="%s"/> <site-control permitted-cross-domain-policies="master-only"/> </cross-domain-policy>""" % get_site_domain() return HttpResponse(x_domain, content_type="application/xml")
def send_mass_emails(username, real_email, subject, html, text): if real_email: recipients = [{ 'username': h['username'], 'first_name': h['first_name'] or 'CommCare User', } for h in UserES().web_users().run().hits] else: recipients = [{ 'username': username, 'first_name': 'CommCare User', }] successes = [] failures = [] for recipient in recipients: context = recipient context.update({ 'url_prefix': '' if settings.STATIC_CDN else 'http://' + get_site_domain(), }) html_template = Template(html) text_template = Template(text) text_content = render_to_string( "hqadmin/email/mass_email_base.txt", { 'email_body': text_template.render(Context(context)), }) html_content = render_to_string( "hqadmin/email/mass_email_base.html", { 'email_body': html_template.render(Context(context)), }) try: send_HTML_email(subject, recipient['username'], html_content, text_content=text_content) successes.append((recipient['username'], None)) except Exception as e: failures.append((recipient['username'], e)) message = ( "Subject: {subject},\n" "Total successes: {success_count} \n Total errors: {failure_count} \n" "".format(subject=subject, success_count=len(successes), failure_count=len(failures))) send_html_email_async("Mass email summary", username, message, text_content=message, file_attachments=[ _mass_email_attachment('successes', successes), _mass_email_attachment('failures', failures) ])
def get_location(request=None): # this is necessary, because www.commcarehq.org always uses https, # but is behind a proxy that won't necessarily look like https if hasattr(settings, "OVERRIDE_LOCATION"): return settings.OVERRIDE_LOCATION if request is None: prefix = settings.DEFAULT_PROTOCOL else: prefix = "https" if request.is_secure() else "http" return "%s://%s" % (prefix, get_site_domain())
def send_mass_emails(username, real_email, subject, html, text): if real_email: recipients = [{ 'username': h['username'], 'first_name': h['first_name'] or 'CommCare User', } for h in UserES().web_users().run().hits] else: recipients = [{ 'username': username, 'first_name': 'CommCare User', }] successes = [] failures = [] for recipient in recipients: context = recipient context.update({ 'url_prefix': '' if settings.STATIC_CDN else 'http://' + get_site_domain(), }) html_template = Template(html) text_template = Template(text) text_content = render_to_string("hqadmin/email/mass_email_base.txt", { 'email_body': text_template.render(Context(context)), }) html_content = render_to_string("hqadmin/email/mass_email_base.html", { 'email_body': html_template.render(Context(context)), }) try: send_HTML_email(subject, recipient['username'], html_content, text_content=text_content) successes.append((recipient['username'], None)) except Exception as e: failures.append((recipient['username'], e)) message = ( "Subject: {subject},\n" "Total successes: {success_count} \n Total errors: {failure_count} \n" "".format( subject=subject, success_count=len(successes), failure_count=len(failures)) ) send_html_email_async( "Mass email summary", username, message, text_content=message, file_attachments=[ _mass_email_attachment('successes', successes), _mass_email_attachment('failures', failures)] )
def send_domain_registration_email(recipient, domain_name, guid): DNS_name = get_site_domain() registration_link = 'http://' + DNS_name + reverse( 'registration_confirm_domain') + guid + '/' message_plaintext = u""" Welcome to CommCareHQ! Please click this link: {registration_link} to activate your new project. You will not be able to use your project until you have confirmed this email address. Project name: "{domain}" Username: "******" """ + REGISTRATION_EMAIL_BODY_PLAINTEXT message_html = u""" <h1>Welcome to CommCare HQ!</h1> <p>Please <a href="{registration_link}">go here to activate your new project</a>. You will not be able to use your project until you have confirmed this email address.</p> <p><strong>Project name:</strong> {domain}</p> <p><strong>Username:</strong> {username}</p> """ + REGISTRATION_EMAIL_BODY_HTML params = { "domain": domain_name, "pricing_link": PRICING_LINK, "registration_link": registration_link, "username": recipient, "users_link": USERS_LINK, "wiki_link": WIKI_LINK, } message_plaintext = message_plaintext.format(**params) message_html = message_html.format(**params) subject = 'Welcome to CommCare HQ!'.format(**locals()) try: send_html_email_async.delay(subject, recipient, message_html, text_content=message_plaintext, email_from=settings.DEFAULT_FROM_EMAIL) except Exception: logging.warning("Can't send email, but the message was:\n%s" % message_plaintext)
def _notification_email_on_publish(domain, snapshot, published_by): params = {"domain": domain, "snapshot": snapshot, "published_by": published_by, "url_base": get_site_domain()} text_content = render_to_string( "domain/email/published_app_notification.txt", params) html_content = render_to_string( "domain/email/published_app_notification.html", params) recipients = settings.EXCHANGE_NOTIFICATION_RECIPIENTS subject = "New App on Exchange: %s" % snapshot.title try: for recipient in recipients: send_html_email_async.delay(subject, recipient, html_content, text_content=text_content, email_from=settings.DEFAULT_FROM_EMAIL) except Exception: logging.warning("Can't send notification email, " "but the message was:\n%s" % text_content)
def get_idp_cert_expiration_email_context(idp): """ Utility to generate metadata and render messages for an IdP certificate expiration reminder email. :param idp: IdentityProvider :return: Dict (parameters for sending email) """ today = datetime.datetime.utcnow().date() exp_date = idp.date_idp_cert_expiration.date() num_days_left = (exp_date - today).days if num_days_left == 0: expires_on = _("today!") elif num_days_left == 1: expires_on = _("tomorrow!") else: expires_on = _(f"on {exp_date:{USER_DATE_FORMAT}}.") template_context = { "idp_name": idp.name, "expires_on": expires_on, "contact_email": settings.ACCOUNTS_EMAIL, "base_url": get_site_domain(), } subject = _( "CommCare Alert: Certificate for Identity Provider %(idp_name)s " "expires %(expires_on)s" ) % template_context body_html, body_txt = render_multiple_to_strings( template_context, "sso/email/idp_cert_expiring_reminder.html", "sso/email/idp_cert_expiring_reminder.txt", ) email_context = { "subject": subject, "from": _(f"Dimagi CommCare Accounts <{settings.ACCOUNTS_EMAIL}>"), "to": idp.owner.enterprise_admin_emails, "bcc": [settings.ACCOUNTS_EMAIL], "html": body_html, "plaintext": body_txt, } if idp.owner.dimagi_contact: email_context["bcc"].append(idp.owner.dimagi_contact) return email_context
def send_domain_registration_email(recipient, domain_name, guid, full_name): DNS_name = get_site_domain() registration_link = 'http://' + DNS_name + reverse( 'registration_confirm_domain') + guid + '/' params = { "domain": domain_name, "pricing_link": PRICING_LINK, "registration_link": registration_link, "full_name": full_name, "forum_link": FORUM_LINK, "wiki_link": WIKI_LINK, 'url_prefix': '' if settings.STATIC_CDN else 'http://' + DNS_name, "is_mobile_experience": (toggles.MOBILE_SIGNUP_REDIRECT_AB_TEST_CONTROLLER.enabled( recipient, toggles.NAMESPACE_USER) and toggles.MOBILE_SIGNUP_REDIRECT_AB_TEST.enabled( recipient, toggles.NAMESPACE_USER)) } message_plaintext = render_to_string( 'registration/email/confirm_account.txt', params) message_html = render_to_string('registration/email/confirm_account.html', params) subject = ugettext('Activate your CommCare project') try: send_html_email_async.delay(subject, recipient, message_html, text_content=message_plaintext, email_from=settings.DEFAULT_FROM_EMAIL, ga_track=True) except Exception: logging.warning("Can't send email, but the message was:\n%s" % message_plaintext)
def send_global_domain_registration_email(requesting_user, domain_name): DNS_name = get_site_domain() registration_link = 'http://' + DNS_name + reverse("domain_homepage", args=[domain_name]) message_plaintext = u""" Hello {name}, You have successfully created and activated the project "{domain}" for the CommCare HQ user "{username}". You may access your project by following this link: {registration_link} """ + REGISTRATION_EMAIL_BODY_PLAINTEXT message_html = u""" <h1>New project "{domain}" created!</h1> <p>Hello {name},</p> <p>You may now <a href="{registration_link}">visit your newly created project</a> with the CommCare HQ User <strong>{username}</strong>.</p> """ + REGISTRATION_EMAIL_BODY_HTML params = { "name": requesting_user.first_name, "domain": domain_name, "pricing_link": PRICING_LINK, "registration_link": registration_link, "username": requesting_user.email, "users_link": USERS_LINK, "wiki_link": WIKI_LINK, } message_plaintext = message_plaintext.format(**params) message_html = message_html.format(**params) subject = 'CommCare HQ: New project created!'.format(**locals()) try: send_html_email_async.delay(subject, requesting_user.email, message_html, text_content=message_plaintext, email_from=settings.DEFAULT_FROM_EMAIL) except Exception: logging.warning("Can't send email, but the message was:\n%s" % message_plaintext)
def send_domain_registration_email(recipient, domain_name, guid): DNS_name = get_site_domain() registration_link = 'http://' + DNS_name + reverse('registration_confirm_domain') + guid + '/' message_plaintext = u""" Welcome to CommCareHQ! Please click this link: {registration_link} to activate your new project. You will not be able to use your project until you have confirmed this email address. Project name: "{domain}" Username: "******" """ + REGISTRATION_EMAIL_BODY_PLAINTEXT message_html = u""" <h1>Welcome to CommCare HQ!</h1> <p>Please <a href="{registration_link}">go here to activate your new project</a>. You will not be able to use your project until you have confirmed this email address.</p> <p><strong>Project name:</strong> {domain}</p> <p><strong>Username:</strong> {username}</p> """ + REGISTRATION_EMAIL_BODY_HTML params = { "domain": domain_name, "pricing_link": PRICING_LINK, "registration_link": registration_link, "username": recipient, "users_link": USERS_LINK, "wiki_link": WIKI_LINK, } message_plaintext = message_plaintext.format(**params) message_html = message_html.format(**params) subject = 'Welcome to CommCare HQ!'.format(**locals()) try: send_html_email_async.delay(subject, recipient, message_html, text_content=message_plaintext, email_from=settings.DEFAULT_FROM_EMAIL) except Exception: logging.warning("Can't send email, but the message was:\n%s" % message_plaintext)
def send_mobile_experience_reminder(recipient, full_name): url = absolute_reverse("login") params = { "full_name": full_name, "url": url, 'url_prefix': '' if settings.STATIC_CDN else 'http://' + get_site_domain(), } message_plaintext = render_to_string( 'registration/email/mobile_signup_reminder.txt', params) message_html = render_to_string( 'registration/email/mobile_signup_reminder.html', params) subject = ugettext('Visit CommCareHQ on your computer!') try: send_html_email_async.delay(subject, recipient, message_html, text_content=message_plaintext, email_from=settings.DEFAULT_FROM_EMAIL) except Exception: logging.warning( "Can't send email, but the message was:\n%s" % message_plaintext) raise
def send_domain_registration_email(recipient, domain_name, guid, full_name, first_name): DNS_name = get_site_domain() registration_link = 'http://' + DNS_name + reverse('registration_confirm_domain') + guid + '/' params = { "domain": domain_name, "pricing_link": PRICING_LINK, "registration_link": registration_link, "full_name": full_name, "first_name": first_name, "forum_link": FORUM_LINK, "wiki_link": WIKI_LINK, 'url_prefix': '' if settings.STATIC_CDN else 'http://' + DNS_name, } message_plaintext = render_to_string('registration/email/confirm_account.txt', params) message_html = render_to_string('registration/email/confirm_account.html', params) subject = ugettext('Activate your CommCare project') try: send_html_email_async.delay(subject, recipient, message_html, text_content=message_plaintext, email_from=settings.DEFAULT_FROM_EMAIL) except Exception: logging.warning("Can't send email, but the message was:\n%s" % message_plaintext)