# Path: core/emails.py
# def generic_email(user, this_from_email, this_subject, this_message):
# 
#     c = Context({
#         'username': user.userprofile.get_display_name(),
#         'subject': this_subject,
#         'message': this_message,
#     })
# 
#     email_subject = render_to_string('users/email/generic_message_subject.txt', c).replace('\n', '')
#     email_txt_body = render_to_string('users/email/generic_message_body.txt', c)
#     email_html_body = render_to_string('users/email/generic_message_body.html', c)
# 
#     subject, from_email, to = email_subject, this_from_email, user.email
#     text_content = email_txt_body
#     html_content = email_html_body
# 
#     email_headers = {
#         "X-Mailgun-Campaign-Id": 'h0pk2',
#         'Reply-To': this_from_email
#     }
# 
#     msg = EmailMultiAlternatives(subject,
#                                  text_content,
#                                  from_email,
#                                  [to],
#                                  headers=email_headers
#                                  )
#     msg.attach_alternative(html_content, "text/html")
# 
#     return msg.send(fail_silently=False)
# 
# Path: users/models.py
# class UserProfile(models.Model):
# 
#     """
#     Model used to store more information on users
#     """
#     created_on = models.DateTimeField(_("Created on"), auto_now_add=True)
#     updated_on = models.DateTimeField(_("Updated on"), auto_now=True)
#     user = models.OneToOneField(User, verbose_name=_("User"))
#     customer = models.ForeignKey(Customer, verbose_name=_(
#         "Customer"), on_delete=models.PROTECT, blank=True, null=True, default=None)
#     is_manager = models.BooleanField(
#         _("Is Manager"), help_text=_("Is this user a manager"), default=False)
#     manager = models.ForeignKey("self", verbose_name=_('Manager'), help_text=_(
#         "Select this user's manager"), blank=True, null=True, default=None, on_delete=models.PROTECT)
#     is_admin = models.BooleanField(
#         _("Is Administrator"), help_text=_("Should this user have administrative privileges"), default=False)
#     group = models.ManyToManyField(UserGroup, blank=True, default=None)
# 
#     class Meta:
#         verbose_name = _("User Profile")
#         verbose_name_plural = _("User Profiles")
#         ordering = ['user__first_name', 'created_on']
# 
#     def get_display_name(self):
#         if self.user.get_full_name():
#             return self.user.get_full_name()
#         return self.user.username
# 
#     def __str__(self):
#         return self.get_display_name()
# 
# class UserGroup(models.Model):
#     """
#     A group of users e.g. a department in an organisation
#     """
#     created_on = models.DateTimeField(_("Created on"), auto_now_add=True)
#     updated_on = models.DateTimeField(_("Updated on"), auto_now=True)
#     customer = models.ForeignKey(Customer, verbose_name=_(
#         "Customer"), on_delete=models.PROTECT, blank=True, null=True, default=None)
#     name = models.CharField(_("Group Name"), max_length=300, blank=False)
#     parent = models.ForeignKey(
#         "self", verbose_name=_('Parent Group'), blank=True, null=True, default=None, on_delete=models.PROTECT)
#     manager = models.ForeignKey(
#         "UserProfile", verbose_name=_('Group Manager'), blank=True, null=True, default=None, on_delete=models.PROTECT)
# 
#     class Meta:
#         verbose_name = _("User Group")
#         verbose_name_plural = _("User Groups")
#         ordering = ['name']
# 
#     def __str__(self):
#         return self.name


from core.emails import generic_email
from .models import UserProfile, UserGroup



def send_email_to_users(customer, from_name, from_email, subject, message):
    userprofiles = UserProfile.objects.filter(customer=customer)
    if userprofiles:

# Path: core/emails.py
# def generic_email(user, this_from_email, this_subject, this_message):
# 
#     c = Context({
#         'username': user.userprofile.get_display_name(),
#         'subject': this_subject,
#         'message': this_message,
#     })
# 
#     email_subject = render_to_string('users/email/generic_message_subject.txt', c).replace('\n', '')
#     email_txt_body = render_to_string('users/email/generic_message_body.txt', c)
#     email_html_body = render_to_string('users/email/generic_message_body.html', c)
# 
#     subject, from_email, to = email_subject, this_from_email, user.email
#     text_content = email_txt_body
#     html_content = email_html_body
# 
#     email_headers = {
#         "X-Mailgun-Campaign-Id": 'h0pk2',
#         'Reply-To': this_from_email
#     }
# 
#     msg = EmailMultiAlternatives(subject,
#                                  text_content,
#                                  from_email,
#                                  [to],
#                                  headers=email_headers
#                                  )
#     msg.attach_alternative(html_content, "text/html")
# 
#     return msg.send(fail_silently=False)
# 
# Path: users/models.py
# class UserProfile(models.Model):
# 
#     """
#     Model used to store more information on users
#     """
#     created_on = models.DateTimeField(_("Created on"), auto_now_add=True)
#     updated_on = models.DateTimeField(_("Updated on"), auto_now=True)
#     user = models.OneToOneField(User, verbose_name=_("User"))
#     customer = models.ForeignKey(Customer, verbose_name=_(
#         "Customer"), on_delete=models.PROTECT, blank=True, null=True, default=None)
#     is_manager = models.BooleanField(
#         _("Is Manager"), help_text=_("Is this user a manager"), default=False)
#     manager = models.ForeignKey("self", verbose_name=_('Manager'), help_text=_(
#         "Select this user's manager"), blank=True, null=True, default=None, on_delete=models.PROTECT)
#     is_admin = models.BooleanField(
#         _("Is Administrator"), help_text=_("Should this user have administrative privileges"), default=False)
#     group = models.ManyToManyField(UserGroup, blank=True, default=None)
# 
#     class Meta:
#         verbose_name = _("User Profile")
#         verbose_name_plural = _("User Profiles")
#         ordering = ['user__first_name', 'created_on']
# 
#     def get_display_name(self):
#         if self.user.get_full_name():
#             return self.user.get_full_name()
#         return self.user.username
# 
#     def __str__(self):
#         return self.get_display_name()
# 
# class UserGroup(models.Model):
#     """
#     A group of users e.g. a department in an organisation
#     """
#     created_on = models.DateTimeField(_("Created on"), auto_now_add=True)
#     updated_on = models.DateTimeField(_("Updated on"), auto_now=True)
#     customer = models.ForeignKey(Customer, verbose_name=_(
#         "Customer"), on_delete=models.PROTECT, blank=True, null=True, default=None)
#     name = models.CharField(_("Group Name"), max_length=300, blank=False)
#     parent = models.ForeignKey(
#         "self", verbose_name=_('Parent Group'), blank=True, null=True, default=None, on_delete=models.PROTECT)
#     manager = models.ForeignKey(
#         "UserProfile", verbose_name=_('Group Manager'), blank=True, null=True, default=None, on_delete=models.PROTECT)
# 
#     class Meta:
#         verbose_name = _("User Group")
#         verbose_name_plural = _("User Groups")
#         ordering = ['name']
# 
#     def __str__(self):
#         return self.name


# Path: users/utils.py
from core.emails import generic_email
from .models import UserProfile, UserGroup




def send_email_to_users(customer, from_name, from_email, subject, message):
    userprofiles = UserProfile.objects.filter(customer=customer)
    if userprofiles:

        sender = '{0} <{1}>'.format(from_name, from_email)