UCONN

UCONN
UCONN

SendGrid email

 Email in python app engine using sendgrid


Establish sendgrid account


www.sendgrid.com






Create a single sender





API Keys

Your application, mail client, or website can all use API (Application Programming Interface) keys to authenticate access to SendGrid services. They are the preferred alternative to using a username and password because you can revoke an API key at any time without having to change your username and password. We suggest that you use API keys for connecting to all of SendGrid’s services.

Types of API keys

There are 3 different types of API keys:

  1. Full Access allows the API key to access GET, PATCH, PUT, DELETE and POST endpoints for all parts of your account, excluding billing and Email Address Validation.

  2. Restricted Access customizes levels of access for all parts of your account, excluding billing and Email Address Validation.

  3. Billing Access allows the API key to access billing endpoints for the account.

API key permissions

During the API key creation process, you will be given the option of selecting scopes, or specific permissions, that you would like to assign to your new API key. These permissions restrict which areas of your account your API key will be able to access. When assigning permissions to your API key, you will be given the option to select one of the following levels of access:

  • No Access prevents the API key from accessing any endpoint within the selected permission.

  • Read Access allows the API key to access GET endpoints within the selected permission.

  • Full Access allows the API key to access GET, PATCH, PUT, DELETE, and POST endpoints within the selected permission.

We require that you create a separate API key for making billing-related API calls. This segmentation adds an extra level of security by giving you more control over who has access to the various areas of your account.

Managing API keys

When viewing the API keys page, you will see a list of your current API keys along with the following information:

Name - The name you defined for your API key.

API Key ID - The way you would reference your API key for management through the API (e.g. editing or deleting a key).

Action - Actions you can perform on your API keys, such as editing or deleting the key.

Creating an API key

  1. Navigate to Settings on the left navigation bar, and then select API Keys.

  2. Click Create API Key.

  3. Give your API key a name.

  4. Select Full Access, Restricted Access, or Billing Access.

  5. If you're selecting Restricted Access, or Billing Access, select the specific permissions to give each category. For more information, see API key permissions.

  6. Click Create & View.

  7. Copy your API key somewhere safe. For security reasons, do not put it directly in your code, or commit it somewhere public like GitHub.

You will only be shown your API key one time. Please store it somewhere safe as we will not be able to retrieve or restore it.

There is a limit of 100 API Keys per account.


Storing an API key in an environment variable

Twilio SendGrid recommends storing your API key in an environment variable or a config file that is not stored in version control.

When setting a variable in your program, that variable is readable by any person or system that can access the text file where it’s set. However, a variable that's confined to the environment where the code is executed, stored outside the program itself, is called an environment variable. Only people and programs with access to the environment can read the value assigned to an environment variable. This makes environment variables a more secure choice for storing credentials such as API keys.

Once you assign your API key to an environment variable, you can then tell your program to look for and use that variable. Another benefit of this approach is the ability to set API keys with different permissions in different environments such as development, staging, and production without changing the code you deploy to those environments.

The examples below show how to store your key in a variable named SENDGRID_API_KEY. You can find additional examples in the Twilio Documentation.

To store your SendGrid API key, open your terminal or command prompt and add the following:

export SENDGRID_API_KEY=the_key_you_copied_from_SendGrid

setx SENDGRID_API_KEY the_key_you_copied_from_SendGrid

To then access the variable, you can use the convention provided by your coding language.

var apiKey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY");

apiKey := os.Getenv("SENDGRID_API_KEY")

apiKey = System.getenv("SENDGRID_API_KEY");

const apiKey = process.env.SENDGRID_API_KEY;

$apiKey = getenv('SENDGRID_API_KEY');

api_key = os.environ.get('SENDGRID_API_KEY')

api_key = ENV['SENDGRID_API_KEY']

Editing an API key

Click the action menu in the same row as the key you would like to edit. From here you can delete a key, making it completely inactive, or you can edit your key’s name and permissions.

Deleting an API key

Once you delete a key, it can no longer be used to access SendGrid’s services.

Click the action menu in the same row as the key you want to delete. Select Delete. This will delete the key permanently, making it inactive. SendGrid will reject any subsequent API calls using this deleted API key.

Replacing an old API key with a new one

  1. Locate the API key you would like to replace in your list of keys and select the action menu drop down at the right on the same row, then select Delete API key.

  2. Next, create an API key.

  3. Replace the old API key with the new one in your code.

You may not give an API key greater permissions than you currently have.

Testing an API key

You can test your newly created API key using cURL:

curl -i --request POST \

--url https://api.sendgrid.com/v3/mail/send \

--header 'Authorization: Bearer YOUR_API_KEY_HERE' \

--header 'Content-Type: application/json' \

--data '{"personalizations": [{"to": [{"email": "recipient@example.com"}]}],"from": {"email": "sendeexampexample@example.com"},"subject": "Hello, World!","content": [{"type": "text/plain", "value": "Howdy!"}]}'





A verified sender account has to be established


Single Sender Verification

Verify ownership of a single email address to use as a sender. Learn more

Verify a Single Sender

STATUS

DOMAIN

Verified

jiacovacci@hotmail.com

I used my hotmail address and now I send all emails from this account


I used WEB API to send emails


Need to generate api key to send from app engine


In code below I hard coded my API key in main.py


need to import sendgrid from linux shell command line


pip3 install sendgrid


https://docs.sendgrid.com/ui/account-and-settings/api-keys#creating-an-api-key


Using this program format you should now be able to loop thru your client file and send translated emails to each client



requirements.txt

Flask==1.1.1

PyYAML>=4.2b1

python-http-client>=3.2.1

six==1.11.0

pytest==3.8.2

starkbank-ecdsa>=1.0.0

sendgrid==v6.0.0

 

app.yaml

runtime: python37

 

main.py

import sendgrid

import os

from sendgrid import SendGridAPIClient

from sendgrid.helpers.mail import Mail

 

 

from sendgrid.helpers.mail import Mail, Email, To, Content

 

 

from flask import Flask, request, render_template

# Imports the Google Cloud client library

# If `entrypoint` is not defined in app.yaml, App Engine will look for an app

# called `app` in `main.py`.

 

app = Flask(__name__)

@app.route('/', methods=['POST','GET'])         # This defines when the function below will be called

 

def run_quickstart():

    if request.method == 'POST':

        data = request.form.to_dict(flat=True)  

        Comments = data['mycomments'] # Sends data to the form

        sg = sendgrid.SendGridAPIClient('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')

        from_email = Email("jiacovacci@hotmail.com"# Change to your verified sender

        to_email = To("john.iacovacci1@gmail.com"# Change to your recipient

        subject = "Did this work?"

        content = Content("text/plain", Comments)

        mail = Mail(from_email, to_email, subject, content)

        mail_json = mail.get()

        # Send an HTTP POST request to /mail/send

        response = sg.client.mail.send.post(request_body=mail_json)

        print(response.status_code)

        print(response.headers)

        return render_template('comments.html', FormComments=Comments)   # Renders the page with the response

    else:

        # This code will run if a GET is received

        return render_template('index.html')

 

if __name__ == "__main__":

    # This is used when running locally only. When deploying to Google App

    # Engine, a webserver process such as Gunicorn will serve the app. This

    # can be configured by adding an `entrypoint` to app.yaml.

    app.run(host="127.0.0.1", port=8080, debug=True)

 

 

templates

 

index.html

 

<!DOCTYPE html>

<html lang="en">

 

 

<body>

 

 

<h2>Input</h2>

<form method="post" action="/">

Comments: <textarea name="mycomments" rows="10" cols="30"> </textarea><br> 

 <button type="submit">Submit</button>

 </form>

 

</body>

</html>

 

comments.html

<!doctype html>

<h2>Comments</h2>

            <p>

                {% if FormComments %}

                    {{ FormComments }}

                {% else %}

                    Enter some text above.

                {% endif %}

            </p>

            </html>

 

 

 

 


No comments:

Post a Comment

Disable Billing

Search for Billing Manage billing accounts Go to MYPROJECTS CLICK ON THE 3 BUTTON Actions Then hit disable