from django.core.mail import send_mail
from django.conf import settings
from django.contrib.auth.models import User

'''
'''
def send_email(message, username=None, to_admins=False, only_admins=False):
    if not username:
        return False
    
    if not settings.SEND_EMAIL:
        return False

    users = User.objects.all()
    mails = []

    if not only_admins:
        user = users.get(username=username)
        if user:
            mails.append(user.email)
    
    if to_admins or only_admins:
        for admin in users.filter(is_superuser=True).exclude(username=username).all():
            if admin.email in mails:
                continue

            mails.append(admin.email)
            
    if len(mails) == 0:
        return False

    send_mail(
        settings.SYSTEM_NAME,
        message,
        settings.EMAIL_FROM,
        mails
    )

    return True
