Skip to content
This repository was archived by the owner on Jun 9, 2021. It is now read-only.

Commit 17695f8

Browse files
author
raylu
committed
send mail through mailgun
1 parent 6e9711b commit 17695f8

File tree

6 files changed

+58
-4
lines changed

6 files changed

+58
-4
lines changed

Diff for: config.py

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class WebConfig(Config):
1616
'cookie_secret',
1717
'github_client_id',
1818
'github_client_secret',
19+
'mailgun_api_key',
1920
'debug',
2021
])
2122

Diff for: config.yaml.example

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ web:
44
cookie_secret: 'dis is super sekrit'
55
github_client_id: 'a1' # https://github.com/settings/applications
66
github_client_secret: 'abc123'
7+
mailgun_api_key: null
78
debug: true
89

910
db:

Diff for: db.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,15 @@ def get_user_by(self, field, value):
6868
return cursor.fetchone()
6969

7070
@tornado.gen.coroutine
71-
def get_contact_info(self, github_id):
72-
query = 'SELECT type, info FROM contact_info WHERE github_id = %s;'
73-
cursor = yield self.execute(query, github_id)
74-
return cursor.fetchall()
71+
def get_contact_info(self, github_id, info_type=None):
72+
if info_type is None:
73+
query = 'SELECT type, info FROM contact_info WHERE github_id = %s;'
74+
cursor = yield self.execute(query, github_id)
75+
return cursor.fetchall()
76+
else:
77+
query = 'SELECT info FROM contact_info WHERE github_id = %s AND type = %s;'
78+
cursor = yield self.execute(query, github_id, info_type)
79+
return cursor.fetchone()[0]
7580

7681
@tornado.gen.coroutine
7782
def get_questionnaire(self, github_id):

Diff for: server.py

+28
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python3
22

33
import os
4+
import urllib.parse
45

56
import cleancss
67
import tornado.gen
@@ -97,6 +98,32 @@ def get(self):
9798
users = yield self.db.get_userlist()
9899
self.render('users.html', users=users)
99100

101+
class MailHandler(BaseHandler):
102+
@tornado.web.authenticated
103+
@tornado.gen.coroutine
104+
def post(self, username):
105+
from_user = self.current_user
106+
from_email = yield self.db.get_contact_info(from_user['github_id'], db.ContactInfoType.EMAIL)
107+
from_username = from_user['username'].decode('utf-8')
108+
to_user = yield self.db.get_user_by('username', username)
109+
to_email = yield self.db.get_contact_info(to_user['github_id'], db.ContactInfoType.EMAIL)
110+
data = {
111+
'from': '%s <%s>' % (from_username, from_email),
112+
'to': '%s <%s>' % (username, to_email),
113+
'subject': 'lpmc message from %s' % from_username,
114+
'text': self.get_body_argument('body'),
115+
}
116+
request = tornado.httpclient.HTTPRequest(
117+
method='POST',
118+
url='https://api.mailgun.net/v2/lpmc.io/messages',
119+
auth_username='api',
120+
auth_password=config.mailgun_api_key,
121+
body=urllib.parse.urlencode(data),
122+
)
123+
response = yield tornado.httpclient.AsyncHTTPClient().fetch(request)
124+
data = tornado.escape.json_decode(response.body)
125+
self.render('mail.html', username=username, message=data['message'])
126+
100127
class ClaimHandler(BaseHandler):
101128
@tornado.web.authenticated
102129
@tornado.gen.coroutine
@@ -178,6 +205,7 @@ def get(self, css_path):
178205
(r'/github_emails', GithubEmailsHandler),
179206
(r'/logout', LogoutHandler),
180207
(r'/users', UserListHandler),
208+
(r'/users/(.*)/mail', MailHandler),
181209
(r'/users/(.*)/claim', ClaimHandler),
182210
(r'/users/(.*)/unclaim', UnclaimHandler),
183211
(r'/users/(.*)', ProfileHandler),

Diff for: templates/mail.html

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{% extends "base.html" %}
2+
3+
{% block main %}
4+
5+
{{ message }}
6+
<br>
7+
<a href="/users/{{ username }}">
8+
9+
{% end %}

Diff for: templates/profile.html

+10
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,14 @@ <h3>Mentor: <a href="{{ mentor['username'] }}">{{ mentor['username'] }}</a></h3>
3232
{% end %}
3333
{% end %}
3434

35+
<p>
36+
send mail to {{ user['username'] }}
37+
<br>(note that doing so will reveal your email address to {{ user['username'] }})
38+
<form action="/users/{{ user['username'] }}/mail" method="post">
39+
<textarea name="body"></textarea>
40+
<br>
41+
<input type="submit" value="send">
42+
</form>
43+
</p>
44+
3545
{% end %}

0 commit comments

Comments
 (0)