Django向数据库表中的所有用户发送电子邮件 [英] Django Send emails to all users in the database table

查看:105
本文介绍了Django向数据库表中的所有用户发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始django构建我的第一个应用程序教程,我必须向某些特殊Ocations数据库表中存储的所有用户发送电子邮件。我在Google上进行了搜索,发现了许多API,但发现很难用我的应用程序进行配置。

I have started django building my first app tutorials, i have to send email to all my users store in the database table on some special Ocations. i have searched on google and found many apis but found it very hard to configure with my app.

这是我的模型。py

class Users(models.Model):
    UserID = models.IntegerField(verbose_name='User ID',max_length=255,primary_key=True)
    UserName = models.CharField(verbose_name='User Name',max_length=254,null=True,blank=True)
    Email = models.EmailField(verbose_name='Email',max_length=254,null=True,blank=True)
    Phone = models.CharField(verbose_name='Phone Number',max_length=254,null=True,blank=True)

我想在这里有一个功能这样可以使所有用户一对一并发送电子邮件,还可以告诉您电子邮件是否已发送的状态天气。

i want to have a function here which should get all users one-by-one and send email also tells the status weather the email has been sent or not.

推荐答案

电池的答案还可以,但我会这样做:

battery's answer is ok, but i would do this way:

recievers = []
for user in Users.objects.all():
    recievers.append(user.email)

send_mail(subject, message, from_email, recievers)

这样,您仅打开一次邮件连接服务器,而不是打开每封电子邮件。

this way, you will open only once connection to mail server rather than opening for each email.

这篇关于Django向数据库表中的所有用户发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆