How to Send Email with Ruby

Send transactional email from Ruby with Net::HTTP — works in Rails apps and standalone scripts.

Prerequisites

Set MAILSURGE_API_KEY in your environment and verify your sending domain. The from address must match the verified domain on your API key.

Ruby Net::HTTP example

Build a JSON body, set the Bearer header, and expect status 201 on success.


                                                1
                                require 'json'
                                                2
                                require 'net/http'
                                                3
                                
                                                4
                                uri = URI('https://mailsurge.dev/api/v1/message')
                                                5
                                request = Net::HTTP::Post.new(uri)
                                                6
                                request['Authorization'] = "Bearer #{ENV.fetch('MAILSURGE_API_KEY')}"
                                                7
                                request['Content-Type'] = 'application/json'
                                                8
                                request.body = {
                                                9
                                  from: 'Acme <no-reply@yourdomain.com>',
                                                10
                                  to: ['customer@example.com'],
                                                11
                                  subject: 'Welcome',
                                                12
                                  html: '<p>Your account is now active.</p>',
                                                13
                                  text: 'Your account is now active.'
                                                14
                                }.to_json
                                                15
                                
                                                16
                                response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
                                                17
                                  http.request(request)
                                                18
                                end
                                                19
                                
                                                20
                                raise "Mailsurge request failed: #{response.code} #{response.body}" unless response.code == '201'
                    

Rails tip

Trigger sends from Active Job so request threads stay fast. Store the Mailsurge client in a service object and inject it where mailers or domain events need to fire.

Pair this integration with the deliverability checklist before increasing volume.

Continue reading

Related guides to help you ship production-ready transactional email.

Sending Email with Node.js

Official @mailsurge/node quickstart for modern Node runtimes.

Read guide

Ready to send your first email?

Verify a domain, create an API key, and call POST /api/v1/message from your stack.