예제 #1
0
    def send_email(self):
        """Send out the email and record the subscriptions."""

        subscriptions = self.get_subscriptions()
        if not subscriptions:
            log.info("Nothing to do: List of subscriptions is empty.")
            return

        emails = dict((s.subscriber.email, s) for s in subscriptions)

        messages = []
        for (address, subscription) in emails.items():
            self.email.lang = subscription.locale
            msg = self.email.message(address)
            messages.append(msg)

        log.info("Establishing SMTP connection...")
        connection = mail.get_connection()
        connection.open()

        # We don't want to silence connection errors, but now we want to see
        # (success, failed) from send_messages).
        connection.fail_silently = True
        success, failed = connection.send_messages(messages)

        log.info("%d failed messages" % len(failed))
        log.debug([x.to for x in failed])
        log.info("%d successful messages" % len(success))

        for msg in success:
            dest = msg.to[0]
            sent = Recipient(subscriber_id=emails[dest].subscriber.id, email_id=self.email.id)
            try:
                sent.validate_unique()
            except ValidationError, e:
                # Already exists? Sending was probably forced.
                pass
            else:
                sent.save()
예제 #2
0
파일: base.py 프로젝트: fwenzel/basket
    def send_email(self):
        """Send out the email and record the recipients."""
        recipients = self.get_recipients()
        if not recipients:
            log.info('Nothing to do: List of recipients is empty.')
            return

        log.debug('Establishing SMTP connection...')
        connection = mail.get_connection()
        connection.open()

        for recipient in recipients:
            msg = mail.EmailMultiAlternatives(
                subject=self.get_subject(),
                body=self.get_text(),
                from_email=self.get_from(),
                to=(recipient.email,),
                headers=self.get_headers()
            )
            html = self.get_html()
            if html:
                msg.attach_alternative(html, 'text/html')

            try:
                log.debug('Sending email to %s' % recipient.email)
                msg.send(fail_silently=False)
            except Exception, e:
                log.warning('Sending email to %s failed: %s' % (
                    recipient.email, e))
            else:
                log.info('Email sent to %s' % recipient.email)
                sent = Recipient(subscriber=recipient, email=self.email)
                try:
                    sent.validate_unique()
                except ValidationError, e:
                    # Already exists? Sending was probably forced.
                    pass
                else: