# Path: util/email.py
# def get_name_and_email(email_string):
#     """Take a string that is either 'Name <email>' or just an email address
#     and return a two tuple (email, name). If there is just an email, returns
#     None for the name."""
#     match = re.match(r'(.*) <(.*)>', email_string)
#     if match:
#         return match.group(2), match.group(1)
#     else:
#         return email_string, None


from google.appengine.api.mail import EmailMessage
from util.email import get_name_and_email
import config
import logic.secret
    import sendgrid
# -*- coding: utf-8 -*-


if config.EMAIL_BACKEND == 'sendgrid':
    # a bit of a hack here so that we can avoid adding dependencies unless
    # the user wants them


def send_appengine_email(sender, recipient, subject, body_html, body_text):
    email = EmailMessage()
    email.sender = sender
    email.to = recipient
    email.subject = subject
    email.body = body_text
    email.html = body_html
    email.send()


def send_sendgrid_email(sender, recipient, subject, body_html, body_text):
    key = logic.secret.get_secret('SENDGRID_API_KEY')
    sg = sendgrid.SendGridAPIClient(apikey=key)

    from_ = sendgrid.helpers.mail.Email(*get_name_and_email(sender))

# Path: util/email.py
# def get_name_and_email(email_string):
#     """Take a string that is either 'Name <email>' or just an email address
#     and return a two tuple (email, name). If there is just an email, returns
#     None for the name."""
#     match = re.match(r'(.*) <(.*)>', email_string)
#     if match:
#         return match.group(2), match.group(1)
#     else:
#         return email_string, None


# Path: logic/email.py
from google.appengine.api.mail import EmailMessage
from util.email import get_name_and_email
import config
import logic.secret
    import sendgrid

# -*- coding: utf-8 -*-


if config.EMAIL_BACKEND == 'sendgrid':
    # a bit of a hack here so that we can avoid adding dependencies unless
    # the user wants them


def send_appengine_email(sender, recipient, subject, body_html, body_text):
    email = EmailMessage()
    email.sender = sender
    email.to = recipient
    email.subject = subject
    email.body = body_text
    email.html = body_html
    email.send()


def send_sendgrid_email(sender, recipient, subject, body_html, body_text):
    key = logic.secret.get_secret('SENDGRID_API_KEY')
    sg = sendgrid.SendGridAPIClient(apikey=key)

    from_ = sendgrid.helpers.mail.Email(*get_name_and_email(sender))

    to = sendgrid.helpers.mail.Email(*get_name_and_email(recipient))