Python
Django
Learn how to send emails in Django using Dhesend Python SDK.
Prerequisites
To get the most out of this guide, you’ll need to:
2. Create email sending view
Add a view in views.py to handle email sending and link it in urls.py.
1from django.http import JsonResponse
2from dhesend import Dhesend, SendEmailPayload
3
4def send_email(request):
5 dhesend = Dhesend("your-apikey")
6
7 params: SendEmailPayload = {
8 "from": "Dhesend <example@domain.com>",
9 "to": ["example@dhesend.com"],
10 "subject": "Welcome to Dhesend",
11 "htmlBody": "<strong>Have a nice day!</strong>",
12 }
13
14 try:
15 response = dhesend.Email.send(params)
16 return JsonResponse(response)
17 except Exception as e:
18 return JsonResponse({
19 "error": "An error occurred while sending the email."
20 }, status=500)On this page