Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/adds send another email #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ __pycache__/
.Python
env/
build/
bin/
lib/
include/
develop-eggs/
dist/
downloads/
Expand All @@ -34,6 +37,7 @@ wheels/
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
pip-selfcheck.json

# Unit test / coverage reports
htmlcov/
Expand Down
28 changes: 8 additions & 20 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import os

import sendgrid
from flask import Flask, request
from flask import Flask, request, render_template
from sendgrid.helpers.mail import *

SENDGRID_API_KEY = os.environ.get("SENDGRID_API_KEY")

app = Flask(__name__)


Expand All @@ -18,25 +17,14 @@ def mail():
subject = request.form.get("subject")
content = Content("text/plain", request.form.get("content"))
mail = Mail(from_email, subject, to_email, content)
response = sg.client.mail.send.post(request_body=mail.get())
if response.status_code == 202:
return "Email sent successfully!"
else:
return "Status Code: " + str(response.status_code)

try:
response = sg.client.mail.send.post(request_body=mail.get())
return render_template('success.html')
except:
return render_template('error.html', status_code="400")
else:
return """
<html>
<body>
<form method = "POST">
<p>From: <input type = "text" name = "from_email" value="test@example.com" style="width: 500px;" /></p>
<p>To: <input type = "text" name = "to_email" value="test@example.com" style="width: 500px;" /></p>
<p>Subject: <input type = "text" name = "subject" value="Sending with SendGrid is Fun" style="width: 500px;" /></p>
<p>Content: <input type ="text" name = "content" value="and easy to do anywhere, even with Python" style="width: 500px;" /></p>
<p><input type = "submit" value = "send email" /></p>
</form>
</body>
</html>
"""
return render_template('index.html')


if __name__ == "__main__":
Expand Down
Empty file added static/style.css
Empty file.
6 changes: 6 additions & 0 deletions templates/error.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% extends "layout.html" %}

{% block content %}
<h2>Status code: {{ status_code }}</h2>
<p>Failed to send email</p>
{% endblock %}
11 changes: 11 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% extends "layout.html" %}

{% block content %}
<form method = "POST">
<p>From: <input type = "text" name = "from_email" value="test@example.com" style="width: 500px;" /></p>
<p>To: <input type = "text" name = "to_email" value="test@example.com" style="width: 500px;" /></p>
<p>Subject: <input type = "text" name = "subject" value="Sending with SendGrid is Fun" style="width: 500px;" /></p>
<p>Content: <input type ="text" name = "content" value="and easy to do anywhere, even with Python" style="width: 500px;" /></p>
<p><input type = "submit" value = "send email" /></p>
</form>
{% endblock %}
13 changes: 13 additions & 0 deletions templates/layout.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
<title>SENDGRID-FLASK-HEROKU</title>
</head>
<body>
<div id="content">
{% block content %}
{% endblock %}
</div>
</body>
</html>
6 changes: 6 additions & 0 deletions templates/success.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% extends "layout.html" %}

{% block content %}
<h2>Email sent successfully!</h2>
<p><a href="{{ url_for('mail') }}">Send other email</a></p>
{% endblock %}