【发布时间】:2020-11-05 06:16:39
【问题描述】:
我在网上浏览了一天,但没有找到完美的文章,它提供了发送大量电子邮件的最佳实践。
我已使用 django-ses 配置了 Amazon SES,并且邮件发送正确。现在的问题是我不知道人们如何发送群发邮件,隐藏其他收件人,他们使用什么功能,以及他们遵循什么模式来使发送群发邮件变得高效和轻松。
此外,我们正在使用模板(Django 模板)来处理邮件,下面是我通过混合我在互联网上找到的所有最好的东西得到的最佳解决方案:
# 1. Getting queryset of all recipients which will receive mail
# (mine is a little bit different but at the end, it gives queryset of all emails - not list)
subscribers = EmailNotificationSubscriber.objects.all().values_list('user__user_email', flat=True)
# 2. Opening a connection
# 3. [Looping] using `.iterator()` to fetch email one by one from queryset (I think this is to handle the cases where we have an email list of around 10k or even bigger)
# 4. Creating EmailMessage instance and sending an email using `.send()`
# Function to get HTML Message (instance of `EmailMessage`)
def get_html_msg(subject, from_email, to, template_name, ctx, connection=None):
message = get_template(os.path.join(settings.BASE_DIR, 'templates', 'email', template_name)).render(ctx)
msg = EmailMessage(subject, message, from_email, to, connection=connection)
msg.content_subtype = 'html'
return msg
# Function which sends mass mail
def send_mass_mail(subject, qs, mail_template='base.html', ctx=None, fail_silently=True, *args, **kwargs):
from_email = settings.EMAIL_FROM
with get_email_connection() as connection:
for recipient in qs.iterator():
print(f"Sending to recipient: {recipient}")
msg = get_html_msg(subject, from_email, [recipient], mail_template, ctx, connection)
msg.send(fail_silently)
以上是我正在做的:
- 只获取电子邮件而不将其转换为列表,以便我可以使用
.iterator() - 使用单一连接发送所有邮件
- 通过逐一发送邮件来使用循环隐藏其他收件人
(我将为所有收件人使用相同的模板,因此稍后我将对其进行重构以提高性能)
那么,人们如何使用 Amazon SES 发送群发邮件?他们是否使用了其他东西?一个开源代码库或示例会很有帮助。
非常感谢
编辑 1: 删除表情符号
编辑 2: 缩小问题
【问题讨论】:
-
这是一个专业的网站;请相应地写,使用文字而不是表情符号。
-
感谢@CodyGray。我真的很抱歉,因为我不知道表情符号的使用。我已经更新了我的问题。感谢您指引我正确的方向。
-
当然,没问题。感谢更新。但是,对于 Stack Overflow,您的问题仍然过于宽泛,因为您要问 许多 不同的问题,其中一些问题本身非常宽泛(例如,第 5 点,“最佳实践”)。这就是您的问题已关闭的原因。我建议将其分解为多个问题,并尝试让它们专注于特定的编程问题。请在How to Ask 和what not to ask 上查看帮助中心的建议。
-
嗨,@CodyGray 我将再次更新问题以缩小范围。我正在检查两个指南并将更新问题。问候。
标签: python django email amazon-ses django-ses