forked from garyanewsome/mailer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailer.rb
84 lines (67 loc) · 2.04 KB
/
mailer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
require 'sinatra'
require 'sinatra/json'
require 'sinatra/cross_origin'
require 'sinatra/jsonp'
require 'active_support'
require 'active_support/core_ext/object/blank.rb'
require 'action_mailer'
require 'uri'
require 'base64'
require 'yaml'
require 'haml'
require 'cgi'
enable :sessions
configure do
set :server, :puma
set :port, 9494
enable :cross_origin
end
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.view_paths = File.expand_path('views')
class Mailer < ActionMailer::Base
default from: '[email protected]'
layout false
def notification(form_data, account_info)
@form_data = form_data
@time = Time.now.getutc
mail(to: account_info[:to],
subject: "New message from #{account_info[:website]}",
template_path: 'mailer',
template_name: 'notification',
content_type: 'text/html'
)
end
end
if File.exists? 'accounts.yaml'
set :accounts, YAML.load_file('accounts.yaml').deep_symbolize_keys!
else
set :accounts, {}
end
post '/' do
if params['public_token']
public_token = params['public_token'].to_sym
account_configuration = settings.accounts[public_token]
redirect '/error' unless account_configuration
else
redirect '/error'
end
ActionMailer::Base.smtp_settings = account_configuration[:smtp]
if params['verify'].nil? || params['verify'].empty?
Mailer.notification(params, account_configuration[:account]).deliver_now
end
after_success = params.fetch("after_success", account_configuration[:account][:after_success])
redirect after_success
end
get '/' do
payloadStr = Base64.decode64(params['payload'])
payload = CGI::parse payloadStr
public_token = payload['public_token'].first.to_sym
account_configuration = settings.accounts[public_token]
ActionMailer::Base.smtp_settings = account_configuration[:smtp]
Mailer.notification(payload, account_configuration[:account]).deliver_now
jsonp [{success: 'Message Sent'}], params['callback']
end
get '/error' do
haml :error
end