Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

ruby

How to send an email in Ruby

Dian Us Suqlain

Ruby can be used to send emails by making delivery applications. Ruby also provides an email delivery tool called Action Mailer.

SMTP - Simple Mail Transfer Protocol

Ruby provides a built-in class named Net::SMTP for SMTP that handles sending and routing emails between mail servers.

An SMTP instance includes multiple parameters that define the content, address, port number, domain, and other valuable information about an email (defined in Code - Example 02 below).

Note: SMTP allows us to write an email and define functions to perform operations as we desire. But, this is a drawback as well, as it slows down the operating speed if we start to write an email from scratch.

Code - Example 01

Let’s observe the following code example that shows how to write a simple email using SMTP:

require 'net/smtp'

message = <<END_OF_MESSAGE
From: Person A <sender@example.com>
To: Person B <receiver@example.com>
Subject: SMTP email in Ruby
Date: 8th July, 2021 Thursday 02:00 am

Hello Sir! This email is sent through smtp in Ruby :D
END_OF_MESSAGE

Net::SMTP.start('your.smtp.server', 25) do |smtp|
  smtp.send_message message, 'sender@example.com', 
                             'receiver@example.com'
end
SMTP email syntax in Ruby

Explanation

The above code represents an HTML-based email using SMTP.

The top line states what class is required to run the code below it. The body of the code includes a message with some headers: From, To, Subject and Date.

The Net::SMTPclass includes parameters such as the server name, your.smtp.server, and the port number. 25 is the default port number in SMTP.

send mail process in ruby

The Action Mailer in Ruby

SMTP configuration setup

The Action Mailer is a ROR component that allows sending and receiving emails, though we have to configure the component according to the project needs.

If we want to send an email in HTML format, we should include the following line of code as well:

ActionMailer::Base.default_content_type = "text/html"

If we want to send the message in plain text, we include the following line in the config file:

ActionMailer::Base.default_content_type = "text/plain"

Code - Example 02

To configure the Action Mailer in ROR, we need to include the following line of code in the environment.rb file inside the config folder:

config.action_mailer.delivery_method = :smtp

config.action_mailer.smtp_settings = {

   address:              'example_address.com',
   port:                 3306,
   domain:               'example_domain.com',
   user_name:            '<user>',
   password:             '<pass>',
   authentication:       'plain',
   enable_starttls_auto: true  

}
Config file

Explanation

The first line of code above represents the delivery method of the action mailer, i.e., smtp: Simple Mail Transfer Protocol.

The parameters inside the braces {} specify the port number, mailing address, domain name, authentication, etc.

These parameters are in the form of hash, i.e., (key, value pair), and every parameter should have values according to your project.

RELATED TAGS

ruby

CONTRIBUTOR

Dian Us Suqlain
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring