Overview
Our platform includes built-in support for sending emails. This feature enables sending transactional or notification-based emails using a flexible, programmatic interface.
🔧 Use Cases
- User registration confirmations
- Password reset links
- System alerts or status updates
-
Custom email notifications
🧱 Prerequisites
Ensure the following settings are configure:( this is configured already in Haptik)
# settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = '<your-smtp-host>'
EMAIL_PORT = 587 # or 465 for SSL
EMAIL_USE_TLS = True # or EMAIL_USE_SSL = True
EMAIL_HOST_USER = '<your-email-address>'
EMAIL_HOST_PASSWORD = '<your-email-password>'
DEFAULT_FROM_EMAIL = ‘<your-default-sender>’
✉️ Sending Email with EmailMessage in Haptik Platfrom
The platform utilizes Django's EmailMessage class to send out emails. Here's how to use it:
from django.core.mail import EmailMessage
def send_custom_email(subject, body, to_emails, from_email=None, attachments=None):
email = EmailMessage(
subject=subject,
body=body,
from_email=from_email or DEFAULT_FROM_EMAIL,
to=to_emails,
)
# Optional: Attach files
if attachments:
for attachment in attachments:
email.attach_file(attachment)
# Send the email
email.send(fail_silently=False)
✅ Example
send_custom_email(
subject="Welcome to Our Platform!",
body="Hi there,\n\nThank you for signing up. We're glad to have you!",
to_emails=["user@example.com"]
)
📂 Attachments Support
You can attach files like PDFs, CSVs, images, etc., using the attach_file() method:
python
CopyEdit
email.attach_file('/path/to/file.pdf')
🔁 HTML Emails (Optional Enhancement)
For HTML content, use Django’s EmailMultiAlternatives:
python
CopyEdit
from django.core.mail import EmailMultiAlternatives
email = EmailMultiAlternatives(
subject="HTML Email Example",
body="This is the fallback plain text version.",
from_email=DEFAULT_FROM_EMAIL,
to=["user@example.com"],
)
html_content = "<p>This is an <strong>HTML</strong> message.</p>"
email.attach_alternative(html_content, "text/html")
email.send()
📌 Notes
- Ensure the SMTP credentials are correct and have permission to send emails.
- Monitor for delivery failures or SMTP issues.
- Use environment variables to store sensitive email credentials securely.
🛡️ Security Tip
Avoid hardcoding sensitive information like passwords. Use environment variables or a secrets manager.