The SMTP (Simple Mail Transfer Protocol) is simply a mail Transfer protocol, a set of rules for sending messages from the source address to the destination to control how the letters are transferred.
Ruby provides net::smtp to send mail, and provides two methods for 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 to localhost
- Port-port number, defaults to 25
- Domain-mail sender name, defaults to env["HOSTNAME"]
- Account-Username, default to nil
- Password-user password, default is nil
- AuthType-authentication type, defaults to CRAM_MD5
The SMTP object instantiation method called SendMail, and the parameters are as follows:
- Source-anything that a string or array or each iterator returns at any one time.
- Sender-A string that appears in the form field of an email.
- Recipients-a string or array of strings representing the address of the recipient.
Instance
The following provides a simple Ruby script to send a message:
Require ' net/smtp ' message
= <<message_end
from:private person <me@fromdomain.com>
to:a Test User <test@todomain.com>
subject:smtp e-mail test is
a test e-mail message.
Message_end
net::smtp.start (' localhost ') do |smtp|
Smtp.send_message message, ' me@fromdomain.com ', '
test@todomain.com '
In the example above, you have set up a basic email message, paying attention to the correct title format. To From,to and subject an e-mail message, you need a blank line between the text content and the header information.
Using NET::SMTP to connect to an SMTP server on the local machine, use the Send_message method to send the message, which is the sender's message and the recipient's message.
If you are not running an SMTP server on this computer, you can use NET::SMTP to communicate with the remote SMTP server. If you use a webmail service such as Hotmail or Yahoo Mail, your e-mail provider will provide you with detailed information about the outgoing mail server:
Net::smtp.start (' mail.your-domain.com ')
The code above will connect the host to mail.your-domain.com, the mail server with port number 25, and if you need to fill in the username password, the code is as follows:
Net::smtp.start (' mail.your-domain.com ',,
' localhost ', '
username ', ' password ':p lain)
The above example uses the specified username password to connect to a mail server with a port number of 25 for the mail.your-domain.com host.
send HTML messages using Ruby
NET::SMTP also provides support for sending messages in HTML format.
When sending an e-mail message, you can set up MIME versions, document types, and character sets to send messages in HTML format.
instance
The following instance is used to send messages in HTML format:
Require ' net/smtp ' message
= <<message_end
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 a e-mail message to was sent in HTML format
<b>this was HTML message.</b>
Send a message with an attachment
If you need to send a mixed-content e-mail message, you need to set Content-type to multipart/mixed. This allows you to add the attachment content to the message.
Attachments need to use the pack ("M") function to convert their contents to Base64 format before transmission.
instance
The following instance sends a message with an attachment of/tmp/test.txt:
Require ' net/smtp ' filename = "/tmp/test.txt" # Read file and encode as base64 format filecontent = file.read (filename) encodedcontent = [fi
Lecontent].pack ("M") # base64 marker = "Auniquemarker" Body =<<eof This is a test email to send a attachement. EOF # defines the main header information part1 =<<eof from:private person <me@fromdomain.net> to:a Test User <test@todmain.com&
Gt subject:sending attachement mime-version:1.0 content-type:multipart/mixed;
Boundary=#{marker}--#{marker} EOF # defines message actions Part2 =<<eof content-type:text/plain content-transfer-encoding:8bit #{body}--#{marker} EOF # defines the attachment section 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 mail begin NET::SMTP.
Start (' localhost ') do |smtp| Smtp.sendmail (Mailtext, ' me@fromdomain.net ', [' test@todmain.com ']) end rescue Exception => e print "Exception occured:" + E End
Note: You can specify multiple addresses to send, but you need to separate them with commas.