How to use sendgrid - 10 common examples

To help you get started, we’ve selected a few sendgrid examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sendgrid / sendgrid-python / test / test_email.py View on Github external
def test_add_rfc_email(self):
        name = "SomeName"
        address = "test@example.com"
        name_address = "{} <{}>".format(name, address)
        email = Email(name_address)
        self.assertEqual(email.name, name)
        self.assertEqual(email.email, "test@example.com")
github tytso / xfstests-bld / kvm-xfstests / test-appliance / files / usr / local / lib / gce-ltm / testrunmanager.py View on Github external
or self.report_receiver)
    source_email = sendgrid.helpers.mail.Email(report_sender)
    dest_email = sendgrid.helpers.mail.Email(self.report_receiver)
    logging.debug('email_subject %s, report_sender %s, report_receiver %s',
                  email_subject, report_sender, self.report_receiver)
    try:
      logging.info('Reading reports file as e-mail contents')
      with open(os.path.join(self.agg_results_dir, 'report'), 'r') as ff:
        report_content = sendgrid.helpers.mail.Content('text/plain', ff.read())
    except IOError:
      logging.warning('Unable to read report file for report contents')
      report_content = sendgrid.helpers.mail.Content(
          'text/plain',
          'Could not read contents of report file')
    sg = sendgrid.SendGridAPIClient(apikey=sendgrid_api_key)
    full_email = sendgrid.helpers.mail.Mail(source_email, email_subject,
                                            dest_email, report_content)

    try:
      response = sg.client.mail.send.post(request_body=full_email.get())
    except HTTPError as err:
      # Probably a bad API key. Possible other causes could include sendgrid's
      # API being unavailable, or any other errors that come from the
      # api.sendgrid.com/mail/send endpoint.
      # Docs:
      # https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/index.html
      # https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html
      # Common probable occurrences:
      # 401 unauthorized, 403 forbidden, 413 payload too large, 429 too many
      # requests, 500 server unavailable, 503 v3 api unavailable.
      # also, 200 OK and 202 ACCEPTED
      response = err
github sendgrid / sendgrid-python / test / test_mail_helpers.py View on Github external
# Minimum required to send an email
        self.max_diff = None
        mail = Mail()

        mail.from_email = Email("test@example.com")

        mail.subject = "Hello World from the SendGrid Python Library"

        personalization = Personalization()
        personalization.add_to(Email("test@example.com"))
        mail.add_personalization(personalization)

        # Try to include SendGrid API key
        try:
            mail.add_content(
                Content(
                    "text/plain",
                    "some SG.2123b1B.1212lBaC here"))
            mail.add_content(
                Content(
                    "text/html",
                    "some SG.Ba2BlJSDba.232Ln2 here"))

            self.assertEqual(
                json.dumps(
                    mail.get(),
                    sort_keys=True),
                '{"content": [{"type": "text/plain", "value": "some text here"}, '
                '{"type": "text/html", '
                '"value": "some text here"}], '
                '"from": {"email": "test@example.com"}, "personalizations": '
                '[{"to": [{"email": "test@example.com"}]}], '
github tytso / xfstests-bld / kvm-xfstests / test-appliance / files / usr / local / lib / gce-ltm / testrunmanager.py View on Github external
if not sendgrid_api_key:
      logging.warning('No sendgrid api key found. Can\'t send email.')
      return
    logging.debug('Got sendgrid api key')
    email_subject = 'xfstests results %s-%s %s' % (
        LTM.ltm_username, self.id, self.kernel_version)
    report_sender = (gce_funcs.get_email_report_sender()
                     or self.report_receiver)
    source_email = sendgrid.helpers.mail.Email(report_sender)
    dest_email = sendgrid.helpers.mail.Email(self.report_receiver)
    logging.debug('email_subject %s, report_sender %s, report_receiver %s',
                  email_subject, report_sender, self.report_receiver)
    try:
      logging.info('Reading reports file as e-mail contents')
      with open(os.path.join(self.agg_results_dir, 'report'), 'r') as ff:
        report_content = sendgrid.helpers.mail.Content('text/plain', ff.read())
    except IOError:
      logging.warning('Unable to read report file for report contents')
      report_content = sendgrid.helpers.mail.Content(
          'text/plain',
          'Could not read contents of report file')
    sg = sendgrid.SendGridAPIClient(apikey=sendgrid_api_key)
    full_email = sendgrid.helpers.mail.Mail(source_email, email_subject,
                                            dest_email, report_content)

    try:
      response = sg.client.mail.send.post(request_body=full_email.get())
    except HTTPError as err:
      # Probably a bad API key. Possible other causes could include sendgrid's
      # API being unavailable, or any other errors that come from the
      # api.sendgrid.com/mail/send endpoint.
      # Docs:
github sendgrid / sendgrid-python / test / test_mail_helpers.py View on Github external
message.from_email = From('dx@example.com', 'DX')

        message.reply_to = ReplyTo('dx_reply@example.com', 'DX Reply')

        message.subject = Subject('Sending with SendGrid is Fun 2')

        message.content = Content(
            MimeType.text,
            'and easy to do anywhere, even with Python')
        message.content = Content(
            MimeType.html,
            '<strong>and easy to do anywhere, even with Python</strong>')
        message.content = [
            Content('text/calendar', 'Party Time!!'),
            Content('text/custom', 'Party Time 2!!')
        ]

        message.attachment = Attachment(
            FileContent('base64 encoded content 1'),
            FileName('balance_001.pdf'),
            FileType('application/pdf'),
            Disposition('attachment'),
            ContentId('Content ID 1'))
        message.attachment = [
            Attachment(
                FileContent('base64 encoded content 2'),
                FileName('banner.png'),
                FileType('image/png'),
                Disposition('inline'),
                ContentId('Content ID 2')),
            Attachment(
github sendgrid / sendgrid-python / test / test_mail_v2.py View on Github external
def setUp(self):
        self.sg = SendGridClient(SG_USER, SG_PWD)
        self.maxDiff = None
github sendgrid / sendgrid-python / test / __init__.py View on Github external
def setUp(self):
        self.sg = SendGridClient(SG_USER, SG_PWD)
github sendgrid / sendgrid-python / test / test_mail_helpers.py View on Github external
message.header = Header('X-Test1', 'Test1', p=0)
        message.header = Header('X-Test2', 'Test2', p=0)
        message.header = [
            Header('X-Test3', 'Test3', p=0),
            Header('X-Test4', 'Test4', p=0)
        ]

        message.substitution = Substitution('%name1%', 'Example Name 1', p=0)
        message.substitution = Substitution('%city1%', 'Example City 1', p=0)
        message.substitution = [
            Substitution('%name2%', 'Example Name 2', p=0),
            Substitution('%city2%', 'Example City 2', p=0)
        ]

        message.custom_arg = CustomArg('marketing1', 'true', p=0)
        message.custom_arg = CustomArg('transactional1', 'false', p=0)
        message.custom_arg = [
            CustomArg('marketing2', 'false', p=0),
            CustomArg('transactional2', 'true', p=0)
        ]

        message.send_at = SendAt(1461775051, p=0)

        message.to = To('test10@example.com', 'Example User10', p=1)
        message.to = [
            To('test11@example.com', 'Example User11', p=1),
            To('test12@example.com', 'Example User12', p=1)
        ]

        message.cc = Cc('test13@example.com', 'Example User13', p=1)
        message.cc = [
            Cc('test14@example.com', 'Example User14', p=1),
github sendgrid / sendgrid-python / test / test_mail_helpers.py View on Github external
Header('X-Test3', 'Test3', p=0),
            Header('X-Test4', 'Test4', p=0)
        ]

        message.substitution = Substitution('%name1%', 'Example Name 1', p=0)
        message.substitution = Substitution('%city1%', 'Example City 1', p=0)
        message.substitution = [
            Substitution('%name2%', 'Example Name 2', p=0),
            Substitution('%city2%', 'Example City 2', p=0)
        ]

        message.custom_arg = CustomArg('marketing1', 'true', p=0)
        message.custom_arg = CustomArg('transactional1', 'false', p=0)
        message.custom_arg = [
            CustomArg('marketing2', 'false', p=0),
            CustomArg('transactional2', 'true', p=0)
        ]

        message.send_at = SendAt(1461775051, p=0)

        message.to = To('test10@example.com', 'Example User10', p=1)
        message.to = [
            To('test11@example.com', 'Example User11', p=1),
            To('test12@example.com', 'Example User12', p=1)
        ]

        message.cc = Cc('test13@example.com', 'Example User13', p=1)
        message.cc = [
            Cc('test14@example.com', 'Example User14', p=1),
            Cc('test15@example.com', 'Example User15', p=1)
        ]
github sendgrid / sendgrid-python / test / test_mail_helpers.py View on Github external
message.header = Header('X-Test5', 'Test5', p=1)
        message.header = Header('X-Test6', 'Test6', p=1)
        message.header = [
            Header('X-Test7', 'Test7', p=1),
            Header('X-Test8', 'Test8', p=1)
        ]

        message.substitution = Substitution('%name3%', 'Example Name 3', p=1)
        message.substitution = Substitution('%city3%', 'Example City 3', p=1)
        message.substitution = [
            Substitution('%name4%', 'Example Name 4', p=1),
            Substitution('%city4%', 'Example City 4', p=1)
        ]

        message.custom_arg = CustomArg('marketing3', 'true', p=1)
        message.custom_arg = CustomArg('transactional3', 'false', p=1)
        message.custom_arg = [
            CustomArg('marketing4', 'false', p=1),
            CustomArg('transactional4', 'true', p=1)
        ]

        message.send_at = SendAt(1461775052, p=1)

        message.subject = Subject('Sending with SendGrid is Fun 1', p=1)

        # The values below this comment are global to entire message

        message.from_email = From('dx@example.com', 'DX')

        message.reply_to = ReplyTo('dx_reply@example.com', 'DX Reply')