import logging

from django.core import mail

from .tasks import task_email_user

logger = logging.getLogger(__name__)


def send_email(user, template, context, attachments=None, delete_attachments_after_send=False,
               language_code=None):
    '''
    Send email to user

    :param user: User instance or recipient email addres
    :param template: Template to use for email
    :param context: Context for email
    :param attachments: List of attachments
    :param delete_attachments_after_send: If true, delete attachments from storage after sending
    :param language_code: Language code for template
    :return:
    '''

    ### check if we are using test framework
    if hasattr(mail, 'outbox'):
        ### if yes, do not defer sending email
        send_email_f = task_email_user
    else:
        ### otherwise, defer sending email to celery
        send_email_f = task_email_user.delay

    ### send email
    send_email_f(
        user.pk if user else None,
        template,
        context,
        attachments=attachments,
        delete_attachments_after_send=delete_attachments_after_send,
        language_code=language_code
    )

import logging

from django.core import mail

from .tasks import task_email_user

logger = logging.getLogger(__name__)


def send_email(user, template, context, attachments=None, delete_attachments_after_send=False,
               language_code=None):
    '''
    Send email to user

    :param user: User instance or recipient email addres
    :param template: Template to use for email
    :param context: Context for email
    :param attachments: List of attachments
    :param delete_attachments_after_send: If true, delete attachments from storage after sending
    :param language_code: Language code for template
    :return:
    '''

    ### check if we are using test framework
    if hasattr(mail, 'outbox'):
        ### if yes, do not defer sending email
        send_email_f = task_email_user
    else:
        ### otherwise, defer sending email to celery
        send_email_f = task_email_user.delay

    try:
        user = user.pk
    except AttributeError:
        pass

    ### send email
    send_email_f(
        user,
        template,
        context,
        attachments=attachments,
        delete_attachments_after_send=delete_attachments_after_send,
        language_code=language_code
    )

Fix AttributeError when supplying email address as user
Fix AttributeError when supplying email address as user

  import logging
  
  from django.core import mail
  
  from .tasks import task_email_user
  
  logger = logging.getLogger(__name__)
  
  
  def send_email(user, template, context, attachments=None, delete_attachments_after_send=False,
                 language_code=None):
      '''
      Send email to user
  
      :param user: User instance or recipient email addres
      :param template: Template to use for email
      :param context: Context for email
      :param attachments: List of attachments
      :param delete_attachments_after_send: If true, delete attachments from storage after sending
      :param language_code: Language code for template
      :return:
      '''
  
      ### check if we are using test framework
      if hasattr(mail, 'outbox'):
          ### if yes, do not defer sending email
          send_email_f = task_email_user
      else:
          ### otherwise, defer sending email to celery
          send_email_f = task_email_user.delay
  
+     try:
+         user = user.pk
+     except AttributeError:
+         pass
+ 
      ### send email
      send_email_f(
-         user.pk if user else None,
+         user,
          template,
          context,
          attachments=attachments,
          delete_attachments_after_send=delete_attachments_after_send,
          language_code=language_code
      )
  
Fix AttributeError when supplying email address as user
## Code Before:
import logging

from django.core import mail

from .tasks import task_email_user

logger = logging.getLogger(__name__)


def send_email(user, template, context, attachments=None, delete_attachments_after_send=False,
               language_code=None):
    '''
    Send email to user

    :param user: User instance or recipient email addres
    :param template: Template to use for email
    :param context: Context for email
    :param attachments: List of attachments
    :param delete_attachments_after_send: If true, delete attachments from storage after sending
    :param language_code: Language code for template
    :return:
    '''

    ### check if we are using test framework
    if hasattr(mail, 'outbox'):
        ### if yes, do not defer sending email
        send_email_f = task_email_user
    else:
        ### otherwise, defer sending email to celery
        send_email_f = task_email_user.delay

    ### send email
    send_email_f(
        user.pk if user else None,
        template,
        context,
        attachments=attachments,
        delete_attachments_after_send=delete_attachments_after_send,
        language_code=language_code
    )

## Instruction:
Fix AttributeError when supplying email address as user
## Code After:
import logging

from django.core import mail

from .tasks import task_email_user

logger = logging.getLogger(__name__)


def send_email(user, template, context, attachments=None, delete_attachments_after_send=False,
               language_code=None):
    '''
    Send email to user

    :param user: User instance or recipient email addres
    :param template: Template to use for email
    :param context: Context for email
    :param attachments: List of attachments
    :param delete_attachments_after_send: If true, delete attachments from storage after sending
    :param language_code: Language code for template
    :return:
    '''

    ### check if we are using test framework
    if hasattr(mail, 'outbox'):
        ### if yes, do not defer sending email
        send_email_f = task_email_user
    else:
        ### otherwise, defer sending email to celery
        send_email_f = task_email_user.delay

    try:
        user = user.pk
    except AttributeError:
        pass

    ### send email
    send_email_f(
        user,
        template,
        context,
        attachments=attachments,
        delete_attachments_after_send=delete_attachments_after_send,
        language_code=language_code
    )
