How to use the sendgrid.Message function in sendgrid

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_message.py View on Github external
def test_message_construction(self):
        # set string from address
        message = sendgrid.Message("example@example.com", "subject1", "plain_text", "html")
        self.assertEqual(message.from_name, '', "From name is empty")
        self.assertEqual(message.from_address, 'example@example.com', "From address is set")

        # set tuple from address
        message = sendgrid.Message(("example1@example.com", "John, Doe"), "subject1", "plain_text", "html")
        self.assertEqual(message.from_name, 'John, Doe', "From name is set")
        self.assertEqual(message.from_address, 'example1@example.com', "From address is set")

        # omit html and text parameter
        self.assertRaises(ValueError, sendgrid.Message, ("example1@example.com", "John, Doe"), "subject1")

        # pass html only
        sendgrid.Message(("example1@example.com", "John, Doe"), "subject1", html="html")

        # pass text only
        sendgrid.Message(("example1@example.com", "John, Doe"), "subject1", text="text")
github sendgrid / sendgrid-python / test / test_message.py View on Github external
message.add_category(["category 2", "category 3"])
        self.assertEqual(message.header.as_json(), '{"category": ["category 1", "category 2", "category 3"]}',
            "Category list added")

        # add unique arguments
        message = sendgrid.Message("example@example.com", "subject1", "plain_text", "html")
        message.set_unique_arguments({"customerAccountNumber": "55555", "activationAttempt": "1"})
        self.assertEqual(message.header.as_json(), '{"unique_args": {"activationAttempt": "1", "customerAccountNumber": "55555"}}',
            "Unique arguments added")

        message.add_unique_argument("test", "some_value")
        self.assertEqual(message.header.as_json(), '{"unique_args": {"test": "some_value", "activationAttempt": "1", "customerAccountNumber": "55555"}}',
            "Unique argument added")

        # add header
        message = sendgrid.Message("example@example.com", "subject1", "plain_text", "html")
        message.add_header("x-test", "header1")
        self.assertEqual(message.headers, {"x-test": "header1"}, "Header added")

        # add filter settings
        message = sendgrid.Message("example@example.com", "subject1", "plain_text", "html")
        message.add_filter_setting("gravatar", "enable", 1)
        self.assertEqual(message.header.as_json(), '{"filters": {"gravatar": {"settings": {"enable": 1}}}}',
            "Filter setting added")

        # add sections
        message = sendgrid.Message("example@example.com", "subject1", "plain_text", "html")
        message.set_sections({"section1": "Section1", "section2": "Section2"})
        self.assertEqual(message.header.as_json(), '{"section": {"section2": "Section2", "section1": "Section1"}}',
            "Sections added")
github sendgrid / sendgrid-python / test / test_message.py View on Github external
def test_recipients_adding(self):
        message = sendgrid.Message("example@example.com", "subject1", "plain_text", "html")
        message.add_to("example@example.com", "John Doe")
        self.assertEqual(message.to, ["example@example.com"], "Single email address added")
        self.assertEqual(message.to_name, ["John Doe"], "Single email address added")

        message.add_to(["example1@example.com", "example2@example.com"], ("John Doe", "Jane Doe"))
        self.assertEqual(message.to, ["example@example.com","example1@example.com","example2@example.com",],
            "Email list added")
        self.assertEqual(message.to_name, ["John Doe","John Doe","Jane Doe",], "Email list added")

        # following should replace existing to addresses and use x-smtpapi header instead
        message = sendgrid.Message("example@example.com", "subject1", "plain_text", "html")
        #invalid data
        data = {
            'example1@example.com': {'name': 'Name 1', 'code': 'Code 1'},
            'example2@example.com': {'name': 'Name 2'},
            }
github sendgrid / sendgrid-python / test / test_message.py View on Github external
message = sendgrid.Message("example@example.com", "subject1", "plain_text", "html")
        message.set_unique_arguments({"customerAccountNumber": "55555", "activationAttempt": "1"})
        self.assertEqual(message.header.as_json(), '{"unique_args": {"activationAttempt": "1", "customerAccountNumber": "55555"}}',
            "Unique arguments added")

        message.add_unique_argument("test", "some_value")
        self.assertEqual(message.header.as_json(), '{"unique_args": {"test": "some_value", "activationAttempt": "1", "customerAccountNumber": "55555"}}',
            "Unique argument added")

        # add header
        message = sendgrid.Message("example@example.com", "subject1", "plain_text", "html")
        message.add_header("x-test", "header1")
        self.assertEqual(message.headers, {"x-test": "header1"}, "Header added")

        # add filter settings
        message = sendgrid.Message("example@example.com", "subject1", "plain_text", "html")
        message.add_filter_setting("gravatar", "enable", 1)
        self.assertEqual(message.header.as_json(), '{"filters": {"gravatar": {"settings": {"enable": 1}}}}',
            "Filter setting added")

        # add sections
        message = sendgrid.Message("example@example.com", "subject1", "plain_text", "html")
        message.set_sections({"section1": "Section1", "section2": "Section2"})
        self.assertEqual(message.header.as_json(), '{"section": {"section2": "Section2", "section1": "Section1"}}',
            "Sections added")
github sendgrid / sendgrid-python / test / test_web_transport.py View on Github external
def test_web_transport_valid_password(self):
        fake_urlopen = fudge.Fake('urlopen')
        fake_response = fudge.Fake('fake_response')
        fake_urlencode = fudge.Fake('urlencode')

        urllib2 = fudge.patch_object("urllib2", "urlopen", fake_urlopen)
        fake_urlopen.expects_call().returns(fake_response)
        fake_response.expects("read").returns('{"message": "success", "status": "ok"}')

        message = sendgrid.Message("example@example.com", "subject1", "plain_text", "html")
        message.add_to("recipient@example.com")

        urllib = fudge.patch_object("urllib", "urlencode", fake_urlencode)
        fake_urlencode.expects_call().with_args({'from': 'example@example.com', 'api_user': 'username', 'text': 'plain_text',
                                                 'to': ['recipient@example.com'], 'toname': [''], 'html': 'html',
                                                 'date': message.date, 'api_key': 'password', 'subject': 'subject1'},
                                                 1).returns("")

        web_transport = web.Http('username', 'password')
        self.assertTrue(web_transport.send(message))

        urllib.restore()
        urllib2.restore()
github sendgrid / sendgrid-python / test / test_smtp_transport.py View on Github external
def test_smtp_transport_wrong_password(self):
        fake_login = fudge.Fake('smtp.login')
        smtp_login = fudge.patch_object("smtplib.SMTP", "login", fake_login)
        fake_login.expects_call().with_args('username', 'password').raises(Exception("SMTP authentication error"))

        message = sendgrid.Message("example@example.com", "subject1", "plain_text", "html")
        smtp_transport = smtp.Smtp('username', 'password', tls=False)
        self.assertRaises(sendgrid.exceptions.SGServiceException, smtp_transport.send, message)

        smtp_login.restore()
github sendgrid / sendgrid-python / test / test_web_transport.py View on Github external
def test_web_transport_wrong_password(self):
        fake_urlopen = fudge.Fake('urlopen')
        fake_response = fudge.Fake('fake_response')
        urllib = fudge.patch_object("urllib2", "urlopen", fake_urlopen)
        fake_urlopen.expects_call().returns(fake_response)
        fake_response.expects("read").raises(FakeException('{"message": "error", "errors": "Bad username / password"}'))

        message = sendgrid.Message("example@example.com", "subject1", "plain_text", "html")
        message.add_to("recipient@example.com")

        web_transport = web.Http('username', 'password')
        self.assertRaises(sendgrid.exceptions.SGServiceException, web_transport.send, message)

        urllib.restore()
github sendgrid / sendgrid-python / test / test_message.py View on Github external
def test_header_functions(self):
        # add categories
        message = sendgrid.Message("example@example.com", "subject1", "plain_text", "html")
        message.add_category("category 1")
        self.assertEqual(message.header.as_json(), '{"category": ["category 1"]}', "Category added")

        message.add_category(["category 2", "category 3"])
        self.assertEqual(message.header.as_json(), '{"category": ["category 1", "category 2", "category 3"]}',
            "Category list added")

        # add unique arguments
        message = sendgrid.Message("example@example.com", "subject1", "plain_text", "html")
        message.set_unique_arguments({"customerAccountNumber": "55555", "activationAttempt": "1"})
        self.assertEqual(message.header.as_json(), '{"unique_args": {"activationAttempt": "1", "customerAccountNumber": "55555"}}',
            "Unique arguments added")

        message.add_unique_argument("test", "some_value")
        self.assertEqual(message.header.as_json(), '{"unique_args": {"test": "some_value", "activationAttempt": "1", "customerAccountNumber": "55555"}}',
            "Unique argument added")
github mongolab / mongodb-backup-system / mbs / notification / handler.py View on Github external
def send_notification(self, subject, message, recipient=None):
        subject = subject or DEFAULT_NOTIFICATION_SUBJECT
        try:
            self._ensure_sg_initialized()
            logger.info("Sending notification email...")
            s_message = Message(self.from_address, subject=subject,
                                text=message)

            to_address = listify(recipient or self.to_address)
            for address in to_address:
                s_message.add_to(address)

            self._sendgrid.web.send(s_message)

            logger.info("Email sent successfully!")
        except Exception, e:
            print e
            print traceback.format_exc()
            logger.error("Error while sending email:\n%s" %
                         traceback.format_exc())