Testing email sending without a mail server

I have a Django application that sends an email. The production server has a mail server, but my local mailbox does not. I would like to be able to check email sending locally. Is there a way that django does not send it through an email server and just print the file or console?

+66
django email mail-server
Jan 09 '11 at 21:40
source share
6 answers

configure the application to use Console to send email. It sends emails by standard instead of sending them.

Change settings.py to include this line:

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' 

Remember to remove it for production.

+126
Jan 09 '11 at 10:15
source share

Python has a small SMTP server built-in . You can run it in the second console with this command:

 python -m smtpd -n -c DebuggingServer localhost:1025 

This will simply print all the emails sent to localhost:1025 in the console.

You need to configure Django to use this server in settings.py :

 EMAIL_HOST = 'localhost' EMAIL_PORT = 1025 
+75
Jan 09 2018-11-11T00:
source share

You can configure the application to write emails to temporary files instead of sending them (similar to Daniel Hepper's answers).

 EMAIL_BACKEND = 'django.core.mail.backends.filebased.EmailBackend' EMAIL_FILE_PATH = 'tmp/email-messages/' 

This saves each new message as a separate file. Useful if you send heaps of letters and do not want to use scrolling.

+39
Jan 09 2018-11-11T00:
source share

If your tests pass from django.test.testcases.TestCase, then nothing should be done. Django will replace EmailBackend with "special". Then you can check what was sent as follows:

 def testMethodThatSendAEmail(self): ... from django.core import mail object.method_that_send_email(to='me@example.com') self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].to, ['me@example.com']) ...#etc 

The source object is a special object that is injected into the mail when the python manage.py test runs.

+32
Jan 12 2018-11-12T00:
source share

There is a cool caktus app for this https://github.com/caktus/django-email-bandit Just add this to your settings.py file:

 EMAIL_BACKEND = 'bandit.backends.smtp.HijackSMTPBackend' BANDIT_EMAIL = 'your_email@example.com' 

In addition to email settings. All emails will be redirected to 'your_email@example.com'

Happy coding ...

+11
Oct 17 '13 at 12:22
source share

This clarifies Benjamin's answer. One way to check email if I don't have a local mail server like postfix, sendmail or exim is to start the python mail server. You can run it on port 25 using sudo or just use port> 1024 (reserved ports):

 python -m smtpd -n -c DebuggingServer localhost:1025 #sudo python -m smtpd -n -c DebuggingServer localhost:25 

To test using your current django application code, you can temporarily change settings.py to include it in the bot:

 EMAIL_HOST, EMAIL_PORT, EMAIL_HOST_USER, EMAIL_HOST_PASSWORD = 'localhost', 1025, None, None 

Now test your emails, or you can do it in a shell. /manage.py in another terminal window, for example:

 python manage.py shell 

And paste this code to send an email:

 from django.core.mail import send_mail​ send_mail('Subject here', 'Here is the message.', 'messanger@localhost.com',['any@email.com'], fail_silently=False) 

No need to use real emails, as you will see everything in your terminal. You can dump it into the appropriate container, for example .html, for further testing.

+10
Nov 13 '14 at 17:12
source share



All Articles