-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathemailer.rb
36 lines (29 loc) · 1.56 KB
/
emailer.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
class Emailer
def initialize
@sendgrid = SendGrid::API.new(api_key: ENV["SENDGRID_API_KEY"])
end
def voicemail_notification(url, duration, phone_number)
body = "A new voicemail has arrived from #{phone_number}!\n\nListen to it here: #{url}\n#{duration} seconds long."
message = SendGrid::Mail.new
message.from = SendGrid::Email.new(email: ENV["EMAIL_FROM"])
message.subject = "New Voicemail from #{phone_number}"
personalization = SendGrid::Personalization.new
personalization.add_to(SendGrid::Email.new(email: ENV["EMAIL_TO"]))
message.add_personalization(personalization)
message.add_content(SendGrid::Content.new(type: "text/plain", value: body))
message.add_content(SendGrid::Content.new(type: "text/html", value: body.gsub("\n", "<br>")))
response = @sendgrid.client.mail._("send").post(request_body: message.to_json)
raise "#{response.status_code}: #{response.body}" if response.status_code != "202"
end
def sms_notification(phone_number, body)
message = SendGrid::Mail.new
message.from = SendGrid::Email.new(email: ENV["EMAIL_FROM"])
message.subject = "New SMS from #{phone_number}"
personalization = SendGrid::Personalization.new
personalization.add_to(SendGrid::Email.new(email: ENV["EMAIL_TO"]))
message.add_personalization(personalization)
message.add_content(SendGrid::Content.new(type: "text/plain", value: body))
response = @sendgrid.client.mail._("send").post(request_body: message.to_json)
raise "#{response.status_code}: #{response.body}" if response.status_code != "202"
end
end