简体   繁体   中英

EmailMultiAlternatives not sending BCC emails

I have a Django management command defined like this:

class Command(BaseCommand):
    help = "Sends out an email with new jobs added in a timespan of a week"

    def handle(self, *args, **options):
        time_a_week_ago = timezone.now() - timedelta(days=7)
        time_now = timezone.now()
        job_listings_added_this_week = JobListing.objects.filter(time_added__gte=time_a_week_ago, time_added__lt=time_now)
        email_subscribers = EmailSubscriber.objects.all()
        emails = []
        for subscriber in email_subscribers:
            emails.append(subscriber.email)
        msg_plain = render_to_string("employers/weekly_newsletter.txt", {"joblistings_list": job_listings_added_this_week})
        msg_html = render_to_string("employers/weekly_newsletter.html", {"joblistings_list": job_listings_added_this_week})

        msg = EmailMultiAlternatives("New jobs added this week", msg_plain, "newsletter@myjobboard.com", [],  bcc=[emails])
        msg.attach_alternative(msg_html, "text/html")
        msg.send()

        self.stdout.write(self.style.SUCCESS("Successfuly sent out the weekly email."))

I set the email content to be output to a file. When I run the management command, this is is what I get:

Content-Type: multipart/alternative;
 boundary="===============8145459042862347861=="
MIME-Version: 1.0
Subject: New jobs added this week
From: newsletter@myjobboard.com
Date: Thu, 12 Nov 2020 05:23:05 -0000
Message-ID: 
 <160512424922.55295.17004148678390675586@johns-computer.168.1.22>

--===============8145459042862347861==
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

Jobs added last week
Hey! These job listings were added last week:

    Test job 5 - click here: http://127.0.0.1:8000/12/

    Test job 6 - click here: http://127.0.0.1:8000/13/


--===============8145459042862347861==
Content-Type: text/html; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

<h1> Jobs added last week </h1>

    <p> Hey! These job listings were added last week: </p>
    <ul>
    
        <li><a href="http://127.0.0.1:8000/12/">Test job 5</a></li>
    
        <li><a href="http://127.0.0.1:8000/13/">Test job 6</a></li>
    
    </ul>


--===============8145459042862347861==--

-------------------------------------------------------------------------------

In the email header, I see no Bcc. Why is that? Why is my EmailMultiAlternatives not including the Bcc header?

Try Changing bcc=[emails] to bcc=emails

Since emails is already a list, you won't need additional square brackets

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM