Configuring Django to Send Emails with mailgun

  1. Install the required packages:
  2. Start by installing the requests package using pip: $ pip install requests

  3. Obtain your Mailgun API credentials:

  4. Sign up for an account on the Mailgun website.
  5. Once you have an account, navigate to the "API Keys" section and obtain your API key.

  6. Configure Django settings:

  7. Open your Django project's settings.py file.
  8. 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]'
  9. Replace 'your-mailgun-domain' with your actual Mailgun domain name.
  10. Replace 'your-mailgun-api-key' with your actual Mailgun API key.
  11. Replace '[email protected]' with the email address you want to use as the "from" address.

  12. Test the email sending functionality:

  13. In your Django project, create a new file called mailgun_test.py.
  14. 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.

  15. Utilize Mailgun features:

  16. 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.