YouTip LogoYouTip

Ruby Sending Email

SMTP (Simple Mail Transfer Protocol) is a set of rules for transferring emails from a source address to a destination address, controlling the way messages are relayed. Ruby provides `Net::SMTP` for sending emails and offers 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, defaults to `localhost` * _port_ - port number, defaults to `25` * _domain_ - sender's domain, defaults to `ENV` * _account_ - username, defaults to `nil` * _password_ - user password, defaults to `nil` * _authtype_ - authentication type, defaults to `_cram_md5_` The SMTP object instantiation method calls `sendmail` with the following parameters: * _source_ - a string, array, or anything returned by an iterator at any time. * _sender_ - a string that appears in the email's form fields. * _recipients_ - a string or array of strings representing the recipient addresses. ### Example The following provides a simple Ruby script to send an email: ## Example ```ruby require 'net/smtp' message = <<MESSAGE_END From: Private Person To: A Test User 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' end In the above example, you have set up a basic email message. Note the correct header format. An email requires From, To, and Subject headers, with a blank line separating the headers from the text content. The code uses `Net::SMTP` to connect to the SMTP server on the local machine and the `send_message` method to send the email, with parameters for the sender's and recipient's email addresses. If you don't have an SMTP server running on your local machine, you can use `Net::SMTP` to communicate with a remote SMTP server. If you are using a webmail service (like Hotmail or Yahoo Mail), your email provider will give you the details for the outgoing mail server: ```ruby Net::SMTP.start('mail.your-domain.com') The above code will connect to the mail server at host `mail.your-domain.com` on port `25`. If a username and password are required, the code would be: ```ruby 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 at host `mail.your-domain.com` on port `25`. * * * ## Sending HTML Email with Ruby `Net::SMTP` also supports sending emails in HTML format. When sending an email, you can set the MIME version, document type, and character set to send an HTML-formatted email. ### Example The following example sends an email in HTML format: ## Example ```ruby require 'net/smtp' message = <<MESSAGE_END From: Private Person To: A Test User 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 This is HTML message.

This is headline.

MESSAGE_END Net::SMTP.start('localhost') do |smtp| smtp.send_message message, 'me@fromdomain.com', 'test@todomain.com' end * * * ## Sending Email with Attachments To send an email with mixed content, you need to set the `Content-type` to `multipart/mixed`. This allows you to add attachment content to the email. Attachments need to be converted to base64 format using the **`pack("m")`** function before transmission. ### Example The following example will send an email with an attachment located at `/tmp/test.txt`: ## Example ```ruby require 'net/smtp' filename = "/tmp/test.txt" filecontent = File.read(filename) encodedcontent = .pack("m") # Base64 encoding marker = "AUNIQUEMARKER" body =<<EOF This is a test email to send an attachement. EOF part1 =<<EOF From: Private Person To: A Test User 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 part3 =< e print "Exception occured: " + e end **Note:** You can specify multiple recipient addresses, but they need to be separated by commas.
← Ruby Socket ProgrammingRuby Cgi Sessions β†’