Python django.core.mail 模块,BadHeaderError() 实例源码

我们从Python开源项目中,提取了以下3个代码示例,用于说明如何使用django.core.mail.BadHeaderError()

项目:tumanov_castleoaks    作者:Roamdev    | 项目源码 | 文件源码
def sendtest(self, request, campaign_id):
        from premailer import Premailer
        from django.core.mail import send_mail, BadHeaderError

        receiver = request.POST.get('receiver')
        if not receiver:
            raise Http404

        try:
            campaign = self.model.objects.get(pk=campaign_id)
        except self.model.DoesNotExist:
            pass
        else:
            content = campaign.render_html(request, scheme='http://', test=True)
            content = Premailer(content, strip_important=False).transform()
            plain = campaign.render_plain(request, test=True)

            try:
                send_mail(
                    campaign.subject, plain, settings.DEFAULT_FROM_EMAIL,
                    recipient_list=[receiver],
                    html_message=content
                )
            except BadHeaderError:
                pass

        response = JsonResponse({})
        set_cookie(response, 'last_receiver', receiver)
        return response
项目:django-monkeys    作者:andrew-gardner    | 项目源码 | 文件源码
def contactView(request):
    """
    A simple view that shows the Contact form and sends an e-mail to the
    administrators when submitted.
    """
    submitSuccess = False
    if request.method == 'GET':
        form = ContactForm()
    else:
        form = ContactForm(request.POST)
        if form.is_valid():
            fromEmail = form.cleaned_data['fromEmail']
            subject = form.cleaned_data['subject']
            message = form.cleaned_data['message']
            finalMessage  = "From user : " + str(request.user.username) + "\n"
            finalMessage += "Return e-mail : " + str(fromEmail) + "\n\n"
            finalMessage += ("-" * 30) + "\n\n"
            finalMessage += message

            # Send the mail
            try:
                send_mail(subject, finalMessage, fromEmail, settings.EMAIL_CONTACT_LIST)
            except BadHeaderError:
                return HttpResponse('Invalid e-mail header found.')
            submitSuccess = True

    context = {
                  'form' : form,
                  'submitSuccess' : submitSuccess
              }
    return render(request, 'contact.html', context)
项目:grical    作者:wikical    | 项目源码 | 文件源码
def notify_users_when_wanted( event = None ):
    """ notifies users if *event* matches a filter of a user and the
    user wants to be notified for the matching filter """
    from grical.events.models import Event, Filter
    if isinstance(event, Event):
        pass
    elif isinstance(event, int) or isinstance(event, long):
        event = Event.objects.get(pk = event)
    else:
        event = Event.objects.get(pk = int(event))
    # TODO: the next code iterate throw all users but this is not workable
    # for a big number of users: implement a special data structure which
    # saves filters and can look up fast filters matching an event
    # TODO: show a diff of the changes
    users = User.objects.all()
    for user in users:
        user_filters = Filter.objects.filter( user = user ).filter(
                email = True)
        for fil in user_filters:
            if not fil.matches_event( event ):
                continue
            context = {
                'username': user.username,
                'event': event,
                'filter': fil,
                'site_name': Site.objects.get_current().name,
                'site_domain': Site.objects.get_current().domain, }
            # TODO: create the subject from a text template
            subject = _(u'filter match: ') + event.title
            # TODO: use a preferred language setting for users to send
            # emails to them in this language
            message = render_to_string( 'mail/event_notice.txt', context )
            from_email = settings.DEFAULT_FROM_EMAIL
            if subject and message and from_email and user.email:
                try:
                    if ( not settings.DEBUG ) or user.username in \
                            settings.USERNAMES_TO_MAIL_WHEN_DEBUGGING:
                        send_mail( subject, message, from_email,
                            [user.email,], fail_silently = False )
                except (BadHeaderError, SMTPConnectError), err:
                    logger.error(u'SMTP error while trying to send '
                            'notificationsemail - %s'%err)
                except:
                    logger.error(u'Unexpected error while trying to send '
                            'notifications - %s'%traceback.format_exc())
            else:
                # FIXME: do something meaningfull, e.g. error log
                pass