In this guide I will show you how to quickly setup an email address for your django website using tools from digitalocean and namecheap. Let's get started !
Note: that I assume that you already have a domain name registered with namecheap and a django website installed on a digitalocean dropet.
Create a private email account with namecheap
First step, go to your namecheap account and click on email (in the navbar):
Note: you can choose a trial version if you want (which is avalable during 2 months after being created).
Pick up the plan you need. For example I selected the starter plan:
To go further you need to have a domain name. Since I already got one on namecheap I click on "Use a domain name I own with namecheap":
Next select the domain name you want the email address attached with (if you have multiple domain names), click on continue:
Add to cart:
Confirm order:
and "Pay now" (don't forget to scroll down on this page and check the "I have read and agreed to all Namecheap's Terms of Service and agreements")
Next click on the red button "create mailboxes":
On the next page you will need to remember the records provided in the yellow box:
- MX mx1.privateemail.com
- MX mx2.privateemail.com
- TXT v=spf1 include ... com all
On the same page click on "create mailbox now", a pop up window will then appear asking you to create an email address (you can choose for instance contact@mydomain.com):
Log-in to your private email account
Since your create a mailbox, you can search for private email:
and enter the email address and password you just created to log in to your account:
The following interface should then appear (you can for example send an email to this address to check that it works well):
Update your DigitalOcean droplet
Now let's configure your DigitalOcean droplet. (Note: if you didn't yet route a domain name you own to your digitalocean droplet, just click on create and "Domains/DNS"). Click then on the domain name for which you just created an email box:
click on 'MX'
then enter:
- Hostname: @
- Mail providers Mail server: mx1.privateemail.com
and click on Create Record.
Repeat the same operation with
- Hostname: @
- Mail providers Mail server: mx2.privateemail.com
and click on Create Record.
Next click on TXT and enter
- Paste TXT string here: (the txt you get on namecheap i the yellow box: v=spf1 include ... com all )
- Hostname: @
and click on Create Record.
Done !
Send your first email with Django
Now we can send email with our django application.
First let's edit the settings.py file. If we want to make some test locally (in development). Just add the following line:
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
email will not be sent but will be show in the terminal.
In production add the following lines:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'mail.privateemail.com'
EMAIL_HOST_USER = 'contact@mydomain.com'
EMAIL_HOST_PASSWORD = '***********'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_USE_SSl = False
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
change 'contact@mydomain.com' and ' ' using your email box name and password.
In views.py
from django.core.mail import send_mail
and to send an email:
send_mail(
msg_title,
msg_content,
'contact@mydomain.com',
[email],
fail_silently=False)
replace msg_title, msg_content and contact@mydomain.com and enter the email address of destination.
Note: that it can be a list of email addresses
[john.doe1@gmail.com, john.doe2@gmail.com, john.doe3@gmail.com].
References
- General Private Email configuration for mail clients and mobile devices
- Namecheap Private Email records for domains with third-party DNS
- QUESTION How to set up Email on DigitalOcean Droplet?
- Sending email
- Sending Emails in Django
- Sending email from Django via hosted SMTP
- How to send emails through Django using a private domain email address?
- SMTPRecipientsRefused in Django send email