Quantcast
Channel: JS Tricks » Python
Viewing all articles
Browse latest Browse all 25

Send email using Django and Gmail SMTP

$
0
0

Hello, in this tutorial we will discuss about a very important feature of Django. Yes, in this post we will show you how to Send email using Django and Gmail’s SMTP server.

How to send email using Django

To send email using Django is very easy. You can send email using a few lines of code. Here is a simple example:

from django.core.mail import send_mail

send_mail('Put your Email subject here', 'Put your Email message here.', 'sender_email@example.com', ['recipient_email@example.com'], fail_silently=False)

You must replace sender_email@example.com with the email address of your account and  recipient_email@example.com with the recipient’s email address.

Send email using Django and Gmail SMTP

Send email using Django and Gmail SMTP

Configuring Django for Gmail’s SMTP server

To send email using Django and Gmail SMTP you must have a Gmail account. Your Gmail account’s Username and Password will be needed for sending emails.

If you have that then you can configure Django for your Gmail account by adding the following lines in your Django project’s  settings.py file:

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'your_gmail_username'
EMAIL_HOST_PASSWORD = 'your_gmail_password'
EMAIL_PORT = 587
EMAIL_USE_TLS = True

Here, EMAIL_HOST_USER will be your Gmail account’s Username and EMAIL_HOST_PASSWORD will be your Gmail account’s Password.

Thats all. And now you can Send email with Django. Run the following function through your configured Django project:

from django.core.mail import send_mail

send_mail('Your Email subject', 'Your Email message.', 'sender_email@example.com', ['recipient_email@example.com'], fail_silently=False)

If you have configured your Django project properly and entered valid email address for your recipient then your email will be delivered successfully.

The post Send email using Django and Gmail SMTP appeared first on JS Tricks.


Viewing all articles
Browse latest Browse all 25

Trending Articles