actionmailer-javamail 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,11 @@
1
+ === 0.1.0 / 2009-05-07
2
+
3
+ * Initial release
4
+ * Support for SMTP and SMTPS (over SSL)
5
+ * [Experimental] Untested support for Google App Engine
6
+ * Support for DKIM message signing
7
+ * Support for authentication
8
+
9
+ * Coming soon
10
+ * Google App Engine verification
11
+ * Unit tests
@@ -0,0 +1,12 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ ext/dkim.jar
6
+ init.rb
7
+ lib/action_mailer/java_mail.rb
8
+ lib/java_mail.rb
9
+ lib/java_mail/config.rb
10
+ lib/java_mail/java.rb
11
+ lib/java_mail/mailer.rb
12
+ lib/javamail.rb
@@ -0,0 +1,86 @@
1
+ = actionmailer-javamail
2
+
3
+ == DESCRIPTION:
4
+
5
+ ActionMailer-JavaMail allows the emails to be delivered via the JavaMail library. This allows to
6
+ expand the capabilities of ActionMailer in the jRuby environment via existing JavaMail capabilities.
7
+
8
+ The current capabilities are only implemented to send outgoing email without any support for incoming email.
9
+
10
+ Action Mailer is the part of the Ruby on Rails framework that's designing email-service layers.
11
+
12
+ == FEATURES/PROBLEMS:
13
+
14
+ * Support for SMTP, SMTPS
15
+ * Support for DKIM message signing
16
+ * [Untested] Support for using Google App Engine
17
+ * [Problem] No unit tests
18
+
19
+ == REQUIREMENTS:
20
+
21
+ * ActionMailer 2.3.2
22
+ * JavaMail libraries in your CLASSPATH
23
+
24
+ == INSTALL:
25
+
26
+ * sudo gem install actionmailer-javamail
27
+
28
+ == CONFIGURATION:
29
+
30
+ In your config/environment.rb, add the following:
31
+
32
+ config.gem "actionmailer-javamail", :lib => 'java_mail' if defined?(JRUBY_VERSION)
33
+
34
+ In one of your config/initializer files, add the following:
35
+
36
+ ActionMailer::Base.delivery_method = :javamail
37
+ ActionMailer::Base.javamail_settings = {
38
+ :protocol => :smtps,
39
+ :address => 'smtp.gmail.com',
40
+ :port => 465,
41
+ :domain => 'mydomain.com',
42
+ :user_name => 'user@gmail.com',
43
+ :password => 'password',
44
+ :dkim => { :domain => 'mydomain.com', :selector => 'mysel', :key_file => KEY_FILE_PATH }
45
+ }
46
+
47
+ :protocol - the possible values are :smtp, :smtps, :none. (:none is used for an preconfigured JavaMail)
48
+ :address - allows you to use a remote server
49
+ :port - currently a required option for SMTP port
50
+ :domain - domain to be specified in HELO command
51
+ :user_name - user_name for server authentication
52
+ :password - password for server authentication
53
+ :dkim - (optional) arguments for DKIM signing. Key file needs to be in DER format
54
+
55
+ == REFERENCES:
56
+
57
+ * The gem includes a copy of DKIM message signing library
58
+ http://dkim-javamail.sourceforge.net
59
+
60
+ * The following patch was applied to the library
61
+ http://gist.github.com/108966
62
+
63
+ == LICENSE:
64
+
65
+ (The MIT License)
66
+
67
+ Copyright (c) 2009 Michael Rykov
68
+
69
+ Permission is hereby granted, free of charge, to any person obtaining
70
+ a copy of this software and associated documentation files (the
71
+ 'Software'), to deal in the Software without restriction, including
72
+ without limitation the rights to use, copy, modify, merge, publish,
73
+ distribute, sublicense, and/or sell copies of the Software, and to
74
+ permit persons to whom the Software is furnished to do so, subject to
75
+ the following conditions:
76
+
77
+ The above copyright notice and this permission notice shall be
78
+ included in all copies or substantial portions of the Software.
79
+
80
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
81
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
82
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
83
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
84
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
85
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
86
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,14 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+ require './lib/java_mail.rb'
6
+
7
+ Hoe.new('actionmailer-javamail', JavaMail::VERSION) do |p|
8
+ p.rubyforge_name = 'am-javamail'
9
+ p.developer('Michael Rykov', 'mrykov@gmail.com')
10
+ p.url = 'http://github.com/rykov/actionmailer-javamail'
11
+ p.blog_categories = ['actionmailer-javamail', 'actionmailer', 'javamail', 'jruby', 'rails', 'ruby', 'java']
12
+ end
13
+
14
+ # vim: syntax=Ruby
Binary file
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'java_mail' if defined?(JRUBY_VERSION)
@@ -0,0 +1,14 @@
1
+ module ActionMailer
2
+ module JavaMail
3
+ def self.included(base) #:nodoc:
4
+ base.cattr_accessor :javamail_settings
5
+ base.javamail_settings = {}
6
+ end
7
+
8
+ def perform_delivery_javamail(mail)
9
+ ::JavaMail::Mailer.open(javamail_settings) do |mailer|
10
+ mailer.send_message(mail)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,46 @@
1
+ #--
2
+ # Copyright (c) 2009 Michael Rykov
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+
24
+ $:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
25
+
26
+ require 'java'
27
+
28
+ module JavaMail
29
+ VERSION = '0.1.0'
30
+
31
+ unless defined?(JAVAMAIL_HOME)
32
+ JAVAMAIL_HOME = File.expand_path(File.dirname(__FILE__) + '/..')
33
+ end
34
+
35
+ # wraps all native exceptions
36
+ class JavaMailError < RuntimeError; end
37
+ end
38
+
39
+ require 'java_mail/java'
40
+ require 'java_mail/config'
41
+ require 'java_mail/mailer'
42
+
43
+ if defined?(ActionMailer::Base)
44
+ require 'action_mailer/java_mail'
45
+ ActionMailer::Base.send(:include, ActionMailer::JavaMail)
46
+ end
@@ -0,0 +1,62 @@
1
+ module JavaMail
2
+ class Config
3
+ def initialize(settings = {})
4
+ @settings = settings
5
+ @settings[:protocol] ||= :none
6
+
7
+ unless [:smtp, :smtps, :none].include?(@settings[:protocol])
8
+ raise JavaMailError.new("Invalid protocol #{@settings[:protocol].to_s}")
9
+ end
10
+ end
11
+
12
+ def session_properties
13
+ props = java.util.Properties.new()
14
+ return props if @mode == :none
15
+
16
+ # We only support SMTP (plan and SSL) for now
17
+ props.put('mail.transport.protocol', @settings[:protocol].to_s);
18
+
19
+ # Let's do the basic mapping for SMTP settings
20
+ session_property_key_map.each_pair do |key, property|
21
+ props.put(property, @settings[key].to_s) if @settings[key]
22
+ end
23
+
24
+ # Enable authentication and encryption
25
+ props.put('mail.smtp.auth', 'true') if self.auth?
26
+ props.put('mail.smtp.ssl.protocols', 'SSLv3 TLSv1') if @settings[:protocol] == :smtps;
27
+
28
+ # Debugging
29
+ props.put('mail.debug', 'true') if self.debug?
30
+
31
+ # Return
32
+ props
33
+ end
34
+
35
+ def debug?
36
+ @settings[:debug] ? true : false
37
+ end
38
+
39
+ def dkim?
40
+ @settings[:dkim] && @settings[:dkim][:domain] && @settings[:dkim][:selector] && @settings[:dkim][:key_file]
41
+ end
42
+
43
+ def auth?
44
+ @settings[:user_name] && @settings[:password]
45
+ end
46
+
47
+ [:address, :user_name, :password, :port, :dkim].each do |attr|
48
+ self.class_eval "def #{attr.to_s}; @settings[:#{attr}]; end"
49
+ end
50
+
51
+ protected
52
+ def session_property_key_map
53
+ @@key_map ||= {
54
+ :address => 'mail.smtp.host',
55
+ :port => 'mail.smtp.port',
56
+ :domain => 'mail.smtp.localhost',
57
+ :user_name => 'mail.smtp.user',
58
+ :enable_starttls_auto => 'mail.smtp.starttls.enable'
59
+ }
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,9 @@
1
+ module JavaMail
2
+ module Java
3
+ require 'java'
4
+ Dir["#{JavaMail::JAVAMAIL_HOME}/ext/**/*.jar"].sort.each {|l| require l}
5
+ include_package "javax.mail"
6
+ include_package "javax.mail.internet"
7
+ include_package "de.agitos.dkim"
8
+ end
9
+ end
@@ -0,0 +1,64 @@
1
+ module JavaMail
2
+ class Mailer
3
+ def initialize(settings = {})
4
+ @config = JavaMail::Config.new(settings)
5
+ @session = JavaMail::Java::Session.getInstance(@config.session_properties)
6
+ @session.setDebug(@config.debug?)
7
+ @transport = @session.getTransport
8
+ end
9
+
10
+ def self.open(settings, &block)
11
+ self.new(settings).open(&block)
12
+ end
13
+
14
+ def open(&block)
15
+ begin
16
+ open_transport unless transport_ready?
17
+ block.call(self)
18
+ rescue Exception => e
19
+ raise JavaMail::JavaMailError.new(e)
20
+ ensure
21
+ close_transport if transport_ready?
22
+ end
23
+ end
24
+
25
+ def send_message(tmail)
26
+ raise JavaMail::JavaMailError.new("JavaMail Transport is not connected") unless transport_ready?
27
+ send_message_to_transport(tmail, @transport)
28
+ end
29
+
30
+ protected
31
+ def transport_ready?
32
+ @transport && @transport.connected?
33
+ end
34
+
35
+ def open_transport
36
+ @transport.connect(@config.address, @config.port.to_i, @config.user_name, @config.password)
37
+ end
38
+
39
+ def close_transport
40
+ @transport.close
41
+ end
42
+
43
+ def send_message_to_transport(tmail, transport)
44
+ # Dump TMail into ByteArray
45
+ io_obj = java.io.ByteArrayInputStream.new(tmail.encoded.to_java_bytes)
46
+
47
+ # Convert message ByteArray to MimeMessage
48
+ msg = if @config.dkim?
49
+ dkimSigner = JavaMail::Java::DKIMSigner.new(@config.dkim[:domain], @config.dkim[:selector], @config.dkim[:key_file]);
50
+ JavaMail::Java::SMTPDKIMMessage.new(@session, io_obj, dkimSigner);
51
+ else
52
+ JavaMail::Java::MimeMessage.new(@session, io_obj);
53
+ end
54
+
55
+ # Convert recipients to InternetAddress java array
56
+ recipients = tmail.to_addrs([]).map do |addy|
57
+ JavaMail::Java::InternetAddress.new(addy.address)
58
+ end.to_java(JavaMail::Java::InternetAddress)
59
+
60
+ # Send message
61
+ transport.sendMessage(msg, recipients)
62
+ end
63
+ end
64
+ end
@@ -0,0 +1 @@
1
+ require 'java_mail'
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ extensions: []
3
+
4
+ homepage: http://github.com/rykov/actionmailer-javamail
5
+ executables: []
6
+
7
+ version: !ruby/object:Gem::Version
8
+ version: 0.1.0
9
+ post_install_message:
10
+ date: 2009-05-10 07:00:00 +00:00
11
+ files:
12
+ - History.txt
13
+ - Manifest.txt
14
+ - README.txt
15
+ - Rakefile
16
+ - ext/dkim.jar
17
+ - init.rb
18
+ - lib/action_mailer/java_mail.rb
19
+ - lib/java_mail.rb
20
+ - lib/java_mail/config.rb
21
+ - lib/java_mail/java.rb
22
+ - lib/java_mail/mailer.rb
23
+ - lib/javamail.rb
24
+ rubygems_version: 1.3.1
25
+ rdoc_options:
26
+ - --main
27
+ - README.txt
28
+ signing_key:
29
+ cert_chain: []
30
+
31
+ name: actionmailer-javamail
32
+ has_rdoc: true
33
+ platform: ruby
34
+ summary: ActionMailer-JavaMail allows the emails to be delivered via the JavaMail
35
+ library
36
+ default_executable:
37
+ bindir: bin
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ version:
40
+ requirements:
41
+ - - '>='
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ version:
46
+ requirements:
47
+ - - '>='
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ require_paths:
51
+ - lib
52
+ - ext
53
+ specification_version: 2
54
+ test_files: []
55
+
56
+ dependencies:
57
+ - !ruby/object:Gem::Dependency
58
+ type: :development
59
+ name: hoe
60
+ version_requirement:
61
+ version_requirements: !ruby/object:Gem::Requirement
62
+ version:
63
+ requirements:
64
+ - - '>='
65
+ - !ruby/object:Gem::Version
66
+ version: 1.12.2
67
+ description: ActionMailer-JavaMail allows the emails to be delivered via the JavaMail
68
+ library. This allows to expand the capabilities of ActionMailer in the jRuby environment
69
+ via existing JavaMail capabilities. The current capabilities are only implemented
70
+ to send outgoing email without any support for incoming email. Action Mailer is
71
+ the part of the Ruby on Rails framework that's designing email-service layers.
72
+ email:
73
+ - mrykov@gmail.com
74
+ authors:
75
+ - Michael Rykov
76
+ extra_rdoc_files:
77
+ - History.txt
78
+ - Manifest.txt
79
+ - README.txt
80
+ requirements: []
81
+
82
+ rubyforge_project: am-javamail
83
+ autorequire: