Ruby send email-SMTP


Release date:2023-11-02 Update date:2023-11-02 Editor:admin View counts:226

Label:

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 is localhost

  • port number default is 25

The start method has the following parameters:

  • server - SMTP server IP, default is localhost

  • port -port number, default is 25

  • domain -domain name of the sender of the mail. Default is ENV["HOSTNAME"]

  • account -user name, default nil

  • password -user password. Default is nil

  • authtype -validation type, default is cram_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 the email form field

  • recipients -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

Powered by TorCMS (https://github.com/bukun/TorCMS).