Example #1
0
def mail_moderators(subject_line='',
                    body_text='',
                    raise_on_failure=False,
                    headers=None):
    """sends email to forum moderators and admins
    """
    body_text = absolutize_urls(body_text)
    from django.db.models import Q
    from openode.models import User
    recipient_list = User.objects.filter(Q(status='m')
                                         | Q(is_superuser=True)).filter(
                                             is_active=True).values_list(
                                                 'email', flat=True)
    recipient_list = set(recipient_list)

    from_email = ''
    if hasattr(django_settings, 'DEFAULT_FROM_EMAIL'):
        from_email = django_settings.DEFAULT_FROM_EMAIL

    try:
        msg = mail.EmailMessage(subject_line,
                                body_text,
                                from_email,
                                recipient_list,
                                headers=headers or {})
        msg.content_subtype = 'html'
        msg.send()
    except smtplib.SMTPException, error:
        logging.critical(unicode(error))
        if raise_on_failure == True:
            raise exceptions.EmailNotSent(unicode(error))
Example #2
0
def send_mail(
            subject_line=None,
            body_text=None,
            from_email=django_settings.DEFAULT_FROM_EMAIL,
            recipient_list=None,
            activity_type=None,
            related_object=None,
            headers=None,
            raise_on_failure=False,
        ):
    """
    todo: remove parameters not relevant to the function
    sends email message
    logs email sending activity
    and any errors are reported as critical
    in the main log file

    related_object is not mandatory, other arguments
    are. related_object (if given, will be saved in
    the activity record)

    if raise_on_failure is True, exceptions.EmailNotSent is raised
    """
    body_text = absolutize_urls(body_text)
    try:
        assert(subject_line is not None)
        subject_line = prefix_the_subject_line(subject_line)
        msg = mail.EmailMultiAlternatives(
                        subject_line,
                        clean_html_email(body_text),
                        from_email,
                        recipient_list,
                        headers=headers
                    )
        msg.attach_alternative(body_text, "text/html")
        msg.send()
        if related_object is not None:
            assert(activity_type is not None)
    except Exception, error:
        logging.critical(unicode(error))
        if raise_on_failure == True:
            raise exceptions.EmailNotSent(unicode(error))
Example #3
0
def send_mail(
    subject_line=None,
    body_text=None,
    from_email=django_settings.DEFAULT_FROM_EMAIL,
    recipient_list=None,
    activity_type=None,
    related_object=None,
    headers=None,
    raise_on_failure=False,
):
    """
    todo: remove parameters not relevant to the function
    sends email message
    logs email sending activity
    and any errors are reported as critical
    in the main log file

    related_object is not mandatory, other arguments
    are. related_object (if given, will be saved in
    the activity record)

    if raise_on_failure is True, exceptions.EmailNotSent is raised
    """
    body_text = absolutize_urls(body_text)
    try:
        assert (subject_line is not None)
        subject_line = prefix_the_subject_line(subject_line)
        msg = mail.EmailMultiAlternatives(subject_line,
                                          clean_html_email(body_text),
                                          from_email,
                                          recipient_list,
                                          headers=headers)
        msg.attach_alternative(body_text, "text/html")
        msg.send()
        if related_object is not None:
            assert (activity_type is not None)
    except Exception, error:
        logging.critical(unicode(error))
        if raise_on_failure == True:
            raise exceptions.EmailNotSent(unicode(error))
Example #4
0
def mail_moderators(
            subject_line='',
            body_text='',
            raise_on_failure=False,
            headers=None
        ):
    """sends email to forum moderators and admins
    """
    body_text = absolutize_urls(body_text)
    from django.db.models import Q
    from openode.models import User
    recipient_list = User.objects.filter(
                    Q(status='m') | Q(is_superuser=True)
                ).filter(
                    is_active=True
                ).values_list('email', flat=True)
    recipient_list = set(recipient_list)

    from_email = ''
    if hasattr(django_settings, 'DEFAULT_FROM_EMAIL'):
        from_email = django_settings.DEFAULT_FROM_EMAIL

    try:
        msg = mail.EmailMessage(
                        subject_line,
                        body_text,
                        from_email,
                        recipient_list,
                        headers=headers or {}
                    )
        msg.content_subtype = 'html'
        msg.send()
    except smtplib.SMTPException, error:
        logging.critical(unicode(error))
        if raise_on_failure == True:
            raise exceptions.EmailNotSent(unicode(error))