|
| 1 | +require "email" |
| 2 | +require "option_parser" |
| 3 | + |
| 4 | +module SendEmail |
| 5 | + VERSION = "0.1.0" |
| 6 | + |
| 7 | + class App |
| 8 | + getter :hostname, :port |
| 9 | + |
| 10 | + @hostname : String |
| 11 | + @port : Int32 |
| 12 | + @debug : Bool |
| 13 | + |
| 14 | + @subject : String |
| 15 | + @text : String |
| 16 | + @html : String |
| 17 | + |
| 18 | + @from : String |
| 19 | + @recipients : Array(String) |
| 20 | + |
| 21 | + def run() |
| 22 | + # Create email message |
| 23 | + email = EMail::Message.new |
| 24 | + email.from "[email protected]", "Notify" |
| 25 | + |
| 26 | + email.subject "Test email - hello world!" |
| 27 | + email.message <<-EOM |
| 28 | + Testovací email. |
| 29 | +
|
| 30 | + -- |
| 31 | + Martin M. |
| 32 | + EOM |
| 33 | + |
| 34 | + # Email HTML email body |
| 35 | + email.message_html <<-EOM |
| 36 | +
|
| 37 | + EOM |
| 38 | + |
| 39 | + # Set SMTP client configuration |
| 40 | + # config = EMail::Client::Config.new("smtp.datalite.cz", 587) |
| 41 | + # config.use_tls(EMail::Client::TLSMode::STARTTLS) |
| 42 | + # config.tls_context.verify_mode = OpenSSL::SSL::VerifyMode::NONE |
| 43 | + config = EMail::Client::Config.new("oracleas.datalite.cz", 2525) |
| 44 | + |
| 45 | + # Create SMTP client object |
| 46 | + client = EMail::Client.new(config) |
| 47 | + |
| 48 | + client.start do |
| 49 | + # In this block, default receiver is client |
| 50 | + send(email) |
| 51 | + end |
| 52 | + end |
| 53 | + |
| 54 | + def initialize() |
| 55 | + @hostname = "" |
| 56 | + @port = -1 |
| 57 | + @debug = false |
| 58 | + |
| 59 | + @subject = "" |
| 60 | + @text = "" |
| 61 | + @html = "" |
| 62 | + |
| 63 | + @from = "" |
| 64 | + @recipients = [] of String |
| 65 | + |
| 66 | + _opts = parse_opts() |
| 67 | + end |
| 68 | + |
| 69 | + def parse_opts |
| 70 | + OptionParser.parse do |parser| |
| 71 | + parser.banner = "Usage: send-email [arguments]" |
| 72 | + |
| 73 | + parser.on("-h HOSTNAME", "--hostname=HOSTNAME", "Hostname (SMTP)") { |_hostname| @hostname = _hostname } |
| 74 | + parser.on("-p PORT", "--port=PORT", "Port") { |_port| @port = _port.to_i } |
| 75 | + parser.on("-s SUBJECT", "--subject=SUBJECT", "Subject") { |_subject| @subject = _subject } |
| 76 | + parser.on("-f FROM", "--from=FROM", "From:") { |_from| @from = _from } |
| 77 | + parser.on("-r RECIPIENTS", "--recipients=RECIPIENTS", "RcptTo: (comma separated list)") do |_recipients| |
| 78 | + @recipients = _recipients.split(',') |
| 79 | + end |
| 80 | + parser.on("-t TXT", "--txt-body-file=TXT", "Text body file") { |_text| @text = _text } |
| 81 | + parser.on("-m HTML", "--html-body-file=HTML", "Html body file") { |_html| @html = _html } |
| 82 | + |
| 83 | + parser.on("-d", "--debug", "Debug?") { @debug = true } |
| 84 | + parser.on("-v", "--version", "App version") do |
| 85 | + puts "App name: send-email" |
| 86 | + puts "App version: #{VERSION}" |
| 87 | + exit |
| 88 | + end |
| 89 | + parser.on("-h", "--help", "Show this help") do |
| 90 | + puts parser |
| 91 | + exit |
| 92 | + end |
| 93 | + parser.invalid_option do |flag| |
| 94 | + STDERR.puts "ERROR: #{flag} is not a valid option." |
| 95 | + STDERR.puts parser |
| 96 | + exit(1) |
| 97 | + end |
| 98 | + |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | + end |
| 103 | + |
| 104 | + app = App.new |
| 105 | + app.run |
| 106 | + |
| 107 | +end |
0 commit comments