Tutorial on using Net: SMTP to send emails in Ruby, rubysmtp
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:
require 'net/smtp'message = <<MESSAGE_ENDFrom: Private Person <me@fromdomain.com>To: A Test User <test@todomain.com>Subject: SMTP e-mail testThis is a test e-mail message.MESSAGE_ENDNet::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:
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:
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:
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 testThis is an e-mail message to be sent in HTML format<b>This is HTML message.</b>
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:
require 'net/smtp'filename = "/tmp/test.txt"# Read a file and encode it into base64 formatfilecontent = File.read(filename)encodedcontent = [filecontent].pack("m") # base64marker = "AUNIQUEMARKER"body =<<EOFThis is a test email to send an attachement.EOF# Define the main headers.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# Define the message actionpart2 =<<EOFContent-Type: text/plainContent-Transfer-Encoding:8bit#{body}--#{marker}EOF# Define the attachment sectionpart3 =<<EOFContent-Type: multipart/mixed; name=\"#{filename}\"Content-Transfer-Encoding:base64Content-Disposition: attachment; filename="#{filename}"#{encodedcontent}--#{marker}--EOFmailtext = part1 + part2 + part3# Let's put our code in safe areabegin Net::SMTP.start('localhost') do |smtp| smtp.sendmail(mailtext, 'me@fromdomain.net', ['test@todmain.com']) endrescue Exception => e print "Exception occured: " + e end
Note: multiple destination internal Arrays can be specified, but they must be separated by commas.