Exemple #1
0
def send_revolv_email(template_name,
                      context_dict,
                      recipient_list,
                      from_email=settings.EMAIL_HOST_USER,
                      cc_admins=False,
                      fail_silently=True):
    """
    Send an email rendered from a template.

    Templates for email are specified in a specific yaml file, which is
    expected to exist at settings.EMAIL_TEMPLATES_PATH. The argument
    template_name should reference a top level key of the emails yaml file, and
    the the keys 'subject' and 'body' are expected in the associated value.

    If the yaml email structure specifies an 'hmtl' key as well as a 'body'
    key, then the associated html will be sent as a multipart alternative.

    'context_dict' should be just a Python dictionary of all the context
    variables you want available in the email templates (e.g.
    {'course': course, 'awesome_url': 'www.awesome.com'}).

    'recipient_list' must be a list of email addresse strings we are sending
    to, even if there is only one email in the list.

    On the other hand, 'from_address' is just one email address string, which
    defaults to the string set in the settings file.

    :param: fail_silently will be passed into EmailMultiAlternatives.send() -
            if True, will raise an exception on error.
    :return: the return status of EmailMultiAlternatives.send()
    """
    parser = HTMLParser.HTMLParser()
    with open(settings.EMAIL_TEMPLATES_PATH, 'r') as template:
        template_data = yaml.load(template)
    context = Context(context_dict)
    subject_template = Template(template_data[template_name]['subject'])
    body_template = Template(template_data[template_name]['body'])
    if 'html' in template_data[template_name]:
        html_template = Template(template_data[template_name]['html'])
    else:
        html_template = False
    subject = parser.unescape(subject_template.render(context))
    body = parser.unescape(body_template.render(context))
    if html_template:
        html_body = html_template.render(context)
    else:
        html_body = False

    if cc_admins:
        cc_list = get_all_administrator_emails()
    else:
        cc_list = []
    email = EmailMultiAlternatives(subject=subject,
                                   body=body,
                                   from_email=from_email,
                                   to=recipient_list,
                                   cc=cc_list)
    if html_template:
        email.attach_alternative(html_body, "text/html")
    return email.send(fail_silently=fail_silently)
    def handle(self, *args, **options):
        """
        This handle function is run when the command "python manage.py monthlydonationemail"
        is typed into the command line.

        To run it on any day which is not the first day, use "python manage.py monthlydonationemail --override".

        To silence email notifications to administrators as well, add the "--silence_admin_notifications" flag.

        It iterates through all users, and sends a monthly donation email to any user who donated
        in the last month. (Specifically the last month, not the last 30 days, it it is run on 4/4,
        it'll email users who donated on 3/1 but not on 4/2.) It by default will also send an email
        notification to all administrators.

        """
        if timezone.now().day == 1 or options['override']:  # checks if the it is the first day of the month
            print "Running monthlydonationemail command on " + str(timezone.now()) + "."
            revolv_user_profiles = RevolvUserProfile.objects.all()
            num_emails_sent = 0
            for revolv_user_profile in revolv_user_profiles:
                donation_set = revolv_user_profile.payment_set.all()
                donation_set = get_last_month_donations(donation_set)
                if len(donation_set) > 0:
                    context = {}
                    user = revolv_user_profile.user
                    context['user'] = user
                    context['payments'] = donation_set
                    send_revolv_email(
                        'monthly_donation_email',
                        context, [user.email]
                    )
                    num_emails_sent = num_emails_sent + 1
            # Sends an email notification to administrators
            if not options['silence_admin_notifications']:
                context = {}
                context['emails_sent'] = num_emails_sent
                send_revolv_email(
                    'monthly_donation_email_admin_notification',
                    context, get_all_administrator_emails()
                )
        else:
            print "monthlydonationemail command was not run. Use --override to run it if it is currently not the first of the month."
Exemple #3
0
def send_revolv_email(
    template_name,
    context_dict,
    recipient_list,
    from_email=settings.EMAIL_HOST_USER,
    cc_admins=False,
    fail_silently=True
):
    """
    Send an email rendered from a template.

    Templates for email are specified in a specific yaml file, which is
    expected to exist at settings.EMAIL_TEMPLATES_PATH. The argument
    template_name should reference a top level key of the emails yaml file, and
    the the keys 'subject' and 'body' are expected in the associated value.

    If the yaml email structure specifies an 'hmtl' key as well as a 'body'
    key, then the associated html will be sent as a multipart alternative.

    'context_dict' should be just a Python dictionary of all the context
    variables you want available in the email templates (e.g.
    {'course': course, 'awesome_url': 'www.awesome.com'}).

    'recipient_list' must be a list of email addresse strings we are sending
    to, even if there is only one email in the list.

    On the other hand, 'from_address' is just one email address string, which
    defaults to the string set in the settings file.

    :param: fail_silently will be passed into EmailMultiAlternatives.send() -
            if True, will raise an exception on error.
    :return: the return status of EmailMultiAlternatives.send()
    """
    parser = HTMLParser.HTMLParser()
    with open(settings.EMAIL_TEMPLATES_PATH, 'r') as template:
        template_data = yaml.load(template)
    context = Context(context_dict)
    subject_template = Template(template_data[template_name]['subject'])
    body_template = Template(template_data[template_name]['body'])
    if 'html' in template_data[template_name]:
        html_template = Template(template_data[template_name]['html'])
    else:
        html_template = False
    subject = parser.unescape(subject_template.render(context))
    body = parser.unescape(body_template.render(context))
    if html_template:
        html_body = html_template.render(context)
    else:
        html_body = False

    if cc_admins:
        cc_list = get_all_administrator_emails()
    else:
        cc_list = []
    email = EmailMultiAlternatives(
        subject=subject,
        body=body,
        from_email=from_email,
        to=recipient_list,
        cc=cc_list
    )
    if html_template:
        email.attach_alternative(html_body, "text/html")
    return email.send(fail_silently=fail_silently)
Exemple #4
0
 def test_get_admin_emails(self):
     """Test that we can batch retrive all admin user emails."""
     admins = RevolvUserProfile.factories.admin.create_batch(3)
     emails = get_all_administrator_emails()
     for admin in admins:
         self.assertIn(admin.user.email, emails)