1

Prior to sending mails on actual website, I'm running a small test Django local SMTP server: I have to congifure these settings in settings.py. But where do I put it?

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'localhost'
EMAIL_PORT = '1025'
EMAIL_USE_TLS = True

Then how do I proceed? Please help.

1 Answers1

2

You need to put these in your settings.py file. You can put the below settings anywhere in your settings.py file.

settings.py

...

EMAIL_HOST = 'smtp.gmail.com'  # since you are using a gmail account
EMAIL_PORT = 587  # Gmail SMTP port for TLS
EMAIL_USE_TLS = True    
...

local_settings.py

EMAIL_HOST_USER = 'your_username@gmail.com'
EMAIL_HOST_PASSWORD = 'your_gmail_password'

Then to send an email, you can use Django's send_email().

from django.core.mail import send_mail

send_mail('My Subject', 'My message', 'from@example.com',
    ['to@example.com'], fail_silently=False)

Mail is then sent using the SMTP host and port specified in the EMAIL_HOST and EMAIL_PORT settings in your settings.py file. The EMAIL_HOST_USER and EMAIL_HOST_PASSWORD settings, if set, are used to authenticate to the SMTP server, and the EMAIL_USE_TLS settings control whether a secure connection is used.

Signature of send_email():

send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=None, auth_password=None, connection=None, html_message=None)

Note: You can put your gmail account information in a local_settings.py and add this local_settings to git-ignore. Then include this local_settings in your settings.py. This way only you would be able to see your local_settings.

Rahul Gupta
  • 46,769
  • 10
  • 112
  • 126
  • is that all required? will it send a mail from the from address (from@example.com)? isnt logging in required to send mail then? –  Jul 10 '15 at 07:28
  • if i am sending mail from my gmail account, what do i give in EMAIL_HOST_USER and EMAIL_HOST_PASSWORD? –  Jul 10 '15 at 07:36
  • You need to define `EMAIL_HOST_USER` and `EMAIL_HOST_PASSWORD` for authenticating into the SMTP server defined. – Rahul Gupta Jul 10 '15 at 07:36
  • I'm sorry, but what do I define them as? And how do I start/use SMTP server? –  Jul 10 '15 at 07:39
  • Okay, thank you but what do I have in local_settings.py? And what about running SMTP server? –  Jul 10 '15 at 07:45
  • In your `local_settings`, you can put your Gmail credentials. After defining all the settings, you just need to call the `send_email` function to send mails. – Rahul Gupta Jul 10 '15 at 07:47
  • okok and calling the function directly connects to SMTP server right? –  Jul 10 '15 at 07:55
  • and 'from@example.com' and 'username@gmail.com ' are the same right? –  Jul 10 '15 at 07:57
  • Yes Django will itself create a connection with Google's SMTP server. Also, 'from@example.com' and 'username@gmail.com ' are the same. – Rahul Gupta Jul 10 '15 at 07:58