【发布时间】:2015-12-02 23:02:22
【问题描述】:
我正在尝试实现群发邮件。
这里是群发邮件文档:Just a link to the Django Docs
为了实现这一点,我需要创建这个元组:
datatuple = (
('Subject', 'Message.', 'from@example.com', ['john@example.com']),
('Subject', 'Message.', 'from@example.com', ['jane@example.com']),
)
我在 ORM 中查询了一些收件人的详细信息。然后我会想象会涉及一些循环,每次都会向元组添加另一个收件人。消息的所有元素都相同,除了用户名和电子邮件。
到目前为止我有:
recipients = notification.objects.all().values_list('username','email')
# this returns [(u'John', u'john@example.com'), (u'Jane', u'jane@example.com')]
for recipient in recipients:
to = recipient[1] #access the email
subject = "my big tuple loop"
dear = recipient[0] #access the name
message = "This concerns tuples!"
#### add each recipient to datatuple
send_mass_mail(datatuple)
我一直在尝试这样的事情: SO- tuple from a string and a list of strings
【问题讨论】:
-
你遇到了什么麻烦?
-
您不能向元组添加任何内容,因为元组是不可变的。请改用列表。
-
@DmitryBeransky 这不是真的。它不会导致同一个对象被更改,但元组支持增量以及整数和字符串。
-
@Ryan 你是说
datatuple += (subject, message, 'from@example.com', [to, ]) -
@Ryan BTW,也许
send_mass_mail(datatuple)行应该不缩进并退出循环