Ruby Send Mail - SMTP

Simplified Mail Transfer Protocol (SMTP) is a set of rules for transferring mail from source address to destination address, which controls the way the mail is transferred.

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


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 sender's domain name, default is ENV["HOSTNAME"]
  • account - username, default is nil
  • password - User password, default is nil
  • authtype - Authentication type, default cram_md5

The SMTP object instantiation method calls sendmail with the following parameters:

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


A simple Ruby script is provided below to send mail:


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

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

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

If you don't have an SMTP server running on this machine, you can use Net::SMTP to communicate with a remote SMTP server. If you use a webmail service (such as Hotmail or Yahoo Mail), your email provider will provide you with the details of the sending mail server:


The above code will connect to the mail server with mail.your-domain.com and port number 25. 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 the specified username and password to connect to the mail server with the host number of mail.your-domain.com and port number 25.

Send HTML mail using Ruby

Net::SMTP also provides support for sending HTML-formatted messages.

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


The following example is used to send an HTML-formatted message:


require ' net/smtp' message = <<MESSAGE_END< /span> From: Private Person <me@fromdomain. Com> To: A Test User <test@todomain .com> 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::SMTP.start('localhost') do |smtp| Smtp.send_message message, 'me@fromdomain.com', 'test@todomain.com' End

Send an email with attachments

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

Attachments need to use the pack("m") function to convert their content to base64 format before transfer.


The following example will send an email with the attachment /tmp/test.txt:


require ' net/smtp' filename= "/tmp/test.txt" # Read the file and encode it in base64 format filecontent= File.read(filename) encodedcontent = [filecontent].pack("m ") # base64 marker= "AUNIQUEMARKER" body =<<EOF< /span> This is a test email to send an attachement. EOF # Define primary head information< /span> part1 =<<EOF< /span> From: Private Person <me@fromdomain. Net> To: A Test User <test@todmain .com> Subject: Sending Attachement MIME-Version: 1.0 Content-Type: multipart/mixed; boundary=#{marker} --#{marker} EOF # Define message action Part2 =<<EOF Content-Type: text/plain Content-Transfer-Encoding:8bit #{body} --#{marker} EOF # Defining attachments part3 =<<EOF< /span> Content-Type: multipart/mixed; name=\"#{filename}\" Content-Transfer-Encoding:base64 Content-Disposition: connection; filename="#{filename}" #{encodedcontent} - #{marker}- - EOF Mailtext = part1 + part2 + part3 # send email Start Net::SMTP.start('localhost') do |smtp| Smtp.sendmail(mailtext, 'me@fromdomain.net', ['test@todmain.com']) End Salvage Exception => e Print "Special case occured: " + e End

Note: You can determine numerous delivers to send, however separate them with commas.

welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.