Use SMTP protocol to send emails written in ruby

Source: Internet
Author: User
Tags email account
Require 'net/SMTP'
Require 'ftool'
Require 'getoptlong'
$ Tomail = ""
$ Subject = ""
$ Content = ""
$ Filename = ""
$ I = 0
Def help ()
Puts "Usage:/nsendemail [Option]"
Puts "/t -- to-T destination email"
Puts "/t -- Subject-s Email Subject"
Puts "/t -- content-C email content"
Puts "/t -- file-F email content in file, can't use with-c"
Exit 1
End
Opts = getoptlong. New (
["-- To", "-T", getoptlong: required_argument],
["-- Content", "-c", getoptlong: required_argument],
["-- Subject", "-s", getoptlong: required_argument],
["-- File", "-F", getoptlong: required_argument],
["-- Help", "-h", getoptlong: no_argument]);
Opts. Each do | optopt, optarg |
# Puts "Options: # {optopt}, Arg # {optarg }"
If ("# {optopt}" = "-T" | "# {optopt}" = "-- ")
$ Tomail = "# {optarg }"
$ I + = 1
End
If ("# {optopt}" = "-F" | "# {optopt}" = "-- file ")
$ Filename = "# {optarg }"
$ I + = 1
End
If ("# {optopt}" = "-c" | "# {optopt}" = "-- content ")
$ Content = "# {optarg }"
$ I + = 1
End
If ("# {optopt}" = "-s" | "# {optopt}" = "-- subject ")
$ Subject = "# {optarg }"
$ I + = 1
End
If ("# {optopt}" = "-h" | "# {optopt}" = "-- Help ")
Help ()
End
End
If ($ I! = 3) then
Help ()
End
If ($ filename! = "") Then
F = file. New ("# {$ filename}", "R ")
$ Content = f. Read
F. Close
End
MSG = "Subject: # {$ subject}/n # {$ content}/N"
Net: SMTP. Start ('smtp .sina.com ', 25, 'smtp .sina.com', 'Your Sina email account', 'sina account password',: Login) Do | SMTP |
SMTP. send_message (MSG, 'bornwin @ Sina.com ', ["# {$ tomail}"])
End
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.