SMTP (Simple Mail Transfer Protocol), or simple Mail transfer Protocol, is aset of rules for sending messages from source to destination, which controls the way letters are transferred.
Ruby provides
Net::SMTP
to send emails and provides two methods,
new
and
start
:
new
method has two parameters:
server namedefault islocalhostport numberdefault is 25
The
start
method has the following parameters:
server-SMTPserver IP, default islocalhostport-port number, default is 25domain-domain name of the sender of the mail. Default isENV["HOSTNAME"]account-user name, defaultnilpassword-user password. Default isnilauthtype-validation type, default iscram_md5
The
SMTP
object instantiation method calls the
sendmail
parameters are as follows:
source-anything returned by a string or array or by each iterator at any time.sender-A string that appears in theemailform fieldrecipients-A string or array of strings that represents the address of the recipient.
6.40.1. Example #
The following provides a simple Ruby script to send messages:Example #
require'net/smtp'message=
<<MESSAGE_ENDFrom:PrivatePerson<me@fromdomain.com>To:ATestUser
<test@todomain.com>Subject:SMTPe-mailtestThisisateste-mailmessage.MESSAGE_ENDNet::SMTP.
start('localhost')do\|smtp\|smtp.send_messagemessage,'me@fromdomain.com','test@todomain.com'end