jangosmtp 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ *.tmproj
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 jangosmtp.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Brennon Loveless
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.
@@ -0,0 +1,89 @@
1
+ # Jangosmtp
2
+
3
+ This gem will allow a user to easily incorporate jangosmtp into their existing rails project. This gem uses the jangosmtp api rather than the jangosmtp smtp relay since, according to the jangosmtp docs, the api is faster and more reliable than using the jangosmtp relay. Currently at our company we are only using the send and transaction group functions, but if you would like, feel free to make a request for more functions as well as forking this repo and submitting a new pull request. Please enjoy and I do apologize for not having any tests at this very moment.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'jangosmtp'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install jangosmtp
18
+
19
+ ## Configuration
20
+
21
+ In your environment file there are a few options that you can pass to Jangosmtp as follows:
22
+
23
+ After the gem is installed you will need to add your jango configuration to your environment file. Using the following lines of code. Pay close attention to the comments here.
24
+
25
+ Jangosmtp.options({
26
+ username: 'username',
27
+ password: 'password',
28
+ max_attempts: 1,
29
+ click_tracking: true,
30
+ open_tracking: true,
31
+ auto_generate_plain: true
32
+ })
33
+
34
+ * `username` __is required__ and is the same username you use to login to jangosmtp.
35
+ * `password` __is required__ and is the same password you use to login to jangosmtp.
36
+ * `max_attempts` is __not required__, but if there is a failed call to jangosmtp the gem will attempt the try again every three seconds until max_attempts is reached. There is a catch here, jangosmtp relies on 500 internal server errors even when a value is listed incorrectly, so this will slow down your application. For example: when creating a groups this gem tries to get the group first. If jangosmtp does not find the requested group then it will throw a 500 internal server error. Which will cause the system to try again until the max number of attempts are reached before attempting to create the group. I'm working with jangosmtp for a fix to this issue.
37
+ * `click_tracking` is __not required__ but will default to true which enables tracking for whenever anyone clicks on any link that is included in your email.
38
+ * `open_tracking` is __not required__ but will default to true which enables tracking for whenever anyone opens an email you sent, this is done by jangosmtp including a transparent image in your email so they can monitor when the image is downloaded from their server.
39
+ * `auto_generate_plain` is __not required__ but will default to true which tells jangosmtp that you are sending html content for your email and that jangosmtp should generate the plain text version of the email to be sent along with your email, this is recommended since this gem doesn't currently have the ability to send plain text emails
40
+
41
+ ## Usage
42
+
43
+ ### Get and/or create group
44
+ Get a group if the group exists, otherwise create a group using a group name and return it's id
45
+ `group_name`: group name that the user wants to get the id for, should only contain alphanumeric ( and spaces ) will be cleaned if an incorrect name is used
46
+
47
+ get_create_group( group_name )
48
+
49
+ ### Get group id
50
+ Get the id of a requested group
51
+ `group_name`: group name that the user wants to get the id for, should only contain alphanumeric ( and spaces ) will be cleaned if an incorrect name is used
52
+
53
+ get_group_id( group_name )
54
+
55
+ ### Create group
56
+ Create a group and return the successfull value
57
+ `group_name`: group name that the user wants to get the id for, should only contain alphanumeric ( and spaces ) will be cleaned if an incorrect name is used
58
+
59
+ create_group( group_name )
60
+
61
+ ### Send an email using group name
62
+ Send an email and get/create a requested group
63
+ `group_name`: group name that the user wants the email to be applied to, should only contain alphanumeric ( and spaces ) will be cleaned if an incorrect name is used
64
+ `to_email`: the single email address that the email will be sent to
65
+ `from_email`: the email address that the email will be coming from
66
+ `from_name`: the name that the email address will be coming from
67
+ `html`: the html of the email message to be sent
68
+
69
+ ___IMPORTANT___: This function will attempt to get or create a group for each email that is sent. If you will be sending a lot of emails to the same group I would recommend you create the group first and use the `send_email_with_group_id` function since that takes a group_id and won't attempt to create the group with each email that is sent
70
+
71
+ send_email( group_name, to_email, from_email, from_name, html )
72
+
73
+ ### Send an email using group id
74
+ Send an email using a pre-existing group
75
+ `group_id`: the id of the group that this email will be applied to
76
+ `to_email`: the single email address that the email will be sent to
77
+ `from_email`: the email address that the email will be coming from
78
+ `from_name`: the name that the email address will be coming from
79
+ `html`: the html of the email message to be sent
80
+
81
+ send_email_with_group_id( group_id, to_email, from_email, from_name, html )
82
+
83
+ ## Contributing
84
+
85
+ 1. Fork it
86
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
87
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
88
+ 4. Push to the branch (`git push origin my-new-feature`)
89
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/jangosmtp/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Brennon Loveless"]
6
+ gem.email = ["brennon@fritzandandre.com"]
7
+ gem.description = %q{Library for interfacing with JangoSMTP}
8
+ gem.summary = %q{Will encapsulate all the necessary api calls into an easy to use gem.}
9
+ gem.homepage = "https://github.com/jbrennon/jangosmtp"
10
+
11
+ gem.add_dependency 'mechanize'
12
+
13
+ gem.files = `git ls-files`.split($\)
14
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
15
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
16
+ gem.name = "jangosmtp"
17
+ gem.require_paths = ["lib"]
18
+ gem.version = Jangosmtp::VERSION
19
+ end
@@ -0,0 +1,155 @@
1
+ require 'jangosmtp/version'
2
+
3
+ module Jangosmtp
4
+
5
+ class << self
6
+ attr_accessor :username, :password, :max_attempts, :click_tracking, :open_tracking, :auto_generate_plain
7
+ BASE_URL = 'http://api.jangomail.com/api.asmx/'
8
+
9
+ def options( hash )
10
+ @username = hash[:username] if !hash[:username].nil?
11
+ @password = hash[:password] if !hash[:password].nil?
12
+ @max_attempts = hash[:max_attempts] ||= 1
13
+ @click_tracking = hash[:click_tracking] ||= true
14
+ @open_tracking = hash[:open_tracking] ||= true
15
+ @auto_generate_plain = hash[:open_tracking] ||= true
16
+ end
17
+
18
+ # Get a group if the group exists, otherwise create a group using a group name and return it's id
19
+ # group_name: group name that the user wants to get the id for, should only contain alphanumeric ( and spaces )
20
+ # will be cleaned if an incorrect name is used
21
+ def get_create_group( group_name )
22
+ check_user_pass
23
+ existing_group_id = get_group_id( group_name )
24
+ if existing_group_id == nil
25
+ new_group_id = create_group( group_name )
26
+ return new_group_id
27
+ end
28
+ return existing_group_id
29
+ end
30
+
31
+ # Get the id of a requested group
32
+ # group_name: group name that the user wants to get the id for, should only contain alphanumeric ( and spaces )
33
+ # will be cleaned if an incorrect name is used
34
+ def get_group_id( group_name )
35
+ check_user_pass
36
+ # First we need to clean the group_name since jangosmtp only allows alphanumeric characters
37
+ group_name.tr!('^A-Za-z0-9 ', '')
38
+ options = {
39
+ 'Username' => @username,
40
+ 'Password' => @password,
41
+ 'GroupName' => group_name
42
+ }
43
+
44
+ # First we are going to check the existing groups to make sure that the current group doesn't already exist.
45
+ found_group = false
46
+ existing_group_id = nil
47
+ response = post_with_attempts( "GetTransactionalGroupID", options )
48
+ if response != false
49
+ existing_group_id = Nokogiri::XML.parse(response.body).xpath("*").first.content.split("\n")[2]
50
+ found_group = true
51
+ end
52
+
53
+ return existing_group_id
54
+ end
55
+
56
+ # Create a group and return the successfull value
57
+ # group_name: group name that the user wants to get the id for, should only contain alphanumeric ( and spaces )
58
+ # will be cleaned if an incorrect name is used
59
+ def create_group( group_name )
60
+ check_user_pass
61
+ # First we need to clean the group_name since jangosmtp only allows alphanumeric characters
62
+ group_name.tr!('^A-Za-z0-9 ', '')
63
+ options = {
64
+ 'Username' => @username,
65
+ 'Password' => @password,
66
+ 'GroupName' => group_name
67
+ }
68
+
69
+ response = post_with_attempts( 'AddTransactionalGroup', options )
70
+ if response != false
71
+ new_group_id = Nokogiri::XML.parse(response.body).xpath("*").first.content.split("\n")[2]
72
+ end
73
+ return new_group_id
74
+ end
75
+
76
+ # Send an email and get/create a requested group
77
+ # group_name: group name that the user wants the email to be applied to, should only contain alphanumeric
78
+ # ( and spaces ) will be cleaned if an incorrect name is used
79
+ # to_email: the single email address that the email will be sent to
80
+ # from_email: the email address that the email will be coming from
81
+ # from_name: the name that the email address will be coming from
82
+ # html: the html of the email message to be sent
83
+ #
84
+ # IMPORTANT: This function will attempt to get or create a group for each email that is sent. If you will
85
+ # be sending a lot of emails to the same group I would recommend you create the group first and use
86
+ # the send_email_with_group_id function since that takes a group_id and won't attempt to create the
87
+ # group with each email that is sent
88
+ def send_email( group_name, to_email, from_email, from_name, html )
89
+ check_user_pass
90
+ group_id = get_create_group( group_name )
91
+ unless group_id.nil?
92
+ return send_email_with_group_id( group_id, to_email, from_email, from_name, html )
93
+ end
94
+ end
95
+
96
+ # Send an email using a pre-existing group
97
+ # group_id: the id of the group that this email will be applied to
98
+ # to_email: the single email address that the email will be sent to
99
+ # from_email: the email address that the email will be coming from
100
+ # from_name: the name that the email address will be coming from
101
+ # html: the html of the email message to be sent
102
+ def send_email_with_group_id( group_id, to_email, from_email, from_name, html )
103
+ check_user_pass
104
+ # Send the email using Jango
105
+ options = {
106
+ 'Username' => @username,
107
+ 'Password' => @password,
108
+ 'FromEmail' => from_email,
109
+ 'FromName' => from_name,
110
+ 'ToEmailAddress' => to_email,
111
+ 'Subject' => 'SOCO Authorization Request',
112
+ 'MessagePlain' => 'auto-generate',
113
+ 'MessageHTML' => html,
114
+ 'Options' => 'OpenTrack=' + @open_tracking.to_s + ',ClickTrack=' + @click_tracking.to_s + ',TransactionalGroupID=' + group_id
115
+ }
116
+ return post_with_attempts( 'SendTransactionalEmail', options )
117
+ end
118
+
119
+ private
120
+ # Will verify that the username and password exist for each request
121
+ def check_user_pass
122
+ if @username.nil? || @password.nil?
123
+ raise 'Jangosmtp username and password are required'
124
+ end
125
+ end
126
+
127
+ # Will attempt to post to the jangosmtp action requested using the options hash passed in
128
+ def post_with_attempts( action, options )
129
+ agent = Mechanize.new
130
+ attempt = 0
131
+ response = false
132
+ # Try max_attempts times before skipping
133
+ while((attempt < max_attempts) && !response)
134
+ begin
135
+ response = agent.post( BASE_URL + action, options )
136
+ rescue StandardError => e
137
+ # If there was an error set success to false and try again in 3 seconds
138
+ response = false
139
+ attempt += 1
140
+ sleep 3
141
+ end
142
+ end
143
+ return response
144
+ end
145
+
146
+ # Get either the Rails logger or the a logger to STDOUT
147
+ def logger
148
+ if !Rails.nil?
149
+ return Rails.logger
150
+ else
151
+ return Logger.new( STDOUT )
152
+ end
153
+ end
154
+ end
155
+ end
@@ -0,0 +1,3 @@
1
+ module Jangosmtp
2
+ VERSION = "0.1.1"
3
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jangosmtp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Brennon Loveless
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mechanize
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: Library for interfacing with JangoSMTP
31
+ email:
32
+ - brennon@fritzandandre.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - LICENSE
40
+ - README.md
41
+ - Rakefile
42
+ - jangosmtp.gemspec
43
+ - lib/jangosmtp.rb
44
+ - lib/jangosmtp/version.rb
45
+ homepage: https://github.com/jbrennon/jangosmtp
46
+ licenses: []
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubyforge_project:
65
+ rubygems_version: 1.8.19
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Will encapsulate all the necessary api calls into an easy to use gem.
69
+ test_files: []