Module: Smitty

Defined in:
lib/smitty/croak.rb,
lib/smitty/usage.rb,
lib/smitty/version.rb,
lib/smitty/smtp_connect.rb,
lib/smitty/variable_parser.rb

Constant Summary collapse

USAGE =
"Smitty \#{Smitty::VERSION}\n\nUsage:\n  \#{$0} [options] <from_address> <to_file> <subject> <template>\n  \#{$0} -h | --help\n  \#{$0} -v | --version\n\nRequired Arguments:\n  from_address:                    Address to send mail from.\n  to_file:                         Newline separated file containing recipient addresses.\n  subject:                         Mail subject, can have handlebar replacements.\n  template:                        Handlebars like HTML template.\n\nOptions:\n  -h --help                        Show this usage.\n  -v --version                     Show version.\n  --vars key=value,key=value       Variables for template and subject line, should be a comma separated\n                                   list of key value pairs. Key should be the variable name in your\n                                   template, and value may be a constant or a newline separated file.\n                                   Value file and to_file must have the same amount of lines.\n  -a files                         Attach comma separated list of files.\n  --server SERVER                  SMTP Server, default is localhost.\n  --port PORT                      SMTP Port, default is 25.\n  --ssl                            Use SSL, default is false.\n  --username USER                  SMTP user.\n  --password PASSWORD              SMTP password.\n  --cc address                     Add a cc address.\n  --bcc address                    Add a bcc address.\n  --messageid FQDN                 Set a FQDN to use when generating the message-id for each message.\n  --dry-run                        Output messages and don't send.\n\n"
VERSION =
'0.0.5'

Class Method Summary collapse

Class Method Details

.croak(*messages) ⇒ Object

Prints variable amount of messages and exits



3
4
5
6
# File 'lib/smitty/croak.rb', line 3

def self.croak(*messages)
  messages.each { |message| puts "Error: #{message}"}
  exit(1)
end

.smtp_connect(server, port, ssl, username, password) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/smitty/smtp_connect.rb', line 4

def self.smtp_connect(server, port, ssl, username, password)
  begin
    smtp_server = server.nil? ? 'localhost' : server
    smtp_port = port.nil? ? 25 : port.to_i
    smtp_conn = Net::SMTP.new(smtp_server, smtp_port)
    smtp_conn.enable_tls() if ssl
    if username
      Smitty.croak('No password provided') if password.nil?
      smtp_conn.start(smtp_server, username, password, :plain)
    else
      smtp_conn.start()
    end
    smtp_conn
  rescue Exception => e
    Smitty.croak("Could not connect to SMTP server #{smtp_server}:#{smtp_port}", e.message)
  end
end

.variable_parser(variable_string, mail_length) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/smitty/variable_parser.rb', line 2

def self.variable_parser(variable_string, mail_length)
  pairs = variable_string.split('=')
  # content is a file
  begin
    with_content = File.readlines(pairs[1]).map(&:chomp)
    Smitty.croak("#{pairs[0]} != recipient length") if with_content.length != mail_length
  rescue Errno::ENOENT
    with_content = pairs[1]
  rescue
    croak("Could not parse key value pair #{variable_string}")
  end
  {replace_string: pairs[0], with: with_content }
end