Skip to content

Instantly share code, notes, and snippets.

@LowerDeez
Created October 8, 2017 16:57
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save LowerDeez/9a8b30428a96c4b965d059925a7bd659 to your computer and use it in GitHub Desktop.
Save LowerDeez/9a8b30428a96c4b965d059925a7bd659 to your computer and use it in GitHub Desktop.
Django. Send mail with custom html template and context
<!DOCTYPE html>
<html>
<body>
<br>
<h2>Thank you for your order {{ order.full_name }}</h2>
<h3>Your Order ID: {{ order.short_uuid }}</h3>
<h3>Shipping Details:</h3>
{{ order.full_name }} </br>
{{ order.shipping_address.street }} <br/>
{{ order.shipping_address.city }} <br/>
{{ order.shipping_address.postcode }}
<h3>
Contact Details:
</h3>
Email: {{ order.email }} <br/>
Phone: {{ order.phone }}
<h2>Order Details:</h2>
<table>
<thead>
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Price</th>
<th>Total Price</th>
</tr>
</thead>
<tbody>
{% for item in order.order_items %}
<tr>
<td>{{ item.name }}</td>
<td>{{ item.quantity }}</td>
<td>{{ item.price }}</td>
<td>{{ item.total_price }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>
from django.core.mail import EmailMessage
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.template import Context
from django.template.loader import get_template
from app.settings import EMAIL_ADMIN
from .models.order import Order
@receiver(post_save, sender=Order)
def send_order_email_confirmation(sender, instance, **kwargs):
"""
Send email to customer with order details.
"""
order = instance
message = get_template("emails/order_conf.html").render(Context({
'order': order.get_serialized_data()
}))
mail = EmailMessage(
subject="Order confirmation",
body=message,
from_email=EMAIL_ADMIN,
to=[order.email],
reply_to=[EMAIL_ADMIN],
)
mail.content_subtype = "html"
return mail.send()
@harshhes
Copy link

Helpful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment