Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for How to send email using SMTP server in django?
Shubham Singh Kshatriya
Shubham Singh Kshatriya

Posted on

     

How to send email using SMTP server in django?

Remember when you we signed-up to an application and received a welcome email or you once forgot your password and got an email with password reset link. This small things adds weight to User Experience.

In this article, we will see how to send emails to our users using Django framework and SMTP server. Without wasting any minute let’s jump to the interesting part.

Setting up Gmail for django mail API:

In order to use Google SMTP, you should have a gmail account. Next thing you need is to enableAllow less secure app feature under account security settings. This feature is used for securing your google account from the apps that are less secure to use it in order to prevent hackers from muddle into your account.
Alt Text

Configuring settings.py:

In your settings.py file, add the following configurations.

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'EMAIL_HOST = 'smtp.gmail.com'EMAIL_USE_TLS = TrueEMAIL_PORT = 587EMAIL_HOST_USER = 'your gmail account'EMAIL_HOST_PASSWORD = 'your account’s password'EMAIL_USE_SSL = False
Enter fullscreen modeExit fullscreen mode

Let’s understand these terminologies:

EMAIL_BACKEND:
This setting specifies the backend that we will use for sending an email in Django.

EMAIL_HOST:
This setting is to specify the email service provider.

EMAIL_USE_TLS:
This setting specifies whether the Email uses a TLS connection or not. It is True for Gmail.

EMAIL_PORT:
This is the default setting for Gmail. It is the port used by the SMTP server.

EMAIL_HOST_USER:
The name of the email account which will be used.

EMAIL_HOST_PASSWORD:
The password of the email account which will be used.

EMAIL_USE_SSL: False for Gmail.

Note: Your password is visible here so before deploying it encrypt your password or place in a file or server, where only have access.

Configuring views.py:

Django provides a module namedsend_mail, that we will use to send email to the users. There are many options available for the send_mail but for simplicity we will use only 4 fields.

subject:
It contains the subject of the email

message:
It contains the body of the email

email_from:
It is the sender’s mail address.

recipient_list:
It is the receiver’s email address.

from django.core.mail import send_mailfrom django.conf import settingsdef mail(request):    ...    subject = 'your subject'    message = 'your message'    email_from = settings.EMAIL_HOST_USER    recipient_list = ['receiver's mail address', ]    return render(request,'some_page.html')
Enter fullscreen modeExit fullscreen mode

Configuring urls.py:

urlpatterns = [    path('mail', views.mail, name ='mail'),]
Enter fullscreen modeExit fullscreen mode

With this your code is ready for sending the email to the users. All you need now is to run the server and check whether your methods are functioning properly.

That’s was it for this article. Thanks for reading.

You can connect with me onTwitter for any discussions.

Adios.

Top comments(3)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
thekaranatic profile image
Karan Kakati
CS Undergrad | Presenter | Product Enthusiast
  • Email
  • Location
    India
  • Work
    Tech Student at Not Employed Yet
  • Joined

Thanks for the information but Google has disabled this setting. What can be done now?

CollapseExpand
 
shubhamkshatriya25 profile image
Shubham Singh Kshatriya
Ideas have always excited me. The fact that we could dream of something and bring it to reality fascinates me and Software Engineering provides a window for me to do so.
  • Email
  • Location
    Dahod, Gujarat
  • Education
    Bachelor of Engineering
  • Work
    Software Engineer at Zeus Learning
  • Joined

Hi Karan,

Instead of logging in with the account password, you'll now log in using a password generated for a specific app. You may follow the following steps.

  1. Turn on 2-Step Verification in your google account.
  2. Go to generate apps password and generate a password for your app.
  3. Use this password asEMAIL_HOST_PASSWORD.

I have tested it and it works. Let me know if you still face any issue.

CollapseExpand
 
salman_ali_10903ed7e1cf75 profile image
Salman Ali
  • Joined

I am facing an issue with this, when I send the email to my email or someone who is at my home(connected to my wifi network) it sends the email. But when I send it to someone who is not on my network it doesn't show any errors but no email is recieved by them. Why is that how to solve it?

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Ideas have always excited me. The fact that we could dream of something and bring it to reality fascinates me and Software Engineering provides a window for me to do so.
  • Location
    Dahod, Gujarat
  • Education
    Bachelor of Engineering
  • Work
    Software Engineer at Zeus Learning
  • Joined

More fromShubham Singh Kshatriya

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp