UCONN

UCONN
UCONN

Saturday, July 31, 2021

sendgrid email process, 7/31/2021, 11:15 AM, English, 7/31/2021, 11:15 AM

Your assigned language is: English
Classroom blog: googleclouduconn.blogspot.com

7/31/2021, 11:15 AM

Email in python app engine using sendgrid

Establish sendgrid account

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


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

https://docs.sendgrid.com/ui/account-and-settings/api-keys#storing-an-api-key-in-an-environment-variable
https://docs.sendgrid.com/for-developers/sending-email/quickstart-python
https://docs.sendgrid.com/for-developers/sending-email/api-getting-started
https://cloud.google.com/community/tutorials/send-email-with-sendgrid-and-nodejs-on-google-app-engine
https://cloud.google.com/appengine/docs/standard/python3/sending-messages
https://docs.sendgrid.com/ui/account-and-settings/api-keys#storing-an-api-key-in-an-environment-variable
https://github.com/sendgrid/sendgrid-python
https://docs.sendgrid.com/for-developers


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