def send_email(self):
        message = self.trigger.message
        context_dict = {'volunteer': self.volunteer,
                        'assignment': self.assignment}

        body = message.rendered_body(context_dict)

        email_params = {
            'subject': message.rendered_subject(context_dict),
            'to': [self.volunteer.email_address],
            'from_email': settings.FROM_ADDRESS,
        }

        if message.body_is_html:
            email = EmailMultiAlternatives(**email_params)
            email.attach_alternative(body, "text/html")
            email.auto_text = True
        else:
            email = EmailMessage(**email_params)
            email.body = body
            email.auto_html = True

        name_tag = ("name - %s" % message.name)[:50]
        trigger_tag = ("trigger - %s" % self.trigger.id)[:50]
        email.tags = [name_tag, trigger_tag]
        logger.info("Sending %s" % email_params)
        print("Sending %s" % email_params)
        try:
            email.send(fail_silently=False)
            self.sent_date = date.today()
        except MandrillAPIError:
            print("FAILED %s" % email_params)
            self.send_failed = True
        self.save()
        return not self.send_failed
Beispiel #2
0
    def send_email(self, verbose=False):
        # Return whether or not the sendable was sent.

        message = self.trigger.message
        body = self.email_body()

        email_params = {
            'subject': message.rendered_subject(self._email_context_dict()),
            'to': [self.volunteer.email_address],
            'bcc': self.trigger.bcc(),
            'from_email': self.trigger.campaign.from_address,
        }

        if message.body_is_html:
            email = EmailMultiAlternatives(**email_params)
            email.attach_alternative(body, "text/html")
            email.auto_text = True
        else:
            email = EmailMessage(**email_params)
            email.body = body
            email.auto_html = True

        # Tags are a mandril feature.
        name_tag = ("name - %s" % message.name)[:50]
        trigger_tag = ("trigger - %s" % self.trigger.id)[:50]
        email.tags = [name_tag, trigger_tag]
        logger.info("Sending %s" % email_params)
        if verbose:
            print("Sending %s" % email_params)
        try:
            email.send(fail_silently=False)
            self.sent_date = date.today()
        except MandrillAPIError:
            print("FAILED %s" % email_params)
            self.send_failed = True
        self.save()
        return not self.send_failed
Beispiel #3
0
def send_digest(team_id, for_date, for_project_managers=False):
    """
    Sends digest for the given date to all active members and silent
    recipients of the team.

    Arguments:
        `team`: `Team` object
        `for_date`: A `datetime.datetime` instance in UTC
        `for_project_managers`: Boolean; whether to send only to Project Manager members
    """

    # TODO: create decorator for this repeating pattern: try...except
    try:
        team = Team.objects.get(id=team_id, is_active=True)
    except Team.DoesNotExist:
        logger.exception(
            "Active team with %s ID does not exist." % team_id)
        return

    team_updates = team.get_updates(for_date)

    if team_updates:
        ph_tz = pytz.timezone('Asia/Manila')
        update_for_date = for_date.astimezone(ph_tz).strftime('%a, %b %d %Y')
        context = {
            'members_and_updates': team.get_updates(for_date),
            'team': team,
            'date': update_for_date,
            'domain': get_domain_name(),
        }
        text_body = render_to_string('updates/emails/digest.txt', context)
        html_body = render_to_string('updates/emails/digest.html', context)

        # Prepare email
        from_email = 'Digestus Digest <{email}>'.format(email=team.email)
        subject = 'Digest for {team} for {date}'.format(team=team.name,
                                                        date=update_for_date)
        msg = EmailMultiAlternatives(
            subject=subject,
            body=text_body,
            from_email=from_email,
            to=team.get_recipients(for_project_managers),
        )
        msg.auto_text = True
        msg.preserve_recipients = True
        msg.auto_text = False
        msg.auto_html = False
        msg.attach_alternative(html_body, 'text/html')
        msg.content_subtype = 'html'
        msg.subaccount = team.subaccount_id

        try:
            msg.send()
        except Exception as e:
            logger.exception(
                'Digest sending failed for team with ID: %s. Retrying in 5 minutes.' % team_id)
            send_digest.retry(
                args=[team_id, for_date, for_project_managers],
                exc=e,
                countdown=300,
                max_retries=5,
            )
    else:
        error_msg = 'Team %s has no active members. Sending of digest aborted.' % team.name
        logger.error(error_msg)