command_line_email 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.swp
2
+ *.gem
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in command_line_email.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 James Lavin
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,82 @@
1
+ # CommandLineEmail
2
+
3
+ Send email from the Linux or Mac command line. Attach specific file(s) or all files in a directory. Specify email body as CLI string or load from a file.
4
+
5
+ ## WARNING - NOT YET FULLY IMPLEMENTED
6
+
7
+ This program still needs some work before I finish gemifying it. I have not yet created the bin/ directory file and currently run this by calling lib/command_line_email.rb directly.
8
+
9
+ ## Installation
10
+
11
+ Install command_line_email as:
12
+
13
+ $ gem install command_line_email
14
+
15
+ Or add this line to your application's Gemfile:
16
+
17
+ gem 'command_line_email'
18
+
19
+ and then install with:
20
+
21
+ $ bundle
22
+
23
+ ## Usefulness
24
+
25
+ This gem is useful for anyone who wishes to send email from a server using only the command line. It's also useful for anyone who wishes to send email from a computer on which they don't wish to install an email client. (I use multiple machines and keep all my email on one computer but wish to send emails from other computers.)
26
+
27
+ ## Usage
28
+
29
+ Once set up (see "Configuration File" below), you can send an email using various options (see "Command Line Options" below). Here's one example:
30
+
31
+ ruby lib/command_line_email.rb -t "me" -t "other_me" -s "Test subject line" -b "My body text"
32
+
33
+ ### Configuration File
34
+
35
+ The first time you run command_line_email, it will create a YAML file at ~/.command_line_email.yml. You edit this file to set your email configuration and, optionally, create mailing lists. A configuration file looks something like this:
36
+
37
+ ---
38
+ :connection:
39
+ :address: mail.domain.com
40
+ :port: 25
41
+ :domain: domain.com
42
+ :user_name: name@domain.com
43
+ :password: a2dfsae3355
44
+ :authentication: plain
45
+ :enable_starttls_auto: true
46
+
47
+ :mailing_lists:
48
+ :me: 'me@domain.com'
49
+ :parents: ['mom@domain.com','dad@domain.com']
50
+ :friends: ['joe@anotherdomain.com','sally@yetanotherdomain.com]
51
+
52
+ :defaults:
53
+ :from: 'sender@myemailaddress.com'
54
+ :to: 'my.wife@heremailaddress.com'
55
+ :cc: 'me@myotheremailaddress.com'
56
+ :subject: 'From Johnny Smith'
57
+
58
+ ### Command Line Options
59
+
60
+ The following command line options are available:
61
+
62
+ -t (--to) takes an email address ("me@domain.com"), array of email addresses (['mom@domain.com','dad@domain.com']), or a string specified in mailing_lists (e.g., "me" or "parents" or "friends"), each of which points to an array of email addresses.
63
+
64
+ -f (--from) takes a string as the sender's email address
65
+
66
+ -s (--subject) takes a string as the email subject
67
+
68
+ -b (--body) takes a string as a text body. To provide more than one paragraph, provide a separate -b/--body for each paragraph in the order you wish the paragraphs to appear.
69
+
70
+ -c (--cc) takes the same values as -t/--to and sets the email's "carbon copy" recipients
71
+
72
+ -f (--file) takes a string representing the path to a file you wish to attach to the email. This flag can be used multiple times, each with a different file.
73
+
74
+ -d (--directory) takes a string with a directory path. If provided with the -f/--file option, it is presumed that all files are in the directory specified by the -d/--directory option. If no -f/--file option is present, ALL files within the -d/--directory directory will be attached.
75
+
76
+ ## Contributing
77
+
78
+ 1. Fork it
79
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
80
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
81
+ 4. Push to the branch (`git push origin my-new-feature`)
82
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/clemail ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'command_line_email'
4
+
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'command_line_email/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "command_line_email"
8
+ gem.version = CommandLineEmail::VERSION
9
+ gem.authors = ["James Lavin"]
10
+ gem.email = ["command_line_email@futureresearch.com"]
11
+ gem.description = %q{Send email from the Linux or Mac command line}
12
+ gem.summary = %q{Send email from the Linux or Mac command line. Can attach specific files or all files in a directory. Can load email body from a file.}
13
+ gem.homepage = "https://github.com/JamesLavin/command_line_email"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift("#{File.expand_path(File.dirname(__FILE__))}")
2
+ $LOAD_PATH.unshift("#{File.expand_path(File.dirname(__FILE__))}/extruby/mail")
3
+ $LOAD_PATH.unshift("#{File.expand_path(File.dirname(__FILE__))}/command_line_email")
4
+
5
+ require "mail"
6
+ require "mail/message"
7
+ require "command_line_email/version"
8
+ require "command_line_email/send_mail_setup"
9
+ require "command_line_email/command_line_email"
@@ -0,0 +1,89 @@
1
+ # encoding : UTF-8
2
+
3
+ require 'mail'
4
+ require 'yaml'
5
+ require 'optparse'
6
+ require 'send_mail_setup'
7
+
8
+ user_config = CommandLineEmail::SendMailSetup.new
9
+
10
+ Mail.defaults do
11
+ delivery_method :smtp, user_config.mail_options
12
+ end
13
+
14
+ mail_attrs = {to: []}
15
+
16
+ option_parser = OptionParser.new do |opts|
17
+
18
+ opts.on("-t TO","--to TO") do |to|
19
+ mail_attrs[:to] = to_string_to_mailing_list(user_config, to, mail_attrs[:to])
20
+ end
21
+
22
+ opts.on("-f FROM","--from FROM") do |from|
23
+ mail_attrs[:from] = from
24
+ end
25
+
26
+ opts.on("-b BODY","--body BODY") do |body|
27
+ mail_attrs[:body] ||= ""
28
+ mail_attrs[:body] << body + "\n"
29
+ end
30
+
31
+ opts.on("-s SUBJECT","--subject SUBJECT") do |subject|
32
+ mail_attrs[:subject] = subject
33
+ end
34
+
35
+ opts.on("-c CC","--cc CC") do |cc|
36
+ if user_config.mailing_lists.has_key? cc.to_sym
37
+ mail_attrs[:cc] = user_config.mailing_lists[cc.to_sym]
38
+ else
39
+ mail_attrs[:cc] = cc
40
+ end
41
+ end
42
+
43
+ opts.on("-f FILE","--add-file FILE") do |file|
44
+ mail_attrs[:files] ||= []
45
+ mail_attrs[:files] << file
46
+ end
47
+
48
+ opts.on("-d DIRECTORY","--directory DIRECTORY") do |directory|
49
+ mail_attrs[:directory] = directory
50
+ end
51
+
52
+ end
53
+
54
+ def to_string_to_mailing_list(user_config, to, list=[])
55
+ # takes optional list in case you want to add to an existing list
56
+ if user_config.mailing_lists.has_key? to.to_sym
57
+ Array(user_config.mailing_lists[to.to_sym]).each { |address| list << address }
58
+ else
59
+ list << to
60
+ end
61
+ list
62
+ end
63
+
64
+ def grab_text_from_filename_if_file_exists(filename_or_text)
65
+ if File.exists?(filename_or_text)
66
+ File.open(filename_or_text,'r').read
67
+ else
68
+ filename_or_text
69
+ end
70
+ end
71
+
72
+ option_parser.parse!
73
+
74
+ puts mail_attrs
75
+
76
+ mail = Mail.new do
77
+ from mail_attrs[:from] || user_config.defaults[:from]
78
+ to mail_attrs[:to] || user_config.defaults[:to]
79
+ cc mail_attrs[:cc] || user_config.defaults[:cc] || nil
80
+ subject mail_attrs[:subject] || user_config.defaults[:subject] || ''
81
+ body mail_attrs[:body] ? grab_text_from_filename_if_file_exists(mail_attrs[:body]) : ''
82
+ if mail_attrs[:files]
83
+ attach_selected(mail_attrs[:files], mail_attrs[:directory] || '')
84
+ elsif mail_attrs[:directory]
85
+ attach_all_from_directory(mail_attrs[:directory])
86
+ end
87
+ end
88
+
89
+ mail.deliver!
@@ -0,0 +1,40 @@
1
+ # encoding : UTF-8
2
+
3
+ module CommandLineEmail
4
+
5
+ class SendMailSetup
6
+
7
+ attr_reader :mail_options, :mail_config_file
8
+
9
+ def initialize(config_file = nil)
10
+ @mail_config_file = config_file || File.expand_path('~/.command_line_email.yml')
11
+ set_mail_options
12
+ end
13
+
14
+ def mailing_lists
15
+ mail_config[:mailing_lists]
16
+ end
17
+
18
+ def defaults
19
+ mail_config[:defaults]
20
+ end
21
+
22
+ private
23
+
24
+ def mail_config
25
+ @mail_config ||= YAML::load(File.open(mail_config_file))
26
+ end
27
+
28
+ def set_mail_options
29
+ mail_options = {}
30
+
31
+ mail_config[:connection].keys.each do |key|
32
+ mail_options[key] = mail_config[:connection][key]
33
+ end
34
+
35
+ @mail_options = mail_options
36
+ end
37
+
38
+ end
39
+
40
+ end
@@ -0,0 +1,5 @@
1
+ # encoding: UTF-8
2
+
3
+ module CommandLineEmail
4
+ VERSION = "0.0.1"
5
+ end
@@ -0,0 +1,19 @@
1
+ module Mail
2
+
3
+ class Message
4
+
5
+ def attach_selected(filenames, dir = '')
6
+ filenames.each do |filename|
7
+ add_file dir + filename
8
+ end
9
+ end
10
+
11
+ def attach_all_from_directory(directory)
12
+ Dir.glob(directory + '/*').each do |filepath|
13
+ add_file filepath
14
+ end
15
+ end
16
+
17
+ end
18
+
19
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: command_line_email
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - James Lavin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-25 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Send email from the Linux or Mac command line
15
+ email:
16
+ - command_line_email@futureresearch.com
17
+ executables:
18
+ - clemail
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - bin/clemail
28
+ - command_line_email.gemspec
29
+ - lib/command_line_email.rb
30
+ - lib/command_line_email/command_line_email.rb
31
+ - lib/command_line_email/send_mail_setup.rb
32
+ - lib/command_line_email/version.rb
33
+ - lib/extruby/mail/message.rb
34
+ homepage: https://github.com/JamesLavin/command_line_email
35
+ licenses: []
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 1.8.23
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: Send email from the Linux or Mac command line. Can attach specific files
58
+ or all files in a directory. Can load email body from a file.
59
+ test_files: []