SoFunction
Updated on 2025-04-09

Simple tutorial on writing a program to send emails using Ruby

SMTP (Simple Mail Transfer Protocol) is a simple mail transfer protocol. It is a set of rules for transmitting mail from the source address to the destination address, which controls the transit method of letters.

Ruby provides Net::SMTP to send emails and provides two methods new and start:

The new method has two parameters:

  • Server name defaults to localhost
  • port number defaults to 25

The start method has the following parameters:

  • server - SMTP server IP, default is localhost
  • port - port number, default is 25
  • domain - The domain name of the email sender, default is ENV["HOSTNAME"]
  • account - username, default is nil
  • password - user password, default is nil
  • authtype - Verification type, default to cram_md5

The SMTP object instantiation method calls sendmail, and the parameters are as follows:

  • source - anything returned by a string or array or each iterator at any time.
  • sender - A string that appears in the form field of email.
  • recipients - A string or array of strings that represents the address of the recipient.

Example

The following provides a simple Ruby script to send emails:

require 'net/smtp'
 
message = <<MESSAGE_END
From: Private Person <me@>
To: A Test User <test@>
Subject: SMTP e-mail test
 
This is a test e-mail message.
MESSAGE_END
 
Net::('localhost') do |smtp|
 smtp.send_message message, 'me@',
               'test@'
end

In the above example, you have set up a basic email message, paying attention to the correct title format. An email requires From, To and Subject, and a blank line is required between the text content and the header information.

Use Net::SMTP to connect to the SMTP server on the local machine, and use the send_message method to send mail. The method parameters are sender email and receiver email.

If you do not have an SMTP server running on the machine, you can use Net::SMTP to communicate with the remote SMTP server. If using webmail services such as Hotmail or Yahoo Mail, your email provider will provide you with details about the sending mail server:
Net::('')

The above code will connect to a mail server with the host and port number 25. If you need to fill in the username and password, the code is as follows:

Net::('',
        25,
        'localhost',
        'username', 'password' :plain)

The above example uses the specified username and password to connect to a mail server with the host and port number 25.
Send HTML mail using Ruby

Net::SMTP also provides support for sending mail in HTML format.

When sending emails, you can set the MIME version, document type, and character set to send HTML format emails.
Example

The following example is used to send mail in HTML format:

require 'net/smtp'
 
message = <<MESSAGE_END
From: Private Person <me@>
To: A Test User <test@>
MIME-Version: 1.0
Content-type: text/html
Subject: SMTP e-mail test
 
This is an e-mail message to be sent in HTML format
 
<b>This is HTML message.</b>
<h1>This is headline.</h1>
MESSAGE_END
 
Net::('localhost') do |smtp|
 smtp.send_message message, 'me@',
               'test@'
end

Send emails with attachments

If you need to send mixed content emails, you need to set Content-type to multipart/mixed. This allows you to add attachments to your email.

Before transferring, attachments need to use the pack("m") function to convert their contents to base64 format.
Example

The following instance will send a message with the attachment /tmp/:

require 'net/smtp'
 
filename = "/tmp/"
# Read the file and encode it into base64 formatfilecontent = (filename)
encodedcontent = [filecontent].pack("m")  # base64
 
marker = "AUNIQUEMARKER"
 
body =&lt;&lt;EOF
This is a test email to send an attachement.
EOF
 
# Define the main header informationpart1 =&lt;&lt;EOF
From: Private Person &lt;me@&gt;
To: A Test User &lt;test@&gt;
Subject: Sending Attachement
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=#{marker}
--#{marker}
EOF
 
# Define message actionpart2 =&lt;&lt;EOF
Content-Type: text/plain
Content-Transfer-Encoding:8bit
 
#{body}
--#{marker}
EOF
 
# Define attachmentspart3 =&lt;&lt;EOF
Content-Type: multipart/mixed; name=\"#{filename}\"
Content-Transfer-Encoding:base64
Content-Disposition: attachment; filename="#{filename}"
 
#{encodedcontent}
--#{marker}--
EOF
 
mailtext = part1 + part2 + part3
 
# Send emailbegin
 Net::('localhost') do |smtp|
   (mailtext, 'me@',
             ['test@'])
 end
rescue Exception =&gt; e 
 print "Exception occured: " + e 
end 

Note: You can specify multiple sending addresses, but need to be separated by commas.