From ef7ba43cadb5e97aea1ade94c01927181a5992c4 Mon Sep 17 00:00:00 2001 From: Elmer Thomas Date: Thu, 29 Oct 2015 10:49:44 -0700 Subject: [PATCH 1/3] Clarifying documentation with examples --- README.rst | 40 ++++++++++++++++++++++++++++------------ example_v2_test.py | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 61 insertions(+), 14 deletions(-) diff --git a/README.rst b/README.rst index 57b5514ee..9fc1d35db 100644 --- a/README.rst +++ b/README.rst @@ -37,7 +37,7 @@ Example import sendgrid - sg = sendgrid.SendGridClient('YOUR_SENDGRID_USERNAME', 'YOUR_SENDGRID_PASSWORD') + sg = sendgrid.SendGridClient('YOUR_SENDGRID_API_KEY') message = sendgrid.Mail() message.add_to('John Doe ') @@ -245,7 +245,7 @@ List all API Keys belonging to the authenticated user. .. code:: python - client = sendgrid.SendGridAPIClient(os.environ.get('SENDGRID_API_KEY')) + client = sendgrid.SendGridAPIClient('SENDGRID_API_KEY') status, msg = client.apikeys.get() `Suppression Management`_ @@ -264,7 +264,7 @@ Retrieve all suppression groups associated with the user. .. code:: python - client = sendgrid.SendGridAPIClient(os.environ.get('SENDGRID_API_KEY')) + client = sendgrid.SendGridAPIClient('SENDGRID_API_KEY') status, msg = client.asm_groups.get() Get a single record. @@ -288,9 +288,9 @@ Add recipient addresses to the suppressions list for a given group. .. code:: python - client = sendgrid.SendGridAPIClient(os.environ.get('SENDGRID_API_KEY')) + client = sendgrid.SendGridAPIClient('SENDGRID_API_KEY') group_id = # If no group_id_number, the emails will be added to the global suppression group - emails = ['elmer+test@thinkingserious.com', 'elmer+test2@thinkingserious.com'] + emails = ['test@email.com', 'test2@email.com'] status, msg = client.asm_suppressions.post(group_id, emails) Get suppressed addresses for a given group. @@ -314,28 +314,28 @@ Check if a given email is on the global suppression list. .. code:: python - client = sendgrid.SendGridAPIClient(os.environ.get('SENDGRID_API_KEY')) - email = ['elmer@thinkingserious.com'] + client = sendgrid.SendGridAPIClient('SENDGRID_API_KEY') + email = ['test@email.com'] status, msg = client.asm_global_suppressions.get(email) Get a list of all SendGrid globally unsubscribed emails. .. code:: python - client = sendgrid.SendGridAPIClient(os.environ.get('SENDGRID_API_KEY')) + client = sendgrid.SendGridAPIClient('SENDGRID_API_KEY') status, msg = client.suppressions.get() Add an email to the global suppression list. .. code:: python - client = sendgrid.SendGridAPIClient(os.environ.get('SENDGRID_API_KEY')) - email = ['elmer@thinkingserious.com'] + client = sendgrid.SendGridAPIClient('SENDGRID_API_KEY') + email = ['test@email.com'] status, msg = client.asm_global_suppressions.post(email) Delete an email from the global suppression list. .. code:: python - client = sendgrid.SendGridAPIClient(os.environ.get('SENDGRID_API_KEY')) - email = 'elmer@thinkingserious.com' + client = sendgrid.SendGridAPIClient('SENDGRID_API_KEY') + email = 'test@email.com' status, msg = client.asm_global_suppressions.delete(email) SendGrid's `X-SMTPAPI`_ @@ -346,6 +346,21 @@ If you wish to use the X-SMTPAPI on your own app, you can use the There are implementations for setter methods too. +Example +~~~~~~~ + +.. code:: python + + sg = sendgrid.SendGridClient('SENDGRID_API_KEY') + message = sendgrid.Mail() + message.add_substitution(':first_name', 'John') + message.smtpapi.add_to('John ') + message.set_subject('Testing from the Python library using the SMTPAPI') + message.set_html(':first_name, this was a successful test of using the SMTPAPI library!') + message.set_text(':name, this was a successful test of using the SMTPAPI library!') + message.set_from('Doe ') + sg.send(message) + `Recipients`_ ~~~~~~~~~~~~~ @@ -489,6 +504,7 @@ Using Templates from the Template Engine message.add_filter('templates', 'enable', '1') message.add_filter('templates', 'template_id', 'TEMPLATE-ALPHA-NUMERIC-ID') + message.add_substitution('key', 'value') Tests ~~~~~ diff --git a/example_v2_test.py b/example_v2_test.py index 37410a136..f946f62b0 100755 --- a/example_v2_test.py +++ b/example_v2_test.py @@ -6,11 +6,14 @@ if len(var) == 2: os.environ[var[0]] = var[1] -sg = sendgrid.SendGridClient(os.environ.get('SENDGRID_USERNAME'), os.environ.get('SENDGRID_PASSWORD')) +sg = sendgrid.SendGridClient(os.environ.get('SENDGRID_API_KEY')) """ + +# Basic Send Example + message = sendgrid.Mail() -message.add_to('Elmer Thomas ') +message.add_to('Elmer Thomas ') message.set_subject('Testing from the Python library') message.set_html('This was a successful test!') message.set_text('This was a successful test!') @@ -18,4 +21,32 @@ status, msg = sg.send(message) print status print msg + +# SMTPAPI Basic Send Example + +message = sendgrid.Mail() +message.add_substitution(':first_name', 'Elmer') +message.smtpapi.add_to('Elmer Thomas ') +message.set_subject('Testing from the Python library using the SMTPAPI') +message.set_html(':first_name, this was a successful test of using the SMTPAPI library!') +message.set_text(':name, this was a successful test of using the SMTPAPI library!') +message.set_from('Elmer Thomas ') +status, msg = sg.send(message) +print status +print msg + +# Template Engine Example + +message = sendgrid.Mail() +message.add_filter('templates', 'enable', '1') +message.add_filter('templates', 'template_id', 'TEMPLATE-ALPHA-NUMERIC-ID') +message.add_substitution(':name', 'Elmer') +message.add_to('Elmer Thomas ') +message.set_subject('Testing from the Python library using the SMTPAPI') +message.set_html('This was a successful test of using the SMTPAPI library!') +message.set_text('This was a successful test of using the SMTPAPI library!') +message.set_from('Elmer Thomas ') +status, msg = sg.send(message) +print status +print msg """ \ No newline at end of file From fc5df4837c5a988f0cda1a2cfdc8c571c7ef028a Mon Sep 17 00:00:00 2001 From: Elmer Thomas Date: Tue, 17 Nov 2015 08:19:52 -0800 Subject: [PATCH 2/3] Clarifying the Template Engine example --- example_v2_test.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/example_v2_test.py b/example_v2_test.py index f946f62b0..05d19b0e1 100755 --- a/example_v2_test.py +++ b/example_v2_test.py @@ -36,6 +36,19 @@ print msg # Template Engine Example +# In the template editor, the subject is <%subject%> and the body is: +# +# Hello :name, +# +# <%body%> +# +# With Best Regards, +# +# Your Library Tester +# +# <%subject%> is replaced with the value in message.set_subject +# <%body%> is replaced with the value in message.set_html and message.set_text +# :name is replaced with the value in message.add_substitution message = sendgrid.Mail() message.add_filter('templates', 'enable', '1') @@ -49,4 +62,4 @@ status, msg = sg.send(message) print status print msg -""" \ No newline at end of file +""" From aa4d6f7511152c62debdaf1d9e3376d650eada6b Mon Sep 17 00:00:00 2001 From: Elmer Thomas Date: Tue, 17 Nov 2015 09:17:30 -0800 Subject: [PATCH 3/3] Clean up the README and example --- README.rst | 12 ++++++------ example_v2_test.py | 16 ++++++++-------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/README.rst b/README.rst index 9fc1d35db..7bf4b4672 100644 --- a/README.rst +++ b/README.rst @@ -290,7 +290,7 @@ Add recipient addresses to the suppressions list for a given group. client = sendgrid.SendGridAPIClient('SENDGRID_API_KEY') group_id = # If no group_id_number, the emails will be added to the global suppression group - emails = ['test@email.com', 'test2@email.com'] + emails = ['example@example.com', 'example@example.com'] status, msg = client.asm_suppressions.post(group_id, emails) Get suppressed addresses for a given group. @@ -315,7 +315,7 @@ Check if a given email is on the global suppression list. .. code:: python client = sendgrid.SendGridAPIClient('SENDGRID_API_KEY') - email = ['test@email.com'] + email = ['example@example.com'] status, msg = client.asm_global_suppressions.get(email) Get a list of all SendGrid globally unsubscribed emails. @@ -328,14 +328,14 @@ Add an email to the global suppression list. .. code:: python client = sendgrid.SendGridAPIClient('SENDGRID_API_KEY') - email = ['test@email.com'] + email = ['example@example.com'] status, msg = client.asm_global_suppressions.post(email) Delete an email from the global suppression list. .. code:: python client = sendgrid.SendGridAPIClient('SENDGRID_API_KEY') - email = 'test@email.com' + email = 'example@example.com' status, msg = client.asm_global_suppressions.delete(email) SendGrid's `X-SMTPAPI`_ @@ -354,11 +354,11 @@ Example sg = sendgrid.SendGridClient('SENDGRID_API_KEY') message = sendgrid.Mail() message.add_substitution(':first_name', 'John') - message.smtpapi.add_to('John ') + message.smtpapi.add_to('John ') message.set_subject('Testing from the Python library using the SMTPAPI') message.set_html(':first_name, this was a successful test of using the SMTPAPI library!') message.set_text(':name, this was a successful test of using the SMTPAPI library!') - message.set_from('Doe ') + message.set_from('Jane ') sg.send(message) `Recipients`_ diff --git a/example_v2_test.py b/example_v2_test.py index f946f62b0..1ec19a647 100755 --- a/example_v2_test.py +++ b/example_v2_test.py @@ -13,11 +13,11 @@ # Basic Send Example message = sendgrid.Mail() -message.add_to('Elmer Thomas ') +message.add_to('John Doe ') message.set_subject('Testing from the Python library') message.set_html('This was a successful test!') message.set_text('This was a successful test!') -message.set_from('Elmer Thomas ') +message.set_from('Jane Doe ') +message.add_substitution(':first_name', 'John') +message.smtpapi.add_to('Elmer Thomas ') message.set_subject('Testing from the Python library using the SMTPAPI') message.set_html(':first_name, this was a successful test of using the SMTPAPI library!') message.set_text(':name, this was a successful test of using the SMTPAPI library!') -message.set_from('Elmer Thomas ') +message.set_from('Jane Doe ') status, msg = sg.send(message) print status print msg @@ -40,12 +40,12 @@ message = sendgrid.Mail() message.add_filter('templates', 'enable', '1') message.add_filter('templates', 'template_id', 'TEMPLATE-ALPHA-NUMERIC-ID') -message.add_substitution(':name', 'Elmer') -message.add_to('Elmer Thomas ') +message.add_substitution(':name', 'John') +message.add_to('John Doe This was a successful test of using the SMTPAPI library!') message.set_text('This was a successful test of using the SMTPAPI library!') -message.set_from('Elmer Thomas ') +message.set_from('Jane Doe ') status, msg = sg.send(message) print status print msg