Django Send emails to all users in the database table
Matthew Barrera
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.
here is my model.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.
3 Answers
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.
Sending email is very simple.
For your Users model this would be:
for user in Users.objects.all(): send_mail(subject, message, from_email, user.Email)Checkout Django's Documentation on send emails for more details.
Would be useful if you mention what problem you are facing if you've tried this.
Also note, IntegerField does not have a max_length argument.
for user in Users.objects.all(): send_mail(subject, message, from_email, user.Email)
This is the best solutions and it works well
0