English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Ruby Send Email SMTP

SMTP (Simple Mail Transfer Protocol) is a set of rules used to transmit emails from the source address to the destination address. It controls the way emails are transferred.

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

new The method has two parameters:

  • server name Default is localhost

  • port number Default is  25

start The method has the following parameters:

  • server  - SMTP server IP, default is localhost

  • port  - Port number, default is 25

  • domain - Email sender domain, default is ENV["HOSTNAME"]

  • account - Username, default is nil

  • password - User password, default is nil

  • authtype - Authentication type, default is cram_md5

The example method of the SMTP object calls sendmail, with the parameters as follows:

  • source  - A string or array or anything returned by any iterator at any time.

  • sender -A string that appears in the email's form field.

  • recipients - A string or string array representing the recipient's address.

Online Example

The following provides a simple Ruby script to send emails:

Online Example

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

In the above example, you have set up a basic email message, note the correct title format. An email must have From, To, and Subject, and there should be a blank line between the text content and the header information.

Connect to the local machine's SMTP server using Net::SMTP and send the email using the send_message method, with the method parameters being the sender's email and the recipient's email.

If you do not have an SMTP server running on your local machine, you can use Net::SMTP to communicate with a remote SMTP server. If you use a web-based email service (such as Hotmail or Yahoo Mail), your email provider will provide you with the details of the sending email server:

Net::SMTP.start('mail.your-domain.com')

The above code connects to the host mail.your-domain.com, with the port number of 25email server, if you need to fill in the username and password, the code is as follows:

Net::SMTP.start('mail.your-domain.com', 
                25, 
                'localhost', 
                'username', 'password', :plain)

The above example uses a specified username and password to connect to the host mail.your-domain.com, with the port number of 25email server.

Sending HTML Emails with Ruby

Net::SMTP also provides support for sending HTML formatted emails.

When sending an email, you can set the MIME version, document type, and character set to send an HTML formatted email.

Online Example

The following examples are used to send emails in HTML format:

Online Example

require 'net/smtp'
 
message = <<MESSAGE_END
From: Private Person <[email protected]>
To: A Test User <[email protected]>
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 an HTML message.</b>
<h1>This is headline.</h1>
MESSAGE_END
 
Net::SMTP.start('localhost') do |smtp|
  smtp.send_message message, '[email protected]', 
                             '[email protected]'
end

Send an email with attachment

If you need to send an email with mixed content, you need to set the Content-type is multipart/mixed. This allows you to add attachment content to the email.

The attachment needs to be encoded before transmission pack("m") The function converts its content to base64 Format.

Online Example

The following example will send the attachment as /tmp/Email of test.txt:

Online Example

require 'net/smtp'
 
filename = "/tmp/test.txt"
# Read the file and encode it as base64Format
filecontent = File.read(filename)
encodedcontent = [filecontent].pack("m")  # base64
 
marker = "AUNIQUEMARKER"
 
body =<<EOF
This is a test email to send an attachment.
EOF
 
# Define the main header information
part1 =<<EOF
From: Private Person <[email protected]>
To: A Test User <[email protected]>
Subject: Sending Attachement
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="#{marker}"
--#{marker}
EOF
 
# Define the message action
part2 =<<EOF
Content-Type: text/plain
Content-Transfer-Encoding:8bit
 
#{body}
--#{marker}
EOF
 
# Define the attachment part
part3 =<<EOF
Content-Type: multipart/mixed; name="#{filename}"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="#{filename}"
 
#{encodedcontent}
--#{marker}--
EOF
 
mailtext = part1 + part2 + part3
 
# Send email
begin 
  Net::SMTP.start('localhost') do |smtp|
     smtp.sendmail(mailtext, '[email protected]',
                          ['[email protected]'])
  end
rescue Exception => e  
  print "Exception occurred: " + e  
end

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