site/controllers/email.moon

44 lines
1.2 KiB
Plaintext
Raw Normal View History

2015-02-01 07:00:52 +00:00
lapis = require "lapis"
csrf = require "lapis.csrf"
2015-02-01 06:17:10 +00:00
http = require "lapis.nginx.http"
2015-02-01 07:00:52 +00:00
mime = require "mime"
2015-02-01 06:17:10 +00:00
2015-02-01 07:00:52 +00:00
import respond_to from require "lapis.application"
import assert_valid from require "lapis.validate"
2015-02-03 20:34:20 +00:00
class Email extends lapis.Application
[email: "/contact/email"]: respond_to {
2015-02-01 07:00:52 +00:00
GET: =>
@csrf_token = csrf.generate_token @
return render: true
POST: =>
csrf.assert_token @
2015-02-01 06:17:10 +00:00
2015-02-01 07:00:52 +00:00
assert_valid @params, {
{ "name", exists: true, min_length: 3 }
{ "message", exists: true, min_length: 15 }
{ "email", exists: true, min_length: 3 }
}
2015-02-01 06:17:10 +00:00
2015-02-01 07:00:52 +00:00
res, code = http.simple{
url: "https://api.mailgun.net/v2/mailgun.xeserv.us/messages"
headers: {
"authentication": "Basic " .. (mime.b64 os.getenv "MAILGUN_KEY")
}
2015-02-01 07:05:25 +00:00
method: "POST"
2015-02-01 07:00:52 +00:00
body: {
2015-02-03 00:46:13 +00:00
from: "#{@params.name} <siteusernoreply@mailgun.xeserv.us>",
2015-02-01 07:00:52 +00:00
to: "xena@yolo-swag.com",
2015-02-03 18:27:15 +00:00
subject: "Contact on https://christine.website",
2015-02-01 07:00:52 +00:00
text: "Email from #{@params.name}:\n\n#{@params.message}\n\nPlease reply to #{@params.email}."
}
}
2015-02-01 06:17:10 +00:00
2015-02-01 07:00:52 +00:00
if code != 200
@err = res
return render: "error", status: 500
2015-02-01 06:17:10 +00:00
2015-02-01 07:00:52 +00:00
return render: "success"
}