..
Like any good programming language Ruby also allows you to send e-mail messages. To do this, do not type so much code, just go to the interpreter instead of the language a complete file of all the parameters needed to send.
Underlying everything we have a specific library called Net:: SMTP which provides the user with all the functionality necessary to send email via SMTP (Simple Mail Transfer Protocol), thanks to the library Net:: SMTP will be possible to create a object able to open a TCP (Transmission Control Protocol) to connect to the server for sending. The object in question is used by a specific method called start.
It 'good to clear that Net:: SMTP can not send mail via the Internet, that does not perform a function similar to that which may be the mail () function of a scripting language like PHP.
The task resembles more that of a mail client than to a Webmail to correspondence online.
That said, let's see some code, but here mostremo a simple program that can send email based on user-defined parameters:
# Send an email with Ruby
# Calls the library to connect to the server
require 'net / smtp'
# Specify the parameters of both the sender and recipient
nome_mittente = 'My name'
email_mittente = 'info@miamail.it'
nome_destinatario = 'your name'
email_destinatario = 'info@suamail.it'
# Specify the authentication parameters to the server
host_smtp = 'smtp.provider.it'
porta_smtp = 25
dominio_smtp = 'provider.it'
utente_smtp = 'user'
password_smtp = 'password'
# Subject and body of the email
subject = 'Send an email with Ruby'
body = "Hello World!. \ n"
# Pass parameters to a variable
message = <<END_OF_MESSAGE
From: # {nome_mittente} <# {} email_mittente>
To: # {nome_destinatario} <# {} email_destinatario>
Subject: # {subject}
# {Body}
END_OF_MESSAGE
# Send the email via the start method
Net:: SMTP.start (host_smtp,
porta_smtp,
dominio_smtp,
utente_smtp,
password_smtp,: plain) do | smtp |
smtp.send_message message utente_smtp, email_destinatario
# Close the block of instructions
end
To summarize what we saw in the code, we can see that the first thing to do is to call on the library to connect to the mail server.
Secondly, it is necessary to specify the data about the sender and the recipient name and email address of ships and the same data regarding the user who will receive the message.
The connection to the SMTP server can not be made without disclosure of the necessary authentication parameters: the hostname of the server, the port on which the server is listen for any messages, the domain name on the mail server , the credentials of the sender to use the service (username and password).
It then proceeds with an indication of the subject and message body, the latter data is passed as values for a variable that also contains the pre-specified headers.
Finally, the start method is called the library Net:: SMTP.start through authentication parameters to the server and connects via the do statement (literally "do") allows the sending of the message.
Notice how the symbol: plain do not specify the type of message (not the same then the plain text format of the mail), but the authentication method (or schema).
Properly speaking there are 3 methods of authentication to an SMTP server and Ruby supports them all, they are PLAIN, LOGIN and CRAM MD5, but parameters rigurdano topic relating to the management of the mail server and not the same Ruby, the curious can still landed on speech by one of the many resources found on the Internet in this regard.
| |
Ruby and Ruby on Rails (Course)
Create software and Web applications with Ruby and RoR. From 39 €. |