<commit_before>#!/usr/bin/env ruby

require 'net/smtp'
require 'optparse'

### REMEMBER TO EDIT PASSWORD ###

### yet to do ###
# command flags,
# fixing of timestamp,
# custom interval,
# set env in the header (see the CLI book for this)
# get rid of hardcoded password
# allow for subject, to, from, adjustment/input
# error handling


# what about being able to incorporate an automatic, timed
# command run like
# "every hour, email me the results of 'find *.rb ~/oak/dev/my_lib | less'"
# that would be kinda cool and useful

# control
options = {}
option_parser = OptionParser.new do |opts|
  executable_name = File.basename($PROGRAM_NAME)
  opts.banner = "Send the results of commands, notes, memos, etc., over email.\n
  Usage: #{executable_name} [options] text
  
  "

  # point to a text file to use as the message
  opts.on("-f FILE","--file",
         'Send the contents of a file instead of STDIN') do |file|
    options[:file] = file
  end
  
  # login username
  opts.on("-u USER", "--username",
         'Account username (everything before the @)') do |user|
    options[:user] = user
  end
  
  # login password
  opts.on("-p PASSWORD", "--password",
         'Account password') do |password|
    options[:password] = password
  end

  # message subject
  opts.on("-s SUBJECT", "--subject",
         'Message subject') do |subject|
    options[:subject] = subject
  end
end

option_parser.parse!

if options[:file]
  message_body = File.open(options[:file], 'r') { |l| l.read }
else
  message_body = ARGV.shift
end


# perhaps a bit clever...yields A-Z 0-9 char.
msg_gen = lambda { rand(2) > 0 ? ("A".."Z").to_a[rand(26)] : rand(10).to_s }
# a random string generated by calling the lambda 30 times
# the collect_concat method is...pretty esoteric.
# I've never seen it in anyone else's code. But this is Ruby, right?
# could also be substituted for #flat_map, which is functionally identical,
# according to the docs
my_message_id = (1..30).collect_concat { msg_gen.call }.join 

now = Time.now



msgstr = <<END_OF_MESSAGE
From: <NAME> <<EMAIL>>
To: <NAME> <<EMAIL>>
Subject: #{options[:subject]}
Date: #{now} 
Message-Id: <<EMAIL>>

#{message_body}
END_OF_MESSAGE

# docs: http://rubydoc.org/stdlib/net/1.9.3/Net/SMTP#enable_tls-instance_method
def send_mail(m, options)
  con = Net::SMTP.new('smtp.googlemail.com', 465)
  con.enable_tls
  con.start('gmail.com', options[:user], options[:password], :login) do |smtp|
    smtp.send_message m, '<EMAIL>', '<EMAIL>'
  end
end

send_mail(msgstr, options)<commit_msg>authentication expection handling experiment<commit_after>#!/usr/bin/env ruby

require 'net/smtp'
require 'optparse'

### REMEMBER TO EDIT PASSWORD ###

### yet to do ###
# command flags,
# fixing of timestamp,
# custom interval,
# set env in the header (see the CLI book for this)
# get rid of hardcoded password
# allow for subject, to, from, adjustment/input
# error handling


# what about being able to incorporate an automatic, timed
# command run like
# "every hour, email me the results of 'find *.rb ~/oak/dev/my_lib | less'"
# that would be kinda cool and useful

# control
options = {}
option_parser = OptionParser.new do |opts|
  executable_name = File.basename($PROGRAM_NAME)
  opts.banner = "Send the results of commands, notes, memos, etc., over email.\n
  Usage: #{executable_name} [options] text
  
  "

  # point to a text file to use as the message
  opts.on("-f FILE","--file",
         'Send the contents of a file instead of STDIN') do |file|
    options[:file] = file
  end
  
  # login username
  opts.on("-u USER", "--username",
         'Account username (everything before the @)') do |user|
    options[:user] = user
  end
  
  # login password
  opts.on("-p PASSWORD", "--password",
         'Account password') do |password|
    options[:password] = password
  end

  # message subject
  opts.on("-s SUBJECT", "--subject",
         'Message subject') do |subject|
    options[:subject] = subject
  end
end

option_parser.parse!

if options[:file]
  message_body = File.open(options[:file], 'r') { |l| l.read }
else
  message_body = ARGV[0]
end


# perhaps a bit clever...yields A-Z 0-9 char.
# a random string generated by calling the lambda 30 times
# the collect_concat method is...pretty esoteric.
# I've never seen it in anyone else's code. But this is Ruby, right?
# could also be substituted for #flat_map, which is functionally identical,
# according to the docs
msg_gen = lambda { rand(2) > 0 ? ("A".."Z").to_a[rand(26)] : rand(10).to_s }
my_message_id = (1..30).collect_concat { msg_gen.call }.join 

time_now = Time.now

msgstr = <<END_OF_MESSAGE
From: <NAME> <<EMAIL>>
To: <NAME> <<EMAIL>>
Subject: #{options[:subject]}
Date: #{time_now} 
Message-Id: <<EMAIL>>

#{message_body}
END_OF_MESSAGE

# docs: http://rubydoc.org/stdlib/net/1.9.3/Net/SMTP#enable_tls-instance_method
def send_mail(m, options)
  con = Net::SMTP.new('smtp.googlemail.com', 465)
  con.enable_tls
  con.start('gmail.com', options[:user], options[:password], :login) do |smtp|
    begin
    smtp.send_message m, '<EMAIL>', '<EMAIL>'
  
    rescue Net::SMTPAuthenticationError
      puts "Authentication error. Please make sure you entered a valid username and password.  Run the program with no options for help.
      "
    end
      
  end
end

send_mail(msgstr, options)
{'fraction_non_alphanumeric': 0.1354147075418469, 'fraction_numerical': 0.007334963325183374, 'mean_word_length': 3.7020335985853228, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2899767', 'n_tokens_mistral': 1817, 'n_tokens_neox': 1749, 'n_words': 577}