# Sending an Email from your Django Server with the Django Master

Sending emails from a Django server is a crucial aspect of many web applications. It allows you to communicate with users, send notifications, and perform various other tasks. In this guide, I'll walk you through the process of setting up email functionality in your Django project using SMTP and specifically, using [`smtp.gmail.com`](http://smtp.gmail.com).

To a Master like me, SMTP stands for Send Mail To Peasants. But to a peasant like you, SMTP should stand for Simple Mail Transfer Protocol. It is the standard Protocol followed by emailing systems. You will be using Gmail's SMTP server to send the mail as it doesn't require your project to be hosted.

Learn Python & Django at [django-tutorial.dev](https://django-tutorial.dev)

### **Setting up Django for Email**

#### 1\. Install Django & decouple

If you haven't already, install Django and decouple using the following commands:

```bash
pip install Django
pip install python-decouple
```

#### 2\. Setting up Variables

You will need an App Password for your gmail account to use gmail's smtp. For this:

1. Goto your Google Accounts Security settings [here](http://myaccount.google.com)
    
2. Turn on 2 Factor Authentication (2FA)
    
3. Goto App Passwords and create an app password
    
4. Copy the Password and securely store it somewhere.
    

After You have the App password, create an .env file that will have your environment variables for development stage. I have explained what this does in [this Blog.](https://blog.nischallamichhane.com.np/securing-yourself-a-masters-guide-to-fortification) This is what .env file should look like:

```plaintext
EMAIL_HOST_USER='django@master.com'
EMAIL_HOST_PASSWORD'xxxx xxxx xxxx xxxx'
```

#### 3\. Configure Email Settings

In your Django project settings ([`settings.py`](http://settings.py)), find the `EMAIL_BACKEND` setting and set it to `'django.core.mail.backends.smtp.EmailBackend'`. Also, configure the SMTP settings for Gmail:

```python
# settings.py
from decouple import config
# Email settings
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = config('EMAIL_HOST_USER')  # Your Gmail email address
EMAIL_HOST_PASSWORD = config('EMAIL_HOST_PASSWORD')  # Your Gmail email password or app password
```

Values in EMAIL\_HOST\_USER and EMAIL\_HOST\_PASSWORD will be replaced by decouple with your actual Gmail credentials from '.env' file. Note: It's recommended to use an app password for better security.

### **Writing the Django View**

Now, let's create a simple Django view that sends an email when accessed.

#### 1\. Import Required Modules

In your [`views.py`](http://views.py) file, import the necessary modules:

```python
# views.py

from django.core.mail import send_mail
from django.http import HttpResponse
```

#### 2\. Create the Email-Sending View

Add a function in your [`views.py`](http://views.py) to handle email sending:

```python
# views.py
from decouple import config
from django.core.mail import send_mail

def send_email_view(request):
    to_email = request.user.email
    subject = 'Hello from Your Django App'
    message = 'This is a test email sent from your Django app.'
    from_email = config('EMAIL_HOST_USER')  # Sender's email address
    recipient_list = [to_email]  # Recipient's email address

    send_mail(subject, message, from_email, recipient_list)

    return HttpResponse('Email sent successfully!')
```

You can also research on sending html files as the email message that will render on the receiver's end to prettify your emails.

### **URLs and Testing**

Finally, configure a URL for your new view in your [`urls.py`](http://urls.py):

```python
# urls.py
from django.urls import path
from .views import send_email_view

urlpatterns = [
    path('send_email/', send_email_view, name='send_email'),
]
```

Now, run your Django development server and navigate to [`http://localhost:8000/send_email/`](http://localhost:8000/send_email/) to trigger the email-sending view. Check the console for any errors, and if everything is set up correctly, you should see the 'Email sent successfully!' message.

### **Conclusion**

Congratulations! You've successfully set up email functionality in your Django project using Gmail's SMTP server. This is a basic example, and you can expand upon it by customizing the email content, handling attachments, or incorporating email templates.

Additionally, you may want to explore Django's built-in support for sending HTML emails and managing email templates for a more professional and customizable approach. Suffer Coding!

# This is your 5/900 Steps in becoming a Django Master jr.
