python
47 lines · 1 tab
Priya Sharma
Jan 2026
1 tab
from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string
from django.conf import settings
def send_welcome_email(user):
"""Send welcome email with HTML template."""
context = {
'user': user,
'site_url': settings.SITE_URL,
}
# Render both plain text and HTML
text_content = render_to_string('emails/welcome.txt', context)
html_content = render_to_string('emails/welcome.html', context)
email = EmailMultiAlternatives(
subject='Welcome to our platform!',
body=text_content,
from_email=settings.DEFAULT_FROM_EMAIL,
to=[user.email],
)
email.attach_alternative(html_content, 'text/html')
email.send()
def send_bulk_newsletter(subscriber_list, subject, template_name, context):
"""Send newsletter to multiple subscribers efficiently."""
html_content = render_to_string(f'emails/{template_name}.html', context)
text_content = render_to_string(f'emails/{template_name}.txt', context)
emails = []
for subscriber in subscriber_list:
email = EmailMultiAlternatives(
subject=subject,
body=text_content,
from_email=settings.DEFAULT_FROM_EMAIL,
to=[subscriber.email],
)
email.attach_alternative(html_content, 'text/html')
emails.append(email)
# Send all at once
from django.core.mail import get_connection
connection = get_connection()
connection.send_messages(emails)
1 file · python
Explain with highlit
I send HTML emails using Django templates for consistent branding. The EmailMultiAlternatives class supports both plain text and HTML versions. I render templates with render_to_string and context data. For transactional emails, I queue them via Celery to avoid blocking requests. I use inline CSS because many email clients strip <style> tags. Testing email templates across clients (Gmail, Outlook) is crucial. I track email opens with pixel tracking when needed. This keeps emails maintainable and reusable across the application.
Related snips
python
import os
import stat
for root, _dirs, files in os.walk('/etc'):
for name in files:
path = os.path.join(root, name)
Python security audit script for exposed risky filesystem state
python
auditing
host-security
by Kai Nakamura
1 tab
python
class Product(models.Model):
name = models.CharField(max_length=200)
slug = models.SlugField(blank=True)
price = models.DecimalField(max_digits=10, decimal_places=2)
cost = models.DecimalField(max_digits=10, decimal_places=2)
margin = models.DecimalField(max_digits=5, decimal_places=2, blank=True)
Django model signals vs overriding save
django
python
models
by Priya Sharma
2 tabs
python
from django.urls import path
from . import views
app_name = 'blog'
urlpatterns = [
Django URL namespacing and reverse lookups
django
python
urls
by Priya Sharma
3 tabs
ruby
# app/mailers/user_mailer.rb
class UserMailer < ApplicationMailer
default from: 'noreply@example.com'
def welcome_email(user)
@user = user
ActionMailer advanced patterns for transactional emails
ruby
rails
action-mailer
by Sarah Mitchell
2 tabs
python
import graphene
from graphene_django import DjangoObjectType
from blog.models import Post, Comment
class PostType(DjangoObjectType):
Django GraphQL with Graphene
django
python
graphql
by Priya Sharma
2 tabs
ruby
module EmailNormalization
extend ActiveSupport::Concern
included do
attr_accessor :soft_warnings
Soft Validation: Normalize + Validate Email
rails
activerecord
validations
by codesnips
4 tabs
Share this code
Here's the card — post it anywhere.