Send emails via the Django framework with Mailgun


Websites today are no longer just about pushing content. It’s also about allowing potential clients and customers to interact with you. We see this on Facebook and Twitter, where customers can file complaints or praise a company. While some people will communicate with companies publicly,  the privacy (which is disputed) of email. This is why sites usually have contact forms to send messages directly to businesses. For our Django apps, it takes some code to get you running with a basic and functional contact form.

To accomplish this task, we’ll be using Mailgun and the Django-Anymail package to allow the application to send messages. On your application folder, using PyPI we can install Anymail.  On your terminal enter: pip install django-anymail.

After you’ve installed Django-Anymail, add the following code on your settings.py file:


INSTALLED_APPS = [

#

'anymail',

]

 

#Anymail settings

ANYMAIL = {

   "MAILGUN_API_KEY"'[ Your Mailgun key ]',

   "MAILGUN_SENDER_DOMAIN"'[ Your Mailgun Domain ]',

}

 

EMAIL_BACKEND = "anymail.backends.mailgun.EmailBackend"

DEFAULT_FROM_EMAIL = "[ Your default 'from' email address ]"


After we’ve worked on our settings.py file, on the application that you want the contact form on we will have to create two functions. The first will show the contact page, while the other will execute the message.

On the application, which we’ll refer to as “page” we will have to import Django’s send_mail wrapper. We’ll then create the function to show our contact page.html file, which we’ll make later. For this function, we’ll name it contact_page. Then, we’ll create the function to collect the form data, use the send_mail wrapper to send the message, and issue a confirmation message to the user that it was sent.

After a user sends the message done, the user will be redirected back to the contact page, and the session data “sendmessage” will be displayed on the contact form as our confirmation message: “Message Has Been Sent.”

Your complete views.py file should look like the following:


from django.http import HttpResponse, HttpResponseRedirect

from django.shortcuts import render, render_to_response, get_object_or_404

from django.core.mail import send_mail

 

def send_contact(request):

     name = request.POST.get("name")

     email = request.POST.get("email")

     subject = request.POST.get("subject")

     message = request.POST.get("message")

 

     send_mail("New Message From the Contact Form", message,         email, ["[ email you'll like to send the message to ]"],

  html_message="<html>You received a new message from the contact form."

      "Name:" + name + " "

      "Email Address:" + email + ""

      "Subject:" + subject + """"

       "Message:" + message + ""

   "</html>")

    request.session['sendmessage'= "Message Has Been Sent"

 

return HttpResponseRedirect('[ Your contact page url ]')

 

def contact_page(request):

    return render(request, "[ contact page template ]"




On your urls.py, it should resemble the following:

from django.conf.urls import url

 

from .views import(

   contact_page,

   send_contact,

)

 

urlpatterns = [

   #show contact page

   url(r'^contact-page/$', contact_page),

   #send the contact form

   url(r'^send-contact/$', send_contact),

]


Finally, we will need to create our contact form. Ensure that your form method attributes match the variables on our views.py file. In this case, we are using name, email, subject, and message.

<!--Your Contact Page html file-->

 

{% if request.session.sendmessage %}

<i>{{ request.session.sendmessage }}</i>

{% endif %}

 

<form class="form" action="[ The send_contact form url ]" method="POST">{% csrf_token %}

  <label>Name:</label>

  <input class="form-control" name="name" id="name">

  <label>Email:</label>

  <input class="form-control" type="email" name="email" id="email">

  <label>Subject:</label>

  <input class="form-control" name="subject" id="subject">

  <label>Message:</label>

  <textarea class="form-control" name="message" id="message"></textarea>

 

  <button class="btn btn-primary" type="submit">Send</button>

</form>


After completing the contact page html, you’ll have a basic contact form for your Django application.


Contact Form