Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I log all outgoing email in Django?

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.

like image 355
Mridang Agarwalla Avatar asked Sep 26 '11 08:09

Mridang Agarwalla


People also ask

How do I send multiple emails in django?

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.

How do I get mail in django?

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.

How does django send email?

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.

How do I send an email notification in django REST framework?

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.


2 Answers

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..

like image 163
hymloth Avatar answered Oct 14 '22 02:10

hymloth


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'
like image 27
Amichai Schreiber Avatar answered Oct 14 '22 03:10

Amichai Schreiber