Latest web development tutorials

Ruby sending mail - SMTP

SMTP (Simple Mail Transfer Protocol) that is Simple Mail Transfer Protocol, which is a set of rules for transmitting messages from source to destination, which it has to control the way the letters of transit.

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

new method has two parameters:

  • The defaultserver nameis localhost
  • The defaultport numberis 25

start method has the following parameters:

  • server - SMTPserver IP, the default is localhost
  • port -the port number, the default is 25
  • domain -the mail sender domain, default ENV [ "HOSTNAME"]
  • account -the user name, the default is nil
  • password -the user's password, the default is nil
  • authtype -the type of authentication, the default iscram_md5

SMTP object instantiation method calls sendmail, the following parameters:

  • source -A string or array of each iteration or anything at any time in a return.
  • sender -a string that appears in the form of email field.
  • recipients -a string or array of strings representing the address of the recipient.

Examples

The following provides a simple Ruby script to send mail:

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 e-mail message, pay attention to the correct title format. An e-mail to be From, To, and Subject, between text and header information requires a blank line.

Use Net :: SMTP to connect to the SMTP server on the local machine to send messages using send_message process, process parameters for the sender of the message with the recipient mail.

If you are not running on the machine's SMTP server, you can use the Net :: SMTP communication with remote SMTP server. If you use webmail services (such as Hotmail or Yahoo Mail), your email provider will provide details of the sending mail server for you:

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

The above code will connect host mail.your-domain.com, port number 25 of the mail server, if you need to fill in the user name and password, the code is as follows:

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

The above examples use a user name and password to connect to the specified host mail.your-domain.com, port number 25 of the mail server.


Send HTML email with Ruby

Net :: SMTP also provides support for sending messages in HTML format.

When sending e-mail you can set the MIME version, document type, character set to send messages in HTML format.

Examples

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

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 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 mail with attachments

If you need to send a mixed content of e-mail, you need to set the Content-type is multipart / mixed. So you can add an attachment in the message content.

Accessory before the transfer requires the use pack ( "m") function converts its contents into base64 format.

Examples

The following example will send attachments as /tmp/test.txt e-mail:

require 'net/smtp'

filename = "/tmp/test.txt"
# 读取文件并编码为base64格式
filecontent = File.read(filename)
encodedcontent = [filecontent].pack("m")   # base64

marker = "AUNIQUEMARKER"

body =<<EOF
This is a test email to send an attachement.
EOF

# 定义主要的头部信息
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

# 定义消息动作
part2 =<<EOF
Content-Type: text/plain
Content-Transfer-Encoding:8bit

#{body}
--#{marker}
EOF

# 定义附件部分
part3 =<<EOF
Content-Type: multipart/mixed; name=\"#{filename}\"
Content-Transfer-Encoding:base64
Content-Disposition: attachment; filename="#{filename}"

#{encodedcontent}
--#{marker}--
EOF

mailtext = part1 + part2 + part3

# 发送邮件
begin 
  Net::SMTP.start('localhost') do |smtp|
     smtp.sendmail(mailtext, '[email protected]',
                          ['[email protected]'])
  end
rescue Exception => e  
  print "Exception occured: " + e  
end  

Note: You can specify the address of a plurality of transmission, but requires the use of commas.