43 lines
1.1 KiB
Python
43 lines
1.1 KiB
Python
"""Webapp utils."""
|
|
|
|
from django.utils import timezone
|
|
from django.core.mail import send_mail, EmailMessage
|
|
from datetime import timedelta
|
|
import logging
|
|
from django.conf import settings
|
|
|
|
|
|
def month_from_now():
|
|
"""Return date one month from now."""
|
|
return timezone.now() + timedelta(days=30)
|
|
|
|
|
|
def send_email(to, subject, body):
|
|
try:
|
|
success = send_mail(
|
|
subject,
|
|
body,
|
|
settings.DEFAULT_EMAIL_FROM,
|
|
[to],
|
|
fail_silently=False,
|
|
)
|
|
if success == 0:
|
|
raise Exception('Failed to send email!')
|
|
|
|
except Exception as ex:
|
|
logging.exception('Failed to send email.')
|
|
|
|
|
|
def send_email_with_attachment(to, subject, body, attachment):
|
|
try:
|
|
email = EmailMessage(
|
|
subject, body, settings.DEFAULT_EMAIL_FROM, [to]
|
|
)
|
|
email.attach('raha.pdf', attachment, 'application/pdf;')
|
|
res = email.send()
|
|
if res == 0:
|
|
raise Exception('Failed to send email!')
|
|
|
|
except Exception as ex:
|
|
logging.exception('Failed to send email.')
|