如何防止用户在Django中看到其他订阅者的电子邮件

xzv2uavs  于 2023-04-13  发布在  Go
关注(0)|答案(1)|浏览(106)

我创建了一个django通讯功能,用户订阅,我使用订阅的电子邮件发送通讯到这些电子邮件.现在的问题,我希望你的帮助是,当我发送通讯到订阅者的接收器可以看到其他订阅者的电子邮件.我已经尝试添加BCC,但仍然不工作. below是我的views.py

def send_newsletter(request):
    if request.method == 'POST':
        subject = request.POST['subject']
        body = request.POST['body']
        from_email = settings.EMAIL_HOST_USER
        # get all subscribers' email addresses
        subscribers = Newsletter.objects.all()
        to_email = [subscriber.email for subscriber in subscribers]
        bcc = [instance.email if instance.email != from_email else None for instance in subscribers]

        html_message = get_template('newsletter/newsletter.html').render({'message': body})

        # Create the email message object
        email = EmailMessage(
            subject,
            strip_tags(body),
            from_email,
            to_email,
            bcc,
            headers={'To': 'Undisclosed Recipients <{}>'.format(from_email)}
        )
        email.content_subtype = 'html'
        email.attach_alternative(html_message, "text/html")
        email.send()

        # Save newsletter to database
        newsletter = NewsletterText.objects.create(subject=subject, body=body, status='Published')
        newsletter.save()

        messages.success(request, 'Newsletter sent successfully')
        return redirect('blog:home')

    return render(request, 'newsletter/send_newsletter.html')

我还尝试使用未公开的收件人,如下面的代码所示

def send_newsletter(request):
    if request.method == 'POST':
        subject = request.POST['subject']
        body = request.POST['body']
        from_email = settings.EMAIL_HOST_USER
        # get all subscribers' email addresses
        subscribers = Newsletter.objects.all()
        to_email = [subscriber.email for subscriber in subscribers]
        bcc = []

        html_message = get_template('newsletter/newsletter.html').render({'message': body})

        # Create the email message object
        email = EmailMessage(
            subject,
            strip_tags(body),
            from_email,
            to_email,
            bcc,
            headers={'To': 'Undisclosed Recipients <{}>'.format(from_email)}
        )
        email.content_subtype = 'html'
        email.attach_alternative(html_message, "text/html")
        email.send()

        # Save newsletter to database
        newsletter = NewsletterText.objects.create(subject=subject, body=body, status='Published')
        newsletter.save()

        messages.success(request, 'Newsletter sent successfully')
        return redirect('blog:home')

    return render(request, 'newsletter/send_newsletter.html')

订阅者仍然会收到电子邮件,并能够看到其他订阅者的电子邮件.我希望接收者将无法看到其他订阅者的电子邮件.我在这里做错了什么.我怎么能防止一个订阅者看到其他电子邮件.我甚至试图不通过to_emailEmailMessage()谢谢

s8vozzvw

s8vozzvw1#

不向to收件人发送电子邮件,而只向bcc发送电子邮件:

to_email = [subscriber.email for subscriber in subscribers]
        bcc = [instance.email if instance.email != from_email else None for instance in subscribers]

        # Create the email message object
        email = EmailMessage(
            subject,
            strip_tags(body),
            from_email,
            None, # HERE
            bcc + to_email,  # HERE
            headers={'To': 'Undisclosed Recipients <{}>'.format(from_email)}
        )

相关问题