EmailMultiAlternatives不发送密件抄送电子邮件

时间:2020-11-12 05:31:07

标签: python django email django-manage.py django-management-command

我有这样定义的Django管理命令:

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."))

我将电子邮件内容设置为输出到文件。当我运行管理命令时,这就是我得到的:

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==--

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

在电子邮件标题中,没有看到密件抄送。这是为什么? 为什么我的EmailMultiAlternatives不包括密件抄送标题?

1 个答案:

答案 0 :(得分:0)

尝试将bcc=[emails]更改为bcc=emails

由于emails已经是列表,因此您不需要其他方括号