Send emails through AhaSend’s SMTP servers using Ruby’s versatile email solutions. This guide covers three popular approaches: the built-in net/smtp library, the powerful Mail gem, and Rails’ ActionMailer framework.
Ruby’s Email Flexibility: Ruby offers multiple excellent options for email sending, from lightweight standard library solutions to feature-rich frameworks - choose the one that best fits your application’s needs.
The Mail gem provides a high-level, Ruby-style interface for email handling with rich features like attachments, multipart messages, and template support.
Mail Gem Benefits: Clean API, automatic MIME handling, built-in attachment support, and excellent template integration. Perfect for complex email applications.
require 'mail'# Configure Mail gem defaultsMail.defaults do delivery_method :smtp, { address: 'send.ahasend.com', port: 587, user_name: 'YOUR_SMTP_USERNAME', password: 'YOUR_SMTP_PASSWORD', authentication: 'plain', enable_starttls_auto: true }enddef send_plain_text_email begin mail = Mail.new do from '[email protected]' to '[email protected]' subject 'Welcome to our platform!' body 'Thanks for signing up. We\'re excited to have you!' end mail.deliver! puts "Email sent successfully!" rescue => e puts "Failed to send email: #{e.message}" endendsend_plain_text_email
require 'mail'# Configure Mail gem defaultsMail.defaults do delivery_method :smtp, { address: 'send.ahasend.com', port: 587, user_name: 'YOUR_SMTP_USERNAME', password: 'YOUR_SMTP_PASSWORD', authentication: 'plain', enable_starttls_auto: true }enddef send_email_with_special_headers begin mail = Mail.new do from '[email protected]' to ['[email protected]', '[email protected]'] cc '[email protected]' bcc '[email protected]' subject 'Product Updates with Tracking' # Add AhaSend special headers header['ahasend-track-opens'] = 'true' header['ahasend-track-clicks'] = 'true' header['ahasend-tags'] = 'product-update,newsletter,ruby' header['ahasend-message-retention'] = '30' header['ahasend-message-data-retention'] = '7' text_part do body 'Check out our latest features at https://yourdomain.com/features' end html_part do content_type 'text/html; charset=UTF-8' body <<~HTML <html> <body> <h1>Product Updates</h1> <p>Check out our <a href="https://yourdomain.com/features">latest features</a>!</p> <p>This email has tracking enabled and custom tags.</p> </body> </html> HTML end end mail.deliver! puts "Email sent with special headers!" rescue => e puts "Failed to send email: #{e.message}" endendsend_email_with_special_headers
Use sandbox mode to safely test your Ruby email integration across all methods:
Copy
require 'net/smtp'def send_sandbox_test smtp_settings = { address: 'send.ahasend.com', port: 587, domain: 'yourdomain.com', user_name: 'YOUR_SMTP_USERNAME', password: 'YOUR_SMTP_PASSWORD', authentication: 'plain' } message = <<~MESSAGE_END From: [email protected] To: [email protected] Subject: Sandbox Test Email AhaSend-Sandbox: true AhaSend-Sandbox-Result: deliver This email is sent in sandbox mode for testing. MESSAGE_END begin Net::SMTP.start( smtp_settings[:address], smtp_settings[:port], smtp_settings[:domain], smtp_settings[:user_name], smtp_settings[:password], smtp_settings[:authentication] ) do |smtp| smtp.enable_starttls smtp.send_message(message, '[email protected]', '[email protected]') end puts "Sandbox email sent successfully!" rescue => e puts "Failed to send sandbox email: #{e.message}" endendsend_sandbox_test
Sandbox Benefits: Emails sent in sandbox mode are free, trigger webhooks normally, and never actually deliver to recipients - perfect for development and testing.