51 lines
1.4 KiB
Python
51 lines
1.4 KiB
Python
import logging
|
|
from django.core.mail import send_mail, EmailMultiAlternatives
|
|
from django.conf import settings
|
|
from django.utils.html import strip_tags
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
def send_campaign_email(campaign, recipients):
|
|
"""
|
|
Sends a campaign email to a list of recipients.
|
|
"""
|
|
subject = campaign.subject
|
|
html_content = campaign.body
|
|
text_content = strip_tags(html_content)
|
|
from_email = settings.DEFAULT_FROM_EMAIL
|
|
|
|
success_count = 0
|
|
fail_count = 0
|
|
|
|
for recipient in recipients:
|
|
try:
|
|
msg = EmailMultiAlternatives(subject, text_content, from_email, [recipient.email])
|
|
msg.attach_alternative(html_content, "text/html")
|
|
msg.send()
|
|
success_count += 1
|
|
except Exception as e:
|
|
logger.error(f"Failed to send email to {recipient.email}: {e}")
|
|
fail_count += 1
|
|
|
|
return success_count, fail_count
|
|
|
|
def send_contact_message(name, email, message):
|
|
"""
|
|
Simple wrapper for contact form emails.
|
|
"""
|
|
subject = f"New Contact Message from {name}"
|
|
body = f"From: {name} <{email}>\n\nMessage:\n{message}"
|
|
|
|
try:
|
|
send_mail(
|
|
subject,
|
|
body,
|
|
settings.DEFAULT_FROM_EMAIL,
|
|
settings.CONTACT_EMAIL_TO,
|
|
fail_silently=False,
|
|
)
|
|
return True
|
|
except Exception as e:
|
|
logger.error(f"Failed to send contact message: {e}")
|
|
return False
|