main
1# frozen_string_literal: true
2
3require "mail"
4require "net/smtp"
5require "openssl"
6require "thor"
7
8require "minbox"
9
10module Minbox
11 module Cli
12 class Application < Thor
13 package_name "minbox"
14
15 method_option :from, type: :string, default: "me@example.org"
16 method_option :to, type: :array, default: ["them@example.org"]
17 method_option :subject, type: :string, default: "#{Time.now} This is a test message."
18 method_option :body, type: :string, default: "#{Time.now} This is a test message."
19 desc "send <HOST> <PORT>", "Send mail to SMTP server"
20 def send(host = "localhost", port = 25)
21 Net::SMTP.start(host, port) do |smtp|
22 smtp.debug_output = Minbox.logger
23 smtp.send_message(create_mail(options).to_s, options[:from], options[:to])
24 end
25 end
26
27 method_option :output, type: :array, default: ["stdout"]
28 method_option :tls, type: :boolean, default: false
29 desc "server <HOST> <PORT>", "SMTP server"
30 def server(host = "localhost", port = "25")
31 publisher = Publisher.from(options[:output])
32 server = Server.new(host: host, port: port, tls: options[:tls])
33 server.listen! do |mail|
34 publisher.publish(mail)
35 end
36 end
37
38 desc "version", "Display the current version"
39 def version
40 say Minbox::VERSION
41 end
42
43 private
44
45 def create_mail(options)
46 Mail.new do |x|
47 x.to = options[:to]
48 x.from = options[:from]
49 x.subject = options[:subject]
50 x.body = STDIN.tty? ? options[:body] : $stdin.read
51 end
52 end
53 end
54 end
55end