21

How do you configure Django and Postfix to send emails for Django apps?

I am able to do it using Gmail server settings but I want to send email from my own server using my own domain.

poolie
  • 9,289
  • 1
  • 47
  • 74
toothie
  • 1,019
  • 4
  • 20
  • 47
  • 1
    If django can be configured to use an SMTP server, you could probably just install postfix and point django to localhost:25. – getWeberForStackExchange Oct 13 '14 at 04:48
  • I don't understand. Do you mean putting this in settings.py EMAIL_HOST = 'localhost' EMAIL_PORT = 25 ? – toothie Oct 13 '14 at 04:49
  • 1
    Yes, exactly. Assuming that postfix is setup correctly, I think that should work. I am not familiar with django, but judging by [their documentation](https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-EMAIL_PORT), those are the right settings. – getWeberForStackExchange Oct 13 '14 at 04:52
  • I did that already. Didn't work. – toothie Oct 13 '14 at 04:54
  • 2
    Debug the postfix mail server first. Try typing `telnet localhost 25` on the machine to see if you get a response. If you get a response that looks like `220 hostname.local ESMTP Postfix` then it's on. Type quit, and check the mail queue by typing `mailq`. This will show you if your messages aren't being delivered. The reasoning will be in the maillog, usually `/var/log/maillog`. – getWeberForStackExchange Oct 13 '14 at 05:04
  • @weberwithoneb When i did telnet localhost 587 it gave: telnet: Unable to connect to remote host: Connection refused – toothie Oct 13 '14 at 05:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/62945/discussion-between-monique-and-weberwithoneb). – toothie Oct 13 '14 at 05:10

1 Answers1

39

I banged my head a lot before realizing that it is actually quite simple:

add this to your settings.py

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'localhost'
EMAIL_PORT = 25
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''
EMAIL_USE_TLS = False
DEFAULT_FROM_EMAIL = 'Whatever <whatever@example.com>'

Also make sure that a fully qualified domain name (say mybox.example.com) is set up on your server (how),

Then you need to have these lines in your /etc/postfix/main.cf:

myhostname = mybox.example.com
mydestination = localhost.server.com, localhost, example.com

Also you have to set up proper MX record for your domain (check here) in your dns server (and in your registrar, if you handle dns lookup through you them)

Community
  • 1
  • 1
qliq
  • 11,695
  • 15
  • 54
  • 66