macmailer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/bin/macmailer +43 -0
  2. data/lib/macmailer.rb +67 -0
  3. metadata +63 -0
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/env ruby
2
+ require "trollop"
3
+ require "macmailer"
4
+
5
+ opts = Trollop::options do
6
+ recipient_note = "either as just an address, or an address followed by a name. Can be declared multple times."
7
+ opt :subject, "Subject line.", :short => "-s", :type => :string
8
+ opt :body, "Body.", :short => "-b", :type => :string
9
+ opt :to, "Specify a To: recepient, #{recipient_note}", :multi => true, :type => :strings
10
+ opt :cc, "Specify a CC: recepient, #{recipient_note}", :multi => true, :type => :strings
11
+ opt :bcc, "Specify a BCC: recepient, #{recipient_note}", :multi => true, :type => :strings
12
+ opt :send, "Automatically send the email. If not specified, script only creates the email."
13
+ opt :show, "Bring Mail.app to the front of your screen. Otherwise, remains in background."
14
+ opt :thanks, "Write Jeremy a note. (Note: Does *not* auto-send message. Overrides all other options.)"
15
+ end
16
+
17
+ # Send thanks and exit if "--thanks" flag is provided
18
+ if opts[:thanks]
19
+ MacMailer::Message.new({
20
+ :subject => "re. MacMailer",
21
+ :body => "Thanks! It's freaking awesome.",
22
+ :recipients => [
23
+ :address => "jsvine@gmail.com",
24
+ :name => "Jeremy Singer-Vine"
25
+ ]
26
+ }).create.show
27
+ exit
28
+ end
29
+
30
+ # Otherwise, create and/or send the message
31
+ recipient_types = [ :to, :cc, :bcc ]
32
+ def opt_to_recipients (multi, type)
33
+ multi.map {|m| { :address => m.shift, :name => m.join(" "), :type => type } }
34
+ end
35
+
36
+ message = MacMailer::Message.new({
37
+ :subject => opts[:subject],
38
+ :body => opts[:body],
39
+ :recipients => recipient_types.map do |sym|
40
+ opt_to_recipients(opts[sym], sym)
41
+ end.flatten(1)
42
+ }).method(opts[:send] ? :send : :create).call
43
+ message.show if opts[:show]
@@ -0,0 +1,67 @@
1
+ #!/usr/bin/env ruby
2
+ # The MacMailer module lets you create, and send, emails
3
+ # through OSX's Mail.app program. At its core, MacMailer
4
+ # just writes and executes a temporary AppleScript script.
5
+ module MacMailer
6
+ # Message is the main MacMailer class.
7
+ class Message
8
+ # Creates a MacMailer::Message instance. `props` can be left blank
9
+ # for a blank message, but you'll probably want to pass along some
10
+ # details. Valid keys for `props` include:
11
+ #
12
+ # :subject => String
13
+ # :body => String
14
+ # :recipients => Array (of hashes)
15
+ #
16
+ # Each hash in :recipients supports the following keys:
17
+ #
18
+ # :address => String
19
+ # :name => String
20
+ # :type => :to, :cc, or :bcc
21
+ def initialize (props={})
22
+ @props = props
23
+ end
24
+
25
+ # Creates an email, based on `@props`, using AppleScript.
26
+ # Does not send the email unless the optional `send` parameter
27
+ # is set to `true`.
28
+ def create (send=false)
29
+ script = <<-SCRIPT
30
+ tell application "Mail"
31
+ set msg to make new outgoing message with properties {subject:#{(@props[:subject] || "").dump}, content:#{(@props[:body] || "").dump}, visible:true}
32
+ tell msg
33
+ #{(@props[:recipients] || []).map {|x| _make_recipient(x[:address], x[:name], x[:type]) }.join("\n") }
34
+ #{send ? "send" : ""}
35
+ end tell
36
+ end tell
37
+ SCRIPT
38
+ @shell_response = `/usr/bin/osascript > /dev/null <<MACMAILERSCRIPT
39
+ #{script}
40
+ \nMACMAILERSCRIPT`
41
+ self
42
+ end
43
+
44
+ # Convenience method to create *and* send the message.
45
+ def send
46
+ self.create(send=true)
47
+ end
48
+
49
+ # Brings Mail.app to the front, using AppleScript.
50
+ def show
51
+ `/usr/bin/osascript > /dev/null <<MACMAILERSCRIPT
52
+ tell application "Mail"
53
+ activate
54
+ end tell
55
+ \nMACMAILERSCRIPT`
56
+ self
57
+ end
58
+
59
+ # Helper method to write the AppleScript command to add
60
+ # a new recipient to a message.
61
+ private
62
+ def _make_recipient (address, name, type)
63
+ "make new #{(type || :to).to_s} recipient with properties " +
64
+ "{name:#{(name || address || "").dump}, address:#{(address || "").dump}}"
65
+ end
66
+ end
67
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: macmailer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jeremy Singer-Vine
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-29 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: trollop
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: Ruby binding and command-line interface for Apple's Mail.app
31
+ email: jsvine@gmail.com
32
+ executables:
33
+ - macmailer
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - lib/macmailer.rb
38
+ - bin/macmailer
39
+ homepage: https://github.com/jsvine/macmailer
40
+ licenses: []
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 1.8.24
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: Ruby x Mail.app
63
+ test_files: []