Configuring Django to Send Emails with mailgun
- Install the required packages:
Start by installing the requests package using pip:
$ pip install requests
Obtain your Mailgun API credentials:
- Sign up for an account on the Mailgun website.
Once you have an account, navigate to the "API Keys" section and obtain your API key.
Configure Django settings:
- Open your Django project's settings.py file.
- Add the following configuration settings:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' EMAIL_HOST = 'smtp.mailgun.org' EMAIL_PORT = 587 EMAIL_HOST_USER = 'your-mailgun-domain' EMAIL_HOST_PASSWORD = 'your-mailgun-api-key' EMAIL_USE_TLS = True DEFAULT_FROM_EMAIL = '[email protected]'
- Replace 'your-mailgun-domain' with your actual Mailgun domain name.
- Replace 'your-mailgun-api-key' with your actual Mailgun API key.
Replace '[email protected]' with the email address you want to use as the "from" address.
Test the email sending functionality:
- In your Django project, create a new file called
mailgun_test.py
. Add the following code to the file: ``` from django.core.mail import send_mail
send_mail( 'Subject', 'Message', '[email protected]', ['[email protected]'], fail_silently=False, )
`` - Replace 'Subject' with the desired subject of the email. - Replace 'Message' with the desired content of the email. - Replace '[email protected]' with the email address you configured as the "from" address. - Replace '[email protected]' with the email address of the recipient. - Save the file and run it using the command:
$ python mailgun_test.py` - Check the recipient's email inbox to verify if the email was successfully sent.Utilize Mailgun features:
- Explore the Mailgun documentation to learn about additional features such as sending HTML emails, adding attachments, and managing email lists.
Note: Make sure to replace the placeholder values ('your-mailgun-domain', 'your-mailgun-api-key', '[email protected]', and '[email protected]') with the actual values specific to your Mailgun account and email configuration.