Tutorial on using Net: SMTP to send emails in Ruby
This article describes how to use Net: SMTP class to send emails in Ruby, including the usage of the methods in the class. For more information, see
The protocol between the Simple mail Transfer Protocol (SMTP) for sending emails and the email server that routes the email.
Ruby provides the Net: SMTP Simple Mail Transfer Protocol (SMTP) Client connection, and provides two new methods: new and start.
New has two parameters:
The default server name is localhost.
Port number is 25 by default.
The start method has the following parameters:
Server-ip smtp server name, default: localhost
Port-port number. The default value is 25.
Domain-the email sender's domain name. The default value is ENV ["HOSTNAME"].
Account-user name. The default value is nil.
Password-user password. The default value is nil.
Authtype-Authorization type. The default value is cram_md5.
The SMTP object has an instance method that calls sendmail, which is usually used to mail messages. It has three parameters:
Source-string or array, or any same iteration returns a string.
Sender-a string that appears in the email field.
Recipients-string or array representing the recipient's address
Example:
Below is a simple method to send an email using the Ruby script. Try it:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
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 |
A basic email message has been placed here, using a file. Note that the title is correctly formatted here. An email requires the sender, recipient, and topic header, separated by a blank line from the subject of the email.
When a message is sent, Use Net: SMTP to connect to the SMTP server on the local machine, and then use the send_message method to take the address and destination address as the parameter (even if the Address Email is within its own range, these are not always used to route messages ).
If you have not run an SMTP server on your machine, you can use the Net: SMTP remote SMTP server for communication. Unless you use a webmail service (such as Hotmail or Yahoo Mail), the email provider will provide details about the external mail server. Net: SMTP can be mentioned as follows:
?
1 |
Net: SMTP. start ('mail .your-domain.com ') |
This line of code connects to SMTP server mail.your-domain.com port 25 ., You do not need to use any user name or password. However, you can specify the port number or other parameters if necessary. For example:
?
1 2 3 4 |
Net: SMTP. start ('mail .your-domain.com ', 25, 'Localhost ', 'Username', 'Password': plain) |
This example connects the mail.your-domain.com to the SMTP server using the username and password in plain text format. It is the host name of the localhost client.
Use Ruby to send HTML emails:
To send text messages, all Ruby content is considered as simple text. Even if the text message contains HTML tags, simple text and HTML tags are displayed without formatting the HTML syntax. However, Ruby's Net: SMTP provides the option to actually send HTML messages to HTML emails.
When sending an email message, you can specify a MIME Version, content type, and character set to send an HTML email.
For example:
The following example sends HTML content as an email. Try it:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Require 'net/smtp' Message = <MESSAGE_END From: Private Person <me@fromdomain.com> To: A Test User <test@todomain.com> MIME-type: 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. MESSAGE_END Net: SMTP. start ('localhost') do | smtp | Smtp. send_message message, 'Me @ fromdomain.com ', 'Test @ todomain.com' End |
Send as an email attachment:
To send an email with mixed Content, you must set the Content-type header to multipart/mixed. Then, you can specify the boundaries range for the text and attachments.
The two hyphens are followed by a unique number and cannot appear at the boundary of the message section of the email. The last boundary indicates that the end of the last section of the email must also contain two hyphens.
The append object uses the pack ("m") function to encode the object before base64 encoding.
Example:
In the following example, the file/tmp/test.txt is sent as an attachment. Try it:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
Require 'net/smtp' Filename = "/tmp/test.txt" # Read a file and encode it into 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 headers. Part1 = <EOF From: Private Person <me@fromdomain.net> To: A Test User <test@todmain.com> Subject: Sending Attachement MIME-type: 1.0 Content-Type: multipart/mixed; boundary =#{ marker} -- # {Marker} EOF # Define the message action Part2 = <EOF Content-Type: text/plain Content-Transfer-Encoding: 8bit # {Body} -- # {Marker} EOF # Define 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 # Let's put our code in safe area 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: multiple destination internal Arrays can be specified, but they must be separated by commas.