How can I send an email in a Django function-based view using the Brevo API?

348 Views Asked by At

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 a password reset email, but I am unable to sending email in a 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 my view code below where I 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: %s\n" % e)
                print("Exception when calling EmailCampaignsApi->create_email_campaign: %s\n" % 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)

How can I best achieve sending email in my view using the BREVO (SendinBlue) API in a Django function-based view?

0

There are 0 best solutions below