Skip to content

Instantly share code, notes, and snippets.

@yuhonas
Created January 22, 2024 00:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuhonas/6987c704d94bf1b3cfc84b13cca1e106 to your computer and use it in GitHub Desktop.
Save yuhonas/6987c704d94bf1b3cfc84b13cca1e106 to your computer and use it in GitHub Desktop.
add twilio as a ruby on rails actionmailer delivery method
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