Python sendgrid.SendGridClient() Examples

The following are 3 code examples of sendgrid.SendGridClient(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module sendgrid , or try the search function .
Example #1
Source File: mail.py    From focuson with MIT License 5 votes vote down vote up
def send_email(routes_to_report):
    """Send email using sendgrid."""
    number_of_routes = len(routes_to_report)
    if number_of_routes == 0:
        return False

    formatted_date = dt.datetime.utcnow().strftime("%A, %b %d")
    rich_email_body = render_template(
        "email.html",
        routes=routes_to_report,
        date=formatted_date
    )

    sg = SendGridClient(config.SG_USERNAME, config.SG_PASSWORD, raise_errors=True)

    formatted_time = dt.datetime.utcnow().strftime("%F %T")
    subject = '({}): {} routes'
    subject = subject.format(formatted_time, number_of_routes)

    try:
        message = Mail(
            to=config.TO_EMAIL,
            subject=subject,
            html=rich_email_body,
            from_email=config.FROM_EMAIL
        )

        status, msg = sg.send(message)
        return msg
    except SendGridClientError as e:
        print 'Failed to send email: ', e
        raise 
Example #2
Source File: sending_mail.py    From django-simple-forum with MIT License 5 votes vote down vote up
def Memail(mto, mfrom, msubject, mbody, email_template_name, context):
    if settings.MAIL_SENDER == 'AMAZON':
        sending_mail(msubject, email_template_name, context, mfrom, mto)
    elif settings.MAIL_SENDER == 'MAILGUN':
        requests.post(
            settings.MGUN_API_URL,
            auth=('api', settings.MGUN_API_KEY),
            data={
                'from': mfrom,
                'to': mto,
                'subject': msubject,
                'html': mbody
            })
    elif settings.MAIL_SENDER == 'SENDGRID':
        sg = sendgrid.SendGridClient(settings.SG_USER, settings.SG_PWD)
        sending_msg = sendgrid.Mail()
        sending_msg.set_subject(msubject)
        sending_msg.set_html(mbody)
        sending_msg.set_text(msubject)
        sending_msg.set_from(mfrom)
        sending_msg.add_to(mto)
        reposne = sg.send(sending_msg)
        print (reposne)
    else:
        text_content = msubject
        msg = EmailMultiAlternatives(msubject, mbody, mfrom, mto)
        msg.attach_alternative(mbody, "text/html")
        msg.send() 
Example #3
Source File: email.py    From alertlib with MIT License 5 votes vote down vote up
def _send_to_sendgrid(self, message, email_addresses, cc=None, bcc=None,
                          sender=None):
        username = (base.secret('sendgrid_low_priority_username') or
                    base.secret('sendgrid_username'))
        password = (base.secret('sendgrid_low_priority_password') or
                    base.secret('sendgrid_password'))
        assert username and password, "Can't find sendgrid username/password"
        client = sendgrid.SendGridClient(username, password, raise_errors=True)

        # The sendgrid API client auto-utf8-izes to/cc/bcc, but not
        # subject/text.  Shrug.
        msg = sendgrid.Mail(
            subject=self._get_summary().encode('utf-8'),
            to=email_addresses,
            cc=cc,
            bcc=bcc)
        if self.html:
            # TODO(csilvers): convert the html to text for 'body'.
            # (see base.py about using html2text or similar).
            msg.set_text(message.encode('utf-8'))
            msg.set_html(message.encode('utf-8'))
        else:
            msg.set_text(message.encode('utf-8'))
        # Can't be keyword arg because those don't parse "Name <email>"
        # format.
        msg.set_from(_get_sender(sender))

        client.send(msg)