Ruby send email-SMTP
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 name
default islocalhost
port number
default is 25
The start
method has the following parameters:
server
-SMTP
server IP, default islocalhost
port
-port number, default is 25domain
-domain name of the sender of the mail. Default isENV["HOSTNAME"]
account
-user name, defaultnil
password
-user password. Default isnil
authtype
-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 theemail
form fieldrecipients
-A string or array of strings that represents the address of the recipient.
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