Exemple #1
0
def send_mail(from_email, _to_email, subject, body, html=False,
              from_name="Gitcoin.co", cc_emails=None, categories=None):
    """Send email via SendGrid."""
    # make sure this subscriber is saved
    if not settings.SENDGRID_API_KEY:
        print('No SendGrid API Key set. Not attempting to send email.')
        return

    if categories is None:
        categories = ['default']

    to_email = _to_email
    get_or_save_email_subscriber(to_email, 'internal')

    # setup
    from_name = str(from_name)
    subject = str(subject)
    sg = sendgrid.SendGridAPIClient(apikey=settings.SENDGRID_API_KEY)
    from_email = Email(from_email, from_name)
    to_email = Email(to_email)
    contenttype = "text/plain" if not html else "text/html"

    # build content
    content = Content(contenttype, html) if html else Content(contenttype, body)
    if settings.IS_DEBUG_ENV:
        to_email = Email(settings.CONTACT_EMAIL)  # just to be double secret sure of what were doing in dev
        subject = _("[DEBUG] ") + subject
    mail = Mail(from_email, subject, to_email, content)
    response = None

    # build personalization
    if cc_emails:
        p = Personalization()
        p.add_to(to_email)
        for cc_addr in set(cc_emails):
            cc_addr = Email(cc_addr)
            if settings.IS_DEBUG_ENV:
                cc_addr = to_email
            if cc_addr._email != to_email._email:
                p.add_to(cc_addr)
        mail.add_personalization(p)

    # categories
    for category in categories:
        mail.add_category(Category(category))

    # debug logs
    print(f"-- Sending Mail '{subject}' to {_to_email}")

    # send mails
    try:
        response = sg.client.mail.send.post(request_body=mail.get())
    except UnauthorizedError:
        print(f'-- Sendgrid Mail failure - Unauthorized - Check sendgrid credentials')
    except HTTPError as e:
        print(f'-- Sendgrid Mail failure - {e}')

    return response
Exemple #2
0
def send_mail(from_email, _to_email, subject, body, html=False,
              from_name="Gitcoin.co", cc_emails=None, categories=None, debug_mode=False):
    """Send email via SendGrid."""
    # make sure this subscriber is saved
    if not settings.SENDGRID_API_KEY:
        logger.warning('No SendGrid API Key set. Not attempting to send email.')
        return

    if categories is None:
        categories = ['default']

    to_email = _to_email
    get_or_save_email_subscriber(to_email, 'internal')

    # setup
    from_name = str(from_name)
    subject = str(subject)
    sg = sendgrid.SendGridAPIClient(apikey=settings.SENDGRID_API_KEY)
    from_email = Email(from_email, from_name)
    to_email = Email(to_email)
    contenttype = "text/plain" if not html else "text/html"

    # build content
    content = Content(contenttype, html) if html else Content(contenttype, body)

    # TODO:  A bit of a hidden state change here.  Really confusing when doing development.
    #        Maybe this should be a variable passed into the function the value is set upstream? 
    if settings.IS_DEBUG_ENV or debug_mode:
        to_email = Email(settings.CONTACT_EMAIL)  # just to be double secret sure of what were doing in dev
        subject = _("[DEBUG] ") + subject

    mail = Mail(from_email, subject, to_email, content)
    response = None

    # build personalization
    if cc_emails:
        p = Personalization()
        p.add_to(to_email)
        for cc_addr in set(cc_emails):
            cc_addr = Email(cc_addr)
            if settings.IS_DEBUG_ENV:
                cc_addr = to_email
            if cc_addr._email != to_email._email:
                p.add_to(cc_addr)
        mail.add_personalization(p)

    # categories
    for category in categories:
        mail.add_category(Category(category))

    # debug logs
    logger.info(f"-- Sending Mail '{subject}' to {to_email}")
    try:
        response = sg.client.mail.send.post(request_body=mail.get())
    except UnauthorizedError as e:
        logger.error(f'-- Sendgrid Mail failure - Unauthorized - Check sendgrid credentials')
        logger.error(e)
    except HTTPError as e:
        logger.error(f'-- Sendgrid Mail failure - {e}')

    return response