示例#1
0
def send_email(recipient, subject, message, fail_silently):
    # A little safety net when debugging
    if settings.DEBUG:
        recipient = settings.EMAIL_ADDRESS

    note = None
    success = False
    try:
        msg = EmailMessage(subject, message, settings.EMAIL_ADDRESS, [recipient])
        # msg.content_subtype = "html"  # Main content is now text/html
        msg.send()
        success = True
    except:
        note = traceback.format_exc()
        if fail_silently:
            pass
        raise
    finally:
        member = None
        try:
            members = Member.objects.filter(user__email=recipient)
            if len(members) == 1:
                member = members[0]
        except:
            pass
        try:
            log = SentEmailLog(member=member, recipient=recipient, subject=subject, success=success)
            if note:
                log.note = note
            log.save()
        except:
            pass
示例#2
0
def send_email(recipient, subject, text_message, html_message=None, fail_silently=False):
    # Pull the user from the email address
    user = User.objects.filter(email=recipient).first()

    # A little safety net when debugging
    if settings.DEBUG:
        recipient = settings.EMAIL_ADDRESS

    # Adjust the subject if we have a prefix
    if hasattr(settings, "EMAIL_SUBJECT_PREFIX"):
        subject = settings.EMAIL_SUBJECT_PREFIX.strip() + " " + subject.strip()

    note = None
    success = False
    try:
        msg = EmailMultiAlternatives(subject, text_message, settings.EMAIL_ADDRESS, [recipient])
        if html_message:
            msg.attach_alternative(html_message, 'text/html')
        msg.send(fail_silently=False)
        success = True
    except:
        note = traceback.format_exc()
        if fail_silently:
            pass
        raise
    finally:
        if user:
            try:
                from nadine.models.core import SentEmailLog
                log = SentEmailLog(user=user, recipient=recipient, subject=subject, success=success)
                if note:
                    log.note = note
                log.save()
            except Exception as e:
                logger.error(e)