Sending Emails with SMTP in Ruby
Send emails through AhaSend’s SMTP servers using Ruby’s versatile email solutions. This guide covers three popular approaches: the built-innet/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.
Prerequisites
Before you begin, ensure you have:System Requirements
System Requirements
- Ruby installed on your system (Download Ruby)
- Ruby 2.7 or higher (recommended)
- Basic knowledge of Ruby programming
- Access to install gems (if using Mail gem or Rails)
AhaSend Setup
AhaSend Setup
- Domain verified in your AhaSend account
- SMTP credentials created (username and password)
- Access to your AhaSend dashboard for credential management
Need SMTP Credentials? If you haven’t created SMTP credentials yet, check out our SMTP Credentials guide for step-by-step instructions.
Ruby Email Solutions Overview
Ruby provides multiple excellent options for sending emails via SMTP:Standard Library
Best for: Simple, lightweight email sending
Pros: No dependencies
Cons: Verbosity
Use case: Scripts
Pros: No dependencies
Cons: Verbosity
Use case: Scripts
Mail Gem
Best for: non-Rails applications
Pros: High-level API
Cons: Ext. dependency
Use case: Standalone apps
Pros: High-level API
Cons: Ext. dependency
Use case: Standalone apps
ActionMailer (Rails)
Best for: Rails applications
Pros: Rails integration, templates, testing
Cons: Rails-specific
Use case: Web applications, MVC pattern
Pros: Rails integration, templates, testing
Cons: Rails-specific
Use case: Web applications, MVC pattern
Connection Settings
Use these settings for all Ruby SMTP configurations with AhaSend:Primary Server
Host:
Ports: 587 (recommended), 25, 2525
Security: STARTTLS
Authentication: Required
send.ahasend.comPorts: 587 (recommended), 25, 2525
Security: STARTTLS
Authentication: Required
US Server
Host:
Ports: 587 (recommended), 25, 2525
Security: STARTTLS
Authentication: Required
send-us.ahasend.comPorts: 587 (recommended), 25, 2525
Security: STARTTLS
Authentication: Required
Method 1: Using net/smtp (Standard Library)
Thenet/smtp library is part of Ruby’s standard library, offering a lightweight solution for sending emails without external dependencies.
Standard Library Advantage:
net/smtp comes with Ruby - no gem installation required! Perfect for simple scripts and lightweight applications.Basic Examples
Method 2: Using the Mail Gem
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.
Installation
Add the Mail gem to your project:Basic Examples
Advanced Mail Gem Features
Method 3: Using ActionMailer (Rails)
ActionMailer is part of Ruby on Rails and provides a comprehensive email solution with MVC patterns, templates, and excellent testing support.Rails Integration: ActionMailer seamlessly integrates with Rails applications, providing view templates, internationalization, background job support, and comprehensive testing utilities.
Configuration
Configure your SMTP settings in the appropriate environment file:Basic Mailer Setup
Testing with Sandbox Mode
Use sandbox mode to safely test your Ruby email integration across all methods:Sandbox Benefits: Emails sent in sandbox mode are free, trigger webhooks normally, and never actually deliver to recipients - perfect for development and testing.
Resources
Ruby net/smtp Documentation
Official net/smtp library documentation
Mail Gem Documentation
Comprehensive Mail gem guide and examples
ActionMailer Guide
Official Rails ActionMailer documentation
AhaSend Support
Get help from our engineering team

