Use Ruby to write a simple tutorial for sending emails.

Source: Internet
Author: User

Use Ruby to write a simple tutorial for sending emails.

SMTP (Simple Mail Transfer Protocol) is a Simple Mail Transfer Protocol. It is a set of rules used to send Mail from the source address to the destination address, which controls the Transfer mode of Mail.

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

The new method has two parameters:

  • The default server name is localhost.
  • Port number is 25 by default.

The start method has the following parameters:

  • Server-SMTP server IP address. The default value is localhost.
  • Port-port number. The default value is 25.
  • Domain-mail sender domain name, default: ENV ["HOSTNAME"]
  • Account-user name. The default value is nil.
  • Password-user password. The default value is nil.
  • Authtype-verification type. The default value is cram_md5.

Sendmail is called for the SMTP Object Instantiation method. The parameters are as follows:

  • Source-Anything returned by a string or array or each iterator at any time.
  • Sender-a string that appears in the form field of the email.
  • Recipients-a string or string array that represents the recipient's address.

Instance

The following provides a simple Ruby script to send Emails:

require 'net/smtp' message = <<MESSAGE_ENDFrom: 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'end

In the above example, you have already set a basic email message. Pay attention to the correct title format. An email must be From, To, and Subject. A blank line is required between the text content and the header information.

Use Net: SMTP to connect to the SMTP server on the local machine and use the send_message method to send the mail. The method parameter is the sender mail and the receiver mail.

If you do not have an SMTP server running on the local machine, you can use Net: SMTP to communicate with the remote SMTP server. If you use the network mail service (such as Hotmail or Yahoo Mail), your email provider will provide you with the details of the sending server:
Net: SMTP. start ('mail .your-domain.com ')

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

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

The above instance uses the specified username and password to connect to the mail server with the host as the mail.your-domain.com and port number 25.
Use Ruby to send HTML emails

Net: SMTP also provides support for sending HTML-format mails.

When sending emails, you can set the MIME Version, document type, and character set to send emails in HTML format.
Instance

The following example is used to send an HTML-format Email:

require 'net/smtp' message = <<MESSAGE_ENDFrom: Private Person <me@fromdomain.com>To: A Test User <test@todomain.com>MIME-Version: 1.0Content-type: text/htmlSubject: SMTP e-mail test This is an e-mail message to be sent in HTML format <b>This is HTML message.</b>

Send email with attachment

To send an email with mixed Content, set Content-type to multipart/mixed. In this way, you can add the attachment content in the email.

Before transmitting an attachment, you must use the pack ("m") function to convert its content to base64 format.
Instance

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

Require 'net/smtp 'filename = "/tmp/test.txt" # Read and encode the File in base64 format filecontent = File. read (filename) encodedcontent = [filecontent]. pack ("m") # base64 marker = "AUNIQUEMARKER" body = <EOFThis is a test email to send an attachement. EOF # defines the main header information part1 = <EOFFrom: Private Person <me@fromdomain.net> To: A Test User <test@todmain.com> Subject: Sending AttachementMIME-Version: 1.0Content-Type: multipart/mixed; boundary =#{ marker} -- # {marker} EOF # defines the message action part2 = <EOFContent-Type: text/plainContent-Transfer-Encoding: 8bit # {body} -- # {marker} EOF # define the attachment part part3 = <EOFContent-Type: multipart/mixed; name = \ "# {filename} \" Content-Transfer-Encoding: base64Content-Disposition: attachment; filename = "# {filename}" # {encodedcontent} -- # {marker} -- EOF mailtext = part1 + part2 + part3 # Send email begin Net: SMTP. start ('localhost') do | smtp. sendmail (mailtext, 'Me @ fromdomain.net ', ['test @ todmain.com']) endrescue Exception => e print "Exception occured:" + e end

Note: You can specify multiple Sending addresses, but they must be separated by commas.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.