How can I send Email in Django Function based view using Brevo API
Question:
I have developed a Django web application and integrated sending customize email using Brevo formally known as SendinBlue.
My settings.py
file for sending emails is configured fine, because I am able to receive Password reset email but I am unable to sending email in Function based view. I want to send email to user upon application approval but I am getting
‘User’ object has no attribute ‘swagger_types’
See below my view code where integrate the API for sending Email:
from __future__ import print_function
import sib_api_v3_sdk
from sib_api_v3_sdk.rest import ApiException
@login_required(login_url='user-login')
def approve_applicant(request, pk):
# Get Applicant id
app_id = Profile.objects.get(id=pk)
# Get Applicant's names
app_user = app_id.applicant
applicant_detail = app_id.surname
app_othername = app_id.othernames
app_phone = app_id.phone
app_email = app_id.applicant.email
# app_edu = app_id.applicant.education.qualification
# Try Check Application Submission
try:
app_id = Submitted.objects.get(applicant=app_user)
# When Applicant is Not Found
except Submitted.DoesNotExist:
# Send Message
messages.error(request, f"{applicant_detail} {app_othername} has No Submited Application")
# Redirect Back
return redirect('search-applicant')
else:
approved = Fee.objects.count()
if request.method == "POST":
# applicant = Submitted.objects.get(applicant_id=pk)
# Filter and Update scholarship Approval
Submitted.objects.filter(applicant_id=pk).update(approved='APROVED')
record_fee=Fee.objects.create(applicant=app_user, email=app_email, phone=app_phone)
record_fee.save()
# Instantiate the client with the API KEY
configuration = sib_api_v3_sdk.Configuration()
configuration.api_key['api-key']=config('API_KEY')
api_instance = sib_api_v3_sdk.TransactionalEmailsApi(sib_api_v3_sdk.ApiClient(configuration))
# Define the campaign settings
subject = "SUCCESSGANDE SCHOLARSHIP PORTAL!"
sender = {"name": "SUCCESSGANDE", "email": "[email protected]"}
replyTo = {"name": "SUCCESSGANDE", "email": "[email protected]"}
html_content = "<html><body><h1>Congratulations! Your Scholarship Application has been approved. </h1></body></html>"
to = [{"email": app_email, "name": app_user}]
params = {"parameter": "My param value", "subject": "New Subject"}
send_smtp_email = sib_api_v3_sdk.SendSmtpEmail(to=to, bcc='[email protected]', cc='[email protected]', reply_to=replyTo, headers='Testing', html_content=html_content, sender=sender, subject=subject)
try:
api_response = api_instance.send_transac_email(send_smtp_email)
print(api_response)
except ApiException as e:
print("Exception when calling SMTPApi->send_transac_email: %sn" % e)
print("Exception when calling EmailCampaignsApi->create_email_campaign: %sn" % e)
messages.success(request, f'{applicant_detail} {app_othername} Scholarship Approved successfully')
return redirect('search-applicant')
context = {
'applicant': applicant_detail,
'app_othername': app_othername,
'app_user': app_user,
'approved': approved,
}
return render(request, 'user/approval_form.html', context)
Someone should kindly share on how best I can achieve sending of email in my view using BREVO(SendinBlue) API in Django Function based view.
Answers:
The error you’re encountering, ‘User’ object has no attribute ‘swagger_types’, seems to be related to the Brevo (SendinBlue) library expecting certain attributes in the User
object, which is not part of Django’s User
model.
In your case, it looks like you’re trying to pass the app_user
(which is a User
object) as the recipient of the email. The Brevo library may be expecting additional attributes or methods on the User
object that are not present in your Django User
model.
To resolve this issue, you can try the following:
-
Pass Email and Name Directly:
Instead of passing the entire app_user
object to the to
field, extract the email and name and pass them directly:
to = [{"email": app_email, "name": f"{app_user.first_name} {app_user.last_name}"}]
Ensure that app_user
has first_name
and last_name
attributes. If not, you might need to adjust this based on your user model.
-
Pass Email as a String:
Some libraries expect the email address as a string, so you can also try passing the email directly as a string:
to = app_email
This would depend on the requirements of the Brevo library.
-
Check Documentation:
Refer to the documentation of the Brevo (SendinBlue) library to see the expected format for the recipient information in the to
field.
If the library requires specific attributes, you might need to adjust your user model or create a custom DTO (Data Transfer Object) to meet those requirements.
Here’s an example incorporating the first suggestion:
# Extract user details
to_name = f"{app_user.first_name} {app_user.last_name}"
to_email = app_email
# Define recipient
to = [{"email": to_email, "name": to_name}]
Make sure to adjust the code based on the actual attributes of your user model and the requirements of the Brevo library.
I have developed a Django web application and integrated sending customize email using Brevo formally known as SendinBlue.
My settings.py
file for sending emails is configured fine, because I am able to receive Password reset email but I am unable to sending email in Function based view. I want to send email to user upon application approval but I am getting
‘User’ object has no attribute ‘swagger_types’
See below my view code where integrate the API for sending Email:
from __future__ import print_function
import sib_api_v3_sdk
from sib_api_v3_sdk.rest import ApiException
@login_required(login_url='user-login')
def approve_applicant(request, pk):
# Get Applicant id
app_id = Profile.objects.get(id=pk)
# Get Applicant's names
app_user = app_id.applicant
applicant_detail = app_id.surname
app_othername = app_id.othernames
app_phone = app_id.phone
app_email = app_id.applicant.email
# app_edu = app_id.applicant.education.qualification
# Try Check Application Submission
try:
app_id = Submitted.objects.get(applicant=app_user)
# When Applicant is Not Found
except Submitted.DoesNotExist:
# Send Message
messages.error(request, f"{applicant_detail} {app_othername} has No Submited Application")
# Redirect Back
return redirect('search-applicant')
else:
approved = Fee.objects.count()
if request.method == "POST":
# applicant = Submitted.objects.get(applicant_id=pk)
# Filter and Update scholarship Approval
Submitted.objects.filter(applicant_id=pk).update(approved='APROVED')
record_fee=Fee.objects.create(applicant=app_user, email=app_email, phone=app_phone)
record_fee.save()
# Instantiate the client with the API KEY
configuration = sib_api_v3_sdk.Configuration()
configuration.api_key['api-key']=config('API_KEY')
api_instance = sib_api_v3_sdk.TransactionalEmailsApi(sib_api_v3_sdk.ApiClient(configuration))
# Define the campaign settings
subject = "SUCCESSGANDE SCHOLARSHIP PORTAL!"
sender = {"name": "SUCCESSGANDE", "email": "[email protected]"}
replyTo = {"name": "SUCCESSGANDE", "email": "[email protected]"}
html_content = "<html><body><h1>Congratulations! Your Scholarship Application has been approved. </h1></body></html>"
to = [{"email": app_email, "name": app_user}]
params = {"parameter": "My param value", "subject": "New Subject"}
send_smtp_email = sib_api_v3_sdk.SendSmtpEmail(to=to, bcc='[email protected]', cc='[email protected]', reply_to=replyTo, headers='Testing', html_content=html_content, sender=sender, subject=subject)
try:
api_response = api_instance.send_transac_email(send_smtp_email)
print(api_response)
except ApiException as e:
print("Exception when calling SMTPApi->send_transac_email: %sn" % e)
print("Exception when calling EmailCampaignsApi->create_email_campaign: %sn" % e)
messages.success(request, f'{applicant_detail} {app_othername} Scholarship Approved successfully')
return redirect('search-applicant')
context = {
'applicant': applicant_detail,
'app_othername': app_othername,
'app_user': app_user,
'approved': approved,
}
return render(request, 'user/approval_form.html', context)
Someone should kindly share on how best I can achieve sending of email in my view using BREVO(SendinBlue) API in Django Function based view.
The error you’re encountering, ‘User’ object has no attribute ‘swagger_types’, seems to be related to the Brevo (SendinBlue) library expecting certain attributes in the User
object, which is not part of Django’s User
model.
In your case, it looks like you’re trying to pass the app_user
(which is a User
object) as the recipient of the email. The Brevo library may be expecting additional attributes or methods on the User
object that are not present in your Django User
model.
To resolve this issue, you can try the following:
-
Pass Email and Name Directly:
Instead of passing the entireapp_user
object to theto
field, extract the email and name and pass them directly:to = [{"email": app_email, "name": f"{app_user.first_name} {app_user.last_name}"}]
Ensure that
app_user
hasfirst_name
andlast_name
attributes. If not, you might need to adjust this based on your user model. -
Pass Email as a String:
Some libraries expect the email address as a string, so you can also try passing the email directly as a string:to = app_email
This would depend on the requirements of the Brevo library.
-
Check Documentation:
Refer to the documentation of the Brevo (SendinBlue) library to see the expected format for the recipient information in theto
field.If the library requires specific attributes, you might need to adjust your user model or create a custom DTO (Data Transfer Object) to meet those requirements.
Here’s an example incorporating the first suggestion:
# Extract user details
to_name = f"{app_user.first_name} {app_user.last_name}"
to_email = app_email
# Define recipient
to = [{"email": to_email, "name": to_name}]
Make sure to adjust the code based on the actual attributes of your user model and the requirements of the Brevo library.