kelredd-useful 0.1.16 → 0.1.17

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ Dir[File.join(File.dirname(__FILE__), "mailer" ,"*.rb")].each do |file|
2
+ require file
3
+ end
@@ -0,0 +1,89 @@
1
+ require 'openssl'
2
+ require 'net/smtp'
3
+ require 'tmail'
4
+ require 'sinatra/base'
5
+ require File.join([File.dirname(__FILE__), 'exceptions'])
6
+ require File.join([File.dirname(__FILE__), 'tls'])
7
+
8
+ module Useful
9
+ module SinatraHelpers
10
+ module Mailer
11
+
12
+ class Base
13
+
14
+ CONFIGS = [:server, :domain, :port, :reply_to, :username, :password, :authentication, :content_type, :charset]
15
+ CONFIGS.each do |config|
16
+ attr_reader config
17
+ end
18
+ attr_accessor :logger
19
+
20
+ def initialize(configs={})
21
+ CONFIGS.each do |config|
22
+ instance_variable_set("@#{config}", configs[config])
23
+ end
24
+ @authentication ||= :login
25
+ @reply_to ||= @username
26
+ @content_type ||= 'text/plain'
27
+ @charset ||= 'UTF-8'
28
+ @logger = block_given? ? yield : nil
29
+ end
30
+
31
+ def send(settings={})
32
+ check_configs
33
+ [:to, :subject].each do |setting|
34
+ raise Useful::SinatraHelpers::Mailer::SendError, "cannot send, #{setting} not specified." unless settings[setting]
35
+ end
36
+ mail = generate_mail(settings)
37
+ mail.body = yield(mail) if block_given?
38
+ mail.body ||= ''
39
+ Sinatra::Application.environment.to_s == 'production' ? send_mail(mail) : log_mail(mail)
40
+ log(:info, "Sent '#{mail.subject}' to #{mail.to.join(',')}")
41
+ mail
42
+ end
43
+
44
+ protected
45
+
46
+ def check_configs
47
+ CONFIGS.each do |config|
48
+ raise Useful::SinatraHelpers::Mailer::ConfigError, "#{config} not configured." unless instance_variable_get("@#{config}")
49
+ end
50
+ end
51
+
52
+ def generate_mail(settings)
53
+ mail = TMail::Mail.new
54
+ mail.to = Array.new([settings[:to]])
55
+ mail.from = @reply_to
56
+ mail.reply_to = @reply_to
57
+ mail.subject = settings[:subject]
58
+ mail.date = Time.now
59
+ mail.set_content_type @content_type
60
+ mail.charset = @charset
61
+ mail
62
+ end
63
+
64
+ def send_mail(mail)
65
+ raise Useful::SinatraHelpers::Mailer::SendError, "cannot send, bad (or empty) mail object given." unless mail
66
+ Net::SMTP.start(@server, @port, @domain, @username, @password, @authentication) do |server|
67
+ mail.to.each {|recipient| server.send_message(mail.to_s, mail.from, recipient) }
68
+ end
69
+ end
70
+
71
+ def log_mail(mail)
72
+ log(:debug, mail.to_s)
73
+ end
74
+
75
+ def log(level, msg)
76
+ if(msg)
77
+ if @logger && @logger.respond_to(level)
78
+ @logger.send(level.to_s, msg)
79
+ else
80
+ puts "** [#{level.to_s.upcase}]: [Mailer] #{msg}"
81
+ end
82
+ end
83
+ end
84
+
85
+ end
86
+
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,17 @@
1
+ module Useful
2
+ module SinatraHelpers
3
+ module Mailer
4
+
5
+ class MailerError < StandardError
6
+ end
7
+
8
+ class ConfigError < MailerError
9
+ end
10
+
11
+ class SendError < MailerError
12
+ end
13
+
14
+ end
15
+ end
16
+ end
17
+
@@ -0,0 +1,71 @@
1
+ # Note: This is a complete rip of http://github.com/ambethia/smtp-tls/tree/master
2
+ # => I chose to copy the source in here instead of add yet another gem depencency
3
+ # => I take no credit for this work, check out link for more info.
4
+
5
+ Net::SMTP.class_eval do
6
+ private
7
+ def do_start(helodomain, user, secret, authtype)
8
+ raise IOError, 'SMTP session already started' if @started
9
+
10
+ if RUBY_VERSION > "1.8.6"
11
+ check_auth_args user, secret if user or secret
12
+ else
13
+ check_auth_args user, secret, authtype if user or secret
14
+ end
15
+
16
+ sock = timeout(@open_timeout) { TCPSocket.open(@address, @port) }
17
+ @socket = Net::InternetMessageIO.new(sock)
18
+ @socket.read_timeout = 60 #@read_timeout
19
+
20
+ check_response(critical { recv_response() })
21
+ do_helo(helodomain)
22
+
23
+ if starttls
24
+ raise 'openssl library not installed' unless defined?(OpenSSL)
25
+ ssl = OpenSSL::SSL::SSLSocket.new(sock)
26
+ ssl.sync_close = true
27
+ ssl.connect
28
+ @socket = Net::InternetMessageIO.new(ssl)
29
+ @socket.read_timeout = 60 #@read_timeout
30
+ do_helo(helodomain)
31
+ end
32
+
33
+ authenticate user, secret, authtype if user
34
+ @started = true
35
+ ensure
36
+ unless @started
37
+ # authentication failed, cancel connection.
38
+ @socket.close if not @started and @socket and not @socket.closed?
39
+ @socket = nil
40
+ end
41
+ end
42
+
43
+ def do_helo(helodomain)
44
+ begin
45
+ if @esmtp
46
+ ehlo helodomain
47
+ else
48
+ helo helodomain
49
+ end
50
+ rescue Net::ProtocolError
51
+ if @esmtp
52
+ @esmtp = false
53
+ @error_occured = false
54
+ retry
55
+ end
56
+ raise
57
+ end
58
+ end
59
+
60
+ def starttls
61
+ getok('STARTTLS') rescue return false
62
+ return true
63
+ end
64
+
65
+ def quit
66
+ begin
67
+ getok('QUIT')
68
+ rescue EOFError
69
+ end
70
+ end
71
+ end
@@ -3,7 +3,7 @@ module Useful
3
3
 
4
4
  MAJOR = 0
5
5
  MINOR = 1
6
- TINY = 16
6
+ TINY = 17
7
7
 
8
8
  def self.to_s # :nodoc:
9
9
  [MAJOR, MINOR, TINY].join('.')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kelredd-useful
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.16
4
+ version: 0.1.17
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kelredd
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-06-30 00:00:00 -07:00
12
+ date: 2009-07-12 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -67,6 +67,11 @@ files:
67
67
  - lib/useful/sinatra_helpers/erb/partials.rb
68
68
  - lib/useful/sinatra_helpers/erb/tags.rb
69
69
  - lib/useful/sinatra_helpers/erb.rb
70
+ - lib/useful/sinatra_helpers/mailer
71
+ - lib/useful/sinatra_helpers/mailer/base.rb
72
+ - lib/useful/sinatra_helpers/mailer/exceptions.rb
73
+ - lib/useful/sinatra_helpers/mailer/tls.rb
74
+ - lib/useful/sinatra_helpers/mailer.rb
70
75
  - lib/useful/sinatra_helpers.rb
71
76
  - lib/useful/version.rb
72
77
  - lib/useful.rb