53. Sending emails to users after finishing payment

Now, we will add functionality to send an email to users who complete their purchase.

We can either create a new Python file with a custom function for sending emails or use Django’s built-in email function. We will use Django’s send_mail function for this purpose.

For better organization, we will create a utility function named send_purchase_email inside the utility.py file and use Django’s send_mail function within it.

At the top of the utility.py file, import the send_mail function:

utility.py
from django.core.mail import send_mail

The send_mail function will utilize the configurations set in the settings.py file to send emails. You can specify parameters such as the subject, message content, sender, and recipients (which can be a list of contacts).

Next, we will create the send_purchase_email function inside the utility.py file:

utility.py
def send_purchase_email(order) :
        email = order.client.email
        subject = f"Order Approved {order.id}"
        items_summary = '\n'.join([
    f"Product: {item.itemstock.product.name}, Unitary Price: R$ {item.itemstock.product.price}, Size: {item.itemstock.size}, Quantity: {item.quantity}, Color: {item.itemstock.color.name}, Total product price: R$ {item.total_price}"
    for item in order.items
])
        body = f"""Congratulations! Your order was approved.
        Order ID: {order.id}
        FInal order price: R$ {order.total_cost}
        Order Content: 
        {items_summary}
        Total Quantity: {order.total_quantity}"""
        
        sender = "dantenavaza2005@gmail.com"
        send_mail(subject, body, sender, [email]) #? email must be in list

Here’s a breakdown of the send_purchase_email function:

  • Function Definition:

    • def send_purchase_email(order): Defines a function to send a purchase confirmation email.

  • Extract Email Address:

    • email = order.client.email: Retrieves the recipient's email address from the order's client.

  • Construct Email Subject:

    • subject = f"Order Approved {order.id}": Creates the email subject with the order ID.

  • Generate Items Summary:

    • items_summary = '\n'.join([...]): Creates a summary of items in the order.

      • Uses a list comprehension to format each item’s details.

      • Includes product name, unitary price, size, quantity, color, and total product price.

  • Create Email Body:

    • body = f"""...""": Formats the body of the email.

      • Congratulates the customer.

      • Displays the order ID, final order price, and total quantity.

      • Includes the detailed items summary.

  • Define Sender Email:

    • sender = "dantenavaza2005@gmail.com": Specifies the sender’s email address.

  • Send Email:

    • send_mail(subject, body, sender, [email]): Uses Django’s send_mail function to send the email.

      • subject: Subject of the email.

      • body: Body of the email.

      • sender: Sender’s email address.

      • [email]: Recipient's email address, provided as a list.

Before proceeding, remember to import the send_purchase_email function at the top of the views.py file alongside the other utility functions:

views.py
from .utility import filter_product, min_max_price, order_products, secure_password, send_purchase_email

Now, in the finalize_payment view function, call the send_purchase_email function, passing the order details as an argument:

views.py
#? email system
        send_purchase_email(order)
        

        if request.user.is_authenticated :
            return redirect("my_orders") #? show finished orders

Now, you can make a payment, and an email with the payment details will be sent to the email associated with the order (the one you entered if not logged in, or the email used if already authenticated on the site).

Note: To perform a test payment, ensure you are on the same tab as the Mercado Pago buyer testing account.

Last updated