I want to send an email to users from django admin panel and store the sent mail in the database too. I have two usertypes: 1.Staff 2. Students. When I select staff and give email, it'll send an email to all the staff who are all having "usertype=staff" in User model and vice versa. I found some difficulties to send the mail from the admin panel. Please some one give me an idea.
models.py
username = models.CharField()
first_name = models.CharField()
last_name = models.CharField()
email = models.EmailField()
password = models.CharField()
companyname=models.CharField()
usertype=models.CharField()
is_staff = models.BooleanField()
is_active = models.BooleanField()
is_superuser = models.BooleanField()
last_login = models.DateTimeField()
date_joined = models.DateTimeField()
groups = models.ManyToManyField()
user_permissions = models.ManyToManyField()
class newsletter(models.Model):
USERTYPES = (
('staff', 'staff'),
('student', 'student'),
)
usertype=models.CharField(max_length=50,choices=USERTYPES)
subject=models.CharField(max_length=100)
message=models.TextField(blank=True)
sentdate=models.DateTimeField(auto_now = True)
admin.py
admin.site.register(newsletter)
Sending Emails with the Django ShellWe import the Django send_mail function. Then we import the settings object which contains all the global settings and the per-site settings (those inside the settings.py file). Finally, we pass all the needed arguments to the send_mail function.
send_mail() method is imported from django. The send_mail() method handles the transmission of Emails. There are some important parameters of send_mail(), which we have defined here: subject: Contains the subject part of the Email. message: Contains the Email body.
To add a message, call: from django. contrib import messages messages. add_message(request, messages.INFO, 'Hello world.
How to send multiple mass emails django. We need to create a Tuple of messages and send them using send mass mail. In this tutorial, we create a project which sends email using Django. We fill the data in the form and send it using Django Email.
Perhaps an admin action would be suitable.
See also: sending email in Django.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With