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/mailing list #47

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
32 changes: 32 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@
from secrets import token_bytes
from address import get_addresses
from slugify import slugify
from requests.exceptions import HTTPError

import json
import logging
import os
import sys
import requests

app = Flask(__name__)
app.logger.addHandler(logging.StreamHandler(sys.stdout))
Expand All @@ -43,6 +45,20 @@
app.secret_key = skey


requests.packages.urllib3.util.ssl_.DEFAULT_CIPHERS += "HIGH:!DH:!aNULL"
try:
requests.packages.urllib3.contrib.pyopenssl.DEFAULT_SSL_CIPHER_LIST += (
"HIGH:!DH:!aNULL"
davidswarbrick marked this conversation as resolved.
Show resolved Hide resolved
)
except AttributeError:
# no pyopenssl support used / needed / available
pass

url = "https://us10.api.mailchimp.com/3.0/lists/{}/members/".format(
os.environ["MCLIST_ID"]
)


@app.before_request
def force_https():
criteria = [
Expand Down Expand Up @@ -129,3 +145,19 @@ def display_template(template_slug):
matching_templates, name, postcode, address
)
return render_template("single_email.html", email=email_template[0])


@app.route("/newsletter", methods=["POST"])
def subscribe_to_newsletter():

post_params = {"email_address": request.form["email"], "status": "subscribed"}
r = requests.post(
url, auth=("foo", os.environ["MAILCHIMP_SECRET_KEY"]), json=post_params
davidswarbrick marked this conversation as resolved.
Show resolved Hide resolved
)
try:
r.raise_for_status()
except HTTPError:
return jsonify(status="failed")

results = r.json()
return jsonify(status="success")
103 changes: 99 additions & 4 deletions templates/results.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,112 @@ <h5 style="color: white;">{{ e.name }} to {{ e.target["name"] }} </h5>
{{ e.body }}
</textarea>
<!-- <button class="btn btn-dark" role="button" onclick="_{{ loop.index }}()">Send to {{ e['email'] }}</button> -->
<a class="btn btn-dark mb-1" href="mailto:{{e.target['email']}}?subject={{e.mailto_subject}}&body={{e.mailto_body}}" role="button">Open in default email app</a>
<a class="btn btn-dark mb-1" id="default-mail-button" href="mailto:{{e.target['email']}}?subject={{e.mailto_subject}}&body={{e.mailto_body}}" role="button">Open in default email app</a>

<a class="btn btn-danger btn-email mb-1" onclick="copy_compose('gmail', {{loop.index}}, '{{e.target['email']}}', '{{e.mailto_subject}}')" role="button">Open in Gmail</a>
<a class="btn btn-primary btn-email mb-1" onclick="copy_compose('outlook', {{loop.index}}, '{{e.target['email']}}', '{{e.mailto_subject}}')" role="button">Open in Outlook</a>
<a class="btn btn-info btn-email mb-1" onclick="copy_compose('yahoo', {{loop.index}}, '{{e.target['email']}}', '{{e.mailto_subject}}')"role="button">Open in Yahoo Mail</a>
<a class="btn btn-danger btn-email mb-1" data-toggle="modal" data-target="#newsletterModal" onclick="copy_compose('gmail', {{loop.index}}, '{{e.target['email']}}', '{{e.mailto_subject}}')" role="button">Open in Gmail</a>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are the data-toggle and data-target fields necessary to be added on the buttons to send a template with a specific email provider?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the data-toggle and data-target trigger the modal pop up to display, I could make so that there is a listener for these buttons instead but that seems to add unnecessary js code.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for clarifying on that, agree it's preferable to a listener, however I was questioning what the effect will be - is it so that when someone clicks "Open in Gmail" they get a popup asking them to subscribe to the mailing list? Because that doesn't seem ideal and wouldn't capture people on mobile (for whom these buttons don't show, but make up a large chunk of the users).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rafeeJ since you've tried it out perhaps you could address this concern? otherwise very happy to approve it for staging

<a class="btn btn-primary btn-email mb-1" data-toggle="modal" data-target="#newsletterModal" onclick="copy_compose('outlook', {{loop.index}}, '{{e.target['email']}}', '{{e.mailto_subject}}')" role="button">Open in Outlook</a>
<a class="btn btn-info btn-email mb-1" data-toggle="modal" data-target="#newsletterModal" onclick="copy_compose('yahoo', {{loop.index}}, '{{e.target['email']}}', '{{e.mailto_subject}}')"role="button">Open in Yahoo Mail</a>

<br>
{% endfor %}
</div>

<div class="modal fade" id="newsletterModal" tabindex="-1" role="dialog" aria-labelledby="newsletterModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="newsletterModalLabel">Newsletter</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<div>
Consider subscribing to our newsletter to receive updates about upcoming features such as Template Maker!
</div>
<form role="form" id="newsletter-form">
<div class="form-group">
<label class="control-label">E-Mail Address</label>
<div>
<input type="email" class="form-control" name="email" placeholder="[email protected]">
</div>
</div>

<div class="btn_toolbar">
<input class="btn btn-success mb-1" type="submit" value="Subscribe">
<button class="btn btn-danger mb-1" data-dismiss="modal">Dismiss</button>
</div>
</form>


</div>
</div>
</div>
</div>


<div class="modal fade" id="resultModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Newsletter</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<div id="result">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>

<script type="text/javascript">

function subSubmit(ev)
{
ev.preventDefault();
var request = new XMLHttpRequest();
request.addEventListener('load', subShow);
request.open('POST', {{ url_for('subscribe_to_newsletter')|tojson }});
request.send(new FormData(this));
$('#newsletterModal').modal('hide');
}

function subShow()
{
var data = JSON.parse(this.responseText);
if (data.status == 'success')
{
var div = document.getElementById("result");
var content = document.createTextNode("You have succesfully subscribed to our newsletter, Thank you!");
div.appendChild(content)
$('#resultModal').modal('show');
davidswarbrick marked this conversation as resolved.
Show resolved Hide resolved
}
else if (data.status == 'failed')
{
var div = document.getElementById("result");
var content = document.createTextNode("There was an issue with adding you to the newsletter, please try again later.");
div.appendChild(content)
$('#resultModal').modal('show');
}
}



var form = document.getElementById('newsletter-form');
form.addEventListener('submit', subSubmit);

var button = document.getElementById('default-mail-button');
button.addEventListener('click', function()
{
$('#newsletterModal').modal('show');
});

function copy_compose(client, index, to, subject) {

var body = document.getElementById(`email${index}`);
Expand Down