Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Ruby Sending Email - SMTP

Ruby Sending Email - SMTP

SMTP (Simple Mail Transfer Protocol) is a set of rules for transferring mail from a source address to a destination address, which controls how messages are relayed.

Ruby provides Net::SMTP for sending email, along with two methods, new and start:

new The method has two parameters:

start The method has the following parameters:

The SMTP instance method calls sendmail with the following arguments:

Example

The following is a simple Ruby script to send an email:

Example

require

'

net/smtp

'

message

= <<

MESSAGE_END

From

:

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 set up a basic email message. Pay attention to the correct header format. An email needs From, To, and Subject, and there must be a blank line 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 email, with the sender's and recipient's addresses as arguments.

If you do not have an SMTP server running on your local machine, you can use Net::SMTP to communicate with a remote SMTP server. If you use a webmail service (such as Hotmail or Yahoo Mail), your email provider will give you the details of the outgoing mail server:

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 you need to provide a username and password, the code is as follows:

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, content type, and character set to send an HTML-formatted email.

Example

The following example sends an HTML-formatted email:

Example

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 an e-mail message to be sent in HTML format This is HTML message.<

/

b

<

h1

This

is

headline

.<

/

h1> MESSAGE_END Net::SMTP.start('localhost') do |smtp| smtp.send_message message, 'me@fromdomain.com', 'test@todomain.com' end


Sending Email with Attachments

If you need to send an email with mixed content, set the Content-type to multipart/mixed. This allows you to add attachments to the email.

Before transmission, the attachment needs to use the pack("m") function to convert its content to base64 format.

Example

The following example sends an email with /tmp/test.txt as an attachment:

Example

require

'

net/smtp

'

filename

=

"

/tmp/test.txt

"

Read the file and encode it in base64 format

filecontent

=

File

.

read

(

filename

)

encodedcontent

=

[

filecontent

]

.

pack

(

"

m

"

)

base64

marker

=

"

AUNIQUEMARKER

"

body

=<<

EOF

This

is

a

test

email

to

send

an

attachement

.

EOF

Define the main header information

part1

=<<

EOF

From

:

Private

Person

<

me

@fromdomain

.

net

To

:

A

Test

User

<

test

@todmain

.

com

Subject

:

Sending

Attachement

MIME

Version

:

1.0

Content

Type

:

multipart

/

mixed; boundary=#{marker} --#{marker} EOF # define message action part2 =<

/

plain

Content

Transfer

Encoding

:

8

bit

{body}

--

{marker}

EOF

Define the attachment part

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 email 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 recipient addresses, but they need to be separated by commas.