Sending emails using SMTP in Ruby
By: James Edward Gray II in Ruby Tutorials on 2009-03-03
You must open a connection to an SMTP server before sending messages. The first argument is the address of your SMTP server, and the second argument is the port number. Using SMTP.start with a block is the simplest way to do this. This way, the SMTP connection is closed automatically after the block is executed.
require 'net/smtp' Net::SMTP.start('your.smtp.server', 25) do |smtp| # Use the SMTP object smtp only in this block. end
Replace 'your.smtp.server" with your SMTP server. Normally your system manager or internet provider supplies a server for you.
Then you can send messages.
msgstr = <<END_OF_MESSAGE From: Your Name <[email protected]> To: Destination Address <[email protected]> Subject: test message Date: Sat, 23 Jun 2001 16:26:43 +0900 Message-Id: <[email protected]> This is a test message. END_OF_MESSAGE require 'net/smtp' Net::SMTP.start('your.smtp.server', 25) do |smtp| smtp.send_message msgstr, '[email protected]', '[email protected]' end
Closing the Session
You MUST close the SMTP session after sending messages, by calling the finish method:
# using SMTP#finish smtp = Net::SMTP.start('your.smtp.server', 25) smtp.send_message msgstr, 'from@address', 'to@address' smtp.finish
You can also use the block form of SMTP.start/SMTP#start. This closes the SMTP session automatically:
# using block form of SMTP.start Net::SMTP.start('your.smtp.server', 25) do |smtp| smtp.send_message msgstr, 'from@address', 'to@address' end
I strongly recommend this scheme. This form is simpler and more robust.
HELO domain
In almost all situations, you must provide a third argument to SMTP.start/SMTP#start. This is the domain name which you are on (the host to send mail from). It is called the "HELO domain". The SMTP server will judge whether it should send or reject the SMTP session by inspecting the HELO domain.
Net::SMTP.start('your.smtp.server', 25, 'mail.from.domain') { |smtp| ... }
SMTP Authentication
The Net::SMTP class supports three authentication schemes; PLAIN, LOGIN and CRAM MD5. (SMTP Authentication: [RFC2554]) To use SMTP authentication, pass extra arguments to SMTP.start/SMTP#start.
# PLAIN Net::SMTP.start('your.smtp.server', 25, 'mail.from.domain', 'Your Account', 'Your Password', :plain) # LOGIN Net::SMTP.start('your.smtp.server', 25, 'mail.from.domain', 'Your Account', 'Your Password', :login) # CRAM MD5 Net::SMTP.start('your.smtp.server', 25, 'mail.from.domain', 'Your Account', 'Your Password', :cram_md5)
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Comments