Created
January 22, 2024 00:40
-
-
Save yuhonas/6987c704d94bf1b3cfc84b13cca1e106 to your computer and use it in GitHub Desktop.
add twilio as a ruby on rails actionmailer delivery method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require Rails.application.root.join('app', 'lib', 'phone_number_formatter') | |
module Mail | |
class TwilioSmsDelivery | |
attr_reader :response | |
def initialize(options) | |
@options = options | |
end | |
def deliver!(mail) | |
twilio_client = Twilio::REST::Client.new | |
@response = twilio_client.messages.create( | |
@options.merge( | |
to: PhoneNumberFormatter.format(Array(mail.to).first), | |
body: mail.body.raw_source, | |
status_callback: UrlHelpers.status_webhooks_twilio_url | |
) | |
) | |
mail.message_id = @response.sid | |
end | |
end | |
end | |
ActionMailer::Base.add_delivery_method :twilio_sms, Mail::TwilioSmsDelivery |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment