예제 #1
0
def send_email(to,
               subject,
               body,
               template='email/notification.html',
               link=None,
               link_text="Sign in"):
    """ Send an email using sendgrid.
    Usage: send_email('*****@*****.**', 'Hey from OK', 'hi')
    """
    if not link:
        link = url_for('student.index', _external=True)
    html = render_template(template,
                           subject=subject,
                           body=body,
                           link=link,
                           link_text=link_text)
    message = sendgrid.Mail(to=to,
                            from_name="Okpy.org",
                            from_email="*****@*****.**",
                            subject=subject,
                            html=emailFormat(html),
                            text=body)

    try:
        status, msg = sg.send(message)
        return status
    except (sendgrid.SendGridClientError, sendgrid.SendGridServerError,
            TypeError, ValueError):
        logger.error("Could not send email", exc_info=True)
        return
예제 #2
0
def send_email(to,
               subject,
               body,
               cc=(),
               from_name='Ok',
               link=None,
               link_text="Sign in",
               template='email/notification.html',
               reply_to=None,
               **kwargs):
    """ Send an email using sendgrid.
    Usage: send_email('*****@*****.**', 'Hey from OK', 'hi',
                      cc=['*****@*****.**'], reply_to='*****@*****.**')
    """
    try:
        sg = _get_sendgrid_api_client()
    except ValueError as ex:
        logger.error('Unable to get sendgrid client: %s', ex)
        return False

    if not link:
        link = url_for('student.index', _external=True)

    html = render_template(template,
                           subject=subject,
                           body=body,
                           link=link,
                           link_text=link_text,
                           **kwargs)
    mail = sg_helpers.Mail()
    mail.set_from(sg_helpers.Email('*****@*****.**', from_name))
    mail.set_subject(subject)
    mail.add_content(sg_helpers.Content("text/html", emailFormat(html)))

    if reply_to:
        mail.set_reply_to(sg_helpers.Email(reply_to))

    personalization = sg_helpers.Personalization()
    personalization.add_to(sg_helpers.Email(to))
    for recipient in cc:
        personalization.add_cc(sg_helpers.Email(recipient))

    mail.add_personalization(personalization)

    try:
        response = sg.client.mail.send.post(request_body=mail.get())
    except HTTPError:
        logger.error("Could not send the email", exc_info=True)
        return False

    if response.status_code != 202:
        logger.error("Could not send email: {} - {}".format(
            response.status_code, response.body))
        return False
    return True
예제 #3
0
def send_email(to, subject, body, cc=(), from_name='Ok',
               link=None, link_text="Sign in",
               template='email/notification.html', reply_to=None, **kwargs):
    """ Send an email using sendgrid.
    Usage: send_email('*****@*****.**', 'Hey from OK', 'hi',
                      cc=['*****@*****.**'], reply_to='*****@*****.**')
    """
    try:
        sg = _get_sendgrid_api_client()
    except ValueError as ex:
        logger.error('Unable to get sendgrid client: %s', ex)
        return False

    if not link:
        link = url_for('student.index', _external=True)

    html = render_template(template, subject=subject, body=body,
                           link=link, link_text=link_text, **kwargs)
    mail = sg_helpers.Mail()
    mail.set_from(sg_helpers.Email('*****@*****.**', from_name))
    mail.set_subject(subject)
    mail.add_content(sg_helpers.Content("text/html", emailFormat(html)))

    if reply_to:
        mail.set_reply_to(sg_helpers.Email(reply_to))

    personalization = sg_helpers.Personalization()
    personalization.add_to(sg_helpers.Email(to))
    for recipient in cc:
        personalization.add_cc(sg_helpers.Email(recipient))

    mail.add_personalization(personalization)

    try:
        response = sg.client.mail.send.post(request_body=mail.get())
    except HTTPError:
        logger.error("Could not send the email", exc_info=True)
        return False

    if response.status_code != 202:
        logger.error("Could not send email: {} - {}"
                     .format(response.status_code, response.body))
        return False
    return True
예제 #4
0
def send_email(to, subject, body, template='email/notification.html',
               link=None, link_text="Sign in"):
    """ Send an email using sendgrid.
    Usage: send_email('*****@*****.**', 'Hey from OK', 'hi')
    """
    if not link:
        link = url_for('student.index', _external=True)
    html = render_template(template, subject=subject, body=body,
                           link=link, link_text=link_text)
    message = sendgrid.Mail(
        to=to,
        from_name="Okpy.org",
        from_email="*****@*****.**",
        subject=subject,
        html=emailFormat(html),
        text=body)

    try:
        status, msg = sg.send(message)
        return status
    except (sendgrid.SendGridClientError, sendgrid.SendGridServerError,
            TypeError, ValueError):
        logger.error("Could not send email", exc_info=True)
        return