from celery.task import task
from celery.utils.log import get_task_logger
from django.conf import settings
from django.core.mail import send_mail, mail_admins
from dimagi.utils.django.email import send_HTML_email

logger = get_task_logger(__name__)


@task(queue="email_queue",
      bind=True, default_retry_delay=15 * 60, max_retries=10, acks_late=True)
def send_mail_async(self, subject, message, from_email, recipient_list,
                    fail_silently=False, auth_user=None, auth_password=None,
                    connection=None):
    """ Call with send_mail_async.delay(*args, **kwargs)
    - sends emails in the main celery queue
    - if sending fails, retry in 15 min
    - retry a maximum of 10 times
    """
    try:
        send_mail(subject, message, from_email, recipient_list,
                  fail_silently=fail_silently, auth_user=auth_user,
                  auth_password=auth_password, connection=connection)
    except Exception as e:
        logger.error(
            "Encountered error while sending email titled %(subject)s"
            "to %(recipients)s: %(error)s" % {
                'subject': subject,
                'recipients': ', '.join(recipient_list),
                'error': e,
            })
        self.retry(exc=e)


@task(queue="email_queue",
      bind=True, default_retry_delay=15 * 60, max_retries=10, acks_late=True)
def send_html_email_async(self, subject, recipient, html_content,
                          text_content=None, cc=None,
                          email_from=settings.DEFAULT_FROM_EMAIL,
                          file_attachments=None, bcc=None, ga_track=False, ga_tracking_info=None):
    """ Call with send_HTML_email_async.delay(*args, **kwargs)
    - sends emails in the main celery queue
    - if sending fails, retry in 15 min
    - retry a maximum of 10 times
    """
    try:
        send_HTML_email(subject, recipient, html_content,
                        text_content=text_content, cc=cc, email_from=email_from,
                        file_attachments=file_attachments, bcc=bcc, ga_track=ga_track,
                        ga_tracking_info=ga_tracking_info)
    except Exception as e:
        recipient = list(recipient) if not isinstance(recipient, basestring) else [recipient]
        logger.error(
            "Encountered error while sending email titled %(subject)s"
            "to %(recipients)s: %(error)s" % {
                'subject': subject,
                'recipients': ', '.join(recipient),
                'error': e,
            })
        self.retry(exc=e)


@task(queue="email_queue",
      bind=True, default_retry_delay=15 * 60, max_retries=10, acks_late=True)
def mail_admins_async(self, subject, message, fail_silently=False, connection=None,
                      html_message=None):
    try:
        mail_admins(subject, message, fail_silently, connection, html_message)
    except Exception as e:
        logger.error(
            "Encountered error while sending email titled %(subject)s"
            "to admins: %(error)s" % {
                'subject': subject,
                'error': e,
            })
        self.retry(exc=e)

@@ -26,53 +26,8 @@
 ask%0A
-from celery.utils.log import get_task_logger%0A
 from
@@ -165,43 +165,57 @@
 ail%0A
-%0Alogger = get_task_logger(__name__)
+from dimagi.utils.logging import notify_exception
 %0A%0A%0A@
@@ -938,138 +938,125 @@
     
-logger.error(%0A            %22Encountered error while sending email titled %25(subject)s%22%0A            %22to %25(recipients)s: %25(error)s%22 %25 
+notify_exception(%0A            None,%0A            message=%22Encountered error while sending email%22,%0A            details=
 %7B%0A  
@@ -1138,21 +1138,16 @@
 ecipient
-_list
 ),%0A     
@@ -1174,32 +1174,41 @@
 e,%0A            %7D
+%0A        
 )%0A        self.r
@@ -2227,138 +2227,125 @@
     
-logger.error(%0A            %22Encountered error while sending email titled %25(subject)s%22%0A            %22to %25(recipients)s: %25(error)s%22 %25 
+notify_exception(%0A            None,%0A            message=%22Encountered error while sending email%22,%0A            details=
 %7B%0A  
@@ -2463,32 +2463,41 @@
 e,%0A            %7D
+%0A        
 )%0A        self.r
@@ -2868,34 +2868,64 @@
     
-logger.error(%0A            
+notify_exception(%0A            None,%0A            message=
 %22Enc
@@ -2964,26 +2964,18 @@
 il t
-itled %25(subject)
+o admin
 s%22
+,
 %0A   
@@ -2987,33 +2987,16 @@
     
-%22to admins: %25(error)s%22 %25 
+details=
 %7B%0A  
@@ -3070,16 +3070,25 @@
        %7D
+%0A        
 )%0A      

send async email errors to sentry