My Django application sends out quite a bit of emails and I've tried testing it thoroughly. However, for the first few months, I'd like to log all outgoing emails to ensure that everything is working smoothly. Is there a Django module that allows me to do this and makes the outgoing emails visible through the administration panel
Thanks.
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.
To receive emails in Django, it is better to use the django-mailbox development library if you need to import messages from local mailboxes, POP3, IMAP, or directly receive messages from Postfix or Exim4. While using Django-mailbox, mailbox functions as a message queue that is being gradually processed.
Mail is sent using the SMTP host and port specified in the EMAIL_HOST and EMAIL_PORT settings. The EMAIL_HOST_USER and EMAIL_HOST_PASSWORD settings, if set, are used to authenticate to the SMTP server, and the EMAIL_USE_TLS and EMAIL_USE_SSL settings control whether a secure connection is used.
Inside of the “send_mail.py”, create a function that takes in the following arguments. def send_mail(html,text='Email_body',subject='Hello word',from_email='',to_emails=[]): The next step would be to make sure that the “to_emails” argument is always a “list of emails” and not a string or any other data type.
I do not know if there exists a module that works this way, but writing a custom one is a piece of cake. Just create a separate model and every time you send an email, create a new instance ( use a custom method for email sending ). Then, link this model with the admin and bingo..
Since the OP asked about logging and not about saving to DB, here's a middleware that does that:
import django.core.mail.backends.smtp
import logging
logger = logging.getLogger(__name__) # or you could enter a specific logger name
class LoggingBackend(django.core.mail.backends.smtp.EmailBackend):
def send_messages(self, email_messages):
try:
for msg in email_messages:
logger.info(u"Sending message '%s' to recipients: %s", msg.subject, msg.to)
except:
logger.exception("Problem logging recipients, ignoring")
return super(LoggingBackend, self).send_messages(email_messages)
and then in your settings.py:
EMAIL_BACKEND = 'whereiputit.LoggingBackend'
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