Home »
Ruby Tutorial
Sending email using Ruby
By IncludeHelp Last updated : December 03, 2024
Ruby sending email
Sending emails and routing email among mail servers are handled by Simple Mail Transfer Protocol commonly known as SMTP. Net::SMTP class is a predefined class in Ruby’s library which is purposefully defined for Simple Mail Transfer Protocol.
Net::SMTP has two methods namely new and start. The parameter specification goes as follows,
Methods in Net::SMTP Class
1. new
It consumes two parameters, the first one is a server which is by default 'localhost' and the port number which is by default 25.
2. start
It consumes six parameters namely server, port, domain, account, password, and authtype. The default value of server is localhost, the port is 25, the domain is ENV["HOSTNAME"], account and password is nil and authtype is cram_md5.
sendmail is a method of an SMTP object which takes three parameters namely The source, The sender and the recipient.
Example: Sending a Simple Email
Now, let us understand a very simple way to send one email using Ruby code. refer the example given below,
require 'net/smtp'
message = <<MESSAGE_END
From: Includehelp <[email protected]>
To: Hrithik sinha <[email protected]>
Subject: Urgent Information
Hello there, Greeting from Includehelp.
We hope, you are doing good. Have a great day.
Read articles at Includhelp for best programming guidance.
MESSAGE_END
Net::SMTP.start('localhost') do |smtp|
smtp.send_message message, '[email protected]', '[email protected]'
end
Explanation
The code given above is a very basic way to send email using Ruby. We have integrated a very simple email message. It is required to take care of the format of headers properly. We all know that an email requires three things namely a From, To and Subject which should be identical from the body of an email with the help of a blank line.
Sending an HTML Email
For sending an email, we are supposed to use Net::SMTP class which allows us to connect with the SMTP server on our machine and then make use of send_message method with the message specified, the sender’s address and the receiver’s address as parameters.
If you want to send an HTML file using Ruby script, that file will be treated as a normal document even after including HTML tags in the documents. Net::SMTP class provides you a feature to send an HTML message with the help of an email. Refer the following code for instance.
Example
require 'net/smtp'
message = <<MESSAGE_END
From: Hrihik Sinha <[email protected]>
To: Saksham Sharma <[email protected]>
MIME-Version: 1.0
Content-type: text/html
Subject: An exemplary email
hey bro,
This is an example. Read articles at Includehelp.com for better understanding of concepts.
<h1>Write heading here</h1>
<p>This is a paragraph</p>
<hr>
MESSAGE_END
Net::SMTP.start('localhost') do |smtp|
smtp.send_message message, '[email protected]', '[email protected]'
end
You are supposed to specify Mime version, the type of content and character set if you want to email an HTML text using Ruby script.