sendgrid_smtpapi 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/README.rdoc +111 -0
- data/Rakefile +5 -0
- data/lib/send_grid.rb +30 -0
- data/lib/send_grid/.DS_Store +0 -0
- data/lib/send_grid/api_header.rb +37 -0
- data/lib/send_grid/mail_interceptor.rb +14 -0
- data/lib/send_grid/version.rb +3 -0
- data/lib/sendgrid_smtpapi.rb +8 -0
- data/sendgrid_smtpapi.gemspec +24 -0
- data/spec/api_header_spec.rb +40 -0
- data/spec/fixtures/mailers/mailer.rb +19 -0
- data/spec/mailer_spec.rb +50 -0
- data/spec/spec_helper.rb +7 -0
- data/spec/version_spec.rb +7 -0
- metadata +110 -0
data/.rspec
ADDED
data/Gemfile
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
= SendGrid SMTP API gem for Rails {<img src="https://secure.travis-ci.org/kylejginavan/sendgrid_smtpapi.png"/>}[http://travis-ci.org/kylejginavan/sendgrid_smtpapi]
|
2
|
+
|
3
|
+
SendGrid SMTP API gem provides ActionMailer::Base extensions to use SendGrid API features in you emails.
|
4
|
+
It extends ActionMailer with next methods:
|
5
|
+
|
6
|
+
substitute(patters_string, array_of_substitunion_strings)
|
7
|
+
uniq_args(hash_of_unique_args)
|
8
|
+
category(category_string)
|
9
|
+
open_tracking(enabled = true)
|
10
|
+
add_filter_setting(filter_name, setting_name, value)
|
11
|
+
standard_smtp(enabled = false)
|
12
|
+
|
13
|
+
== Rails 3 configuration
|
14
|
+
|
15
|
+
In your Gemfile:
|
16
|
+
|
17
|
+
gem 'sendgrid_smtpapi'
|
18
|
+
|
19
|
+
In your config/environment.rb:
|
20
|
+
|
21
|
+
ActionMailer::Base.register_interceptor(SendGridSmtpApi::MailInterceptor)
|
22
|
+
|
23
|
+
ActionMailer::Base.smtp_settings = {
|
24
|
+
:address => 'smtp.sendgrid.net',
|
25
|
+
:port => '25',
|
26
|
+
:domain => 'example.com',
|
27
|
+
:authentication => :plain,
|
28
|
+
:user_name => 'login@example.com',
|
29
|
+
:password => 'your password'
|
30
|
+
}
|
31
|
+
|
32
|
+
== Usage examples
|
33
|
+
|
34
|
+
=== Adding multiple recipients:
|
35
|
+
|
36
|
+
class Mailer < ActionMailer::Base
|
37
|
+
default :from => 'no-reply@example.com',
|
38
|
+
:subject => 'An email sent via SendGridSmtpApi'
|
39
|
+
|
40
|
+
def email_with_multiple_recipients
|
41
|
+
mail :to => %w(email1@email.com email2@email.com)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
=== Adding substitution vars
|
46
|
+
|
47
|
+
Mailer class definition:
|
48
|
+
|
49
|
+
class Mailer < ActionMailer::Base
|
50
|
+
default :from => 'no-reply@example.com',
|
51
|
+
:subject => 'An email sent via SendGridSmtpApi with substitutions'
|
52
|
+
|
53
|
+
def email_with_substitutions
|
54
|
+
substitute '-user_name-', %w(User1 User2)
|
55
|
+
|
56
|
+
mail :to => %w(email1@email.com email2@email.com), :body => "Hello, -user_name-!"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
=== Adding category
|
61
|
+
|
62
|
+
Mailer class definition:
|
63
|
+
|
64
|
+
class Mailer < ActionMailer::Base
|
65
|
+
default :from => 'no-reply@example.com',
|
66
|
+
:subject => 'An email sent via SendGridSmtpApi with substitutions'
|
67
|
+
|
68
|
+
def email_with_category
|
69
|
+
category 'SendGridSmtpApiRocks'
|
70
|
+
mail :to => 'email1@email.com'
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
== Apps (formerly called Filters)
|
75
|
+
|
76
|
+
Apps can be applied to any of your email messages and can be configured through SendGridSmtpApi gem.
|
77
|
+
|
78
|
+
=== Open Tracking
|
79
|
+
|
80
|
+
Add an invisible image at the end of the email to track e-mail opens. If the email recipient has images enabled on the email client, a request to server for the invisible image is executed and an open is logged.
|
81
|
+
|
82
|
+
class Mailer < ActionMailer::Base
|
83
|
+
default :from => 'no-reply@example.com',
|
84
|
+
:subject => 'An email sent via SendGridSmtpApi'
|
85
|
+
|
86
|
+
def email_with_open_tracking_enabled
|
87
|
+
open_tracking true
|
88
|
+
mail :to => 'email@email.com'
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
|
93
|
+
=== Use Standard SMTP
|
94
|
+
|
95
|
+
if you want to customize the header for sendgrids api but you need keep standard SMTP see below:
|
96
|
+
|
97
|
+
class Mailer < ActionMailer::Base
|
98
|
+
default :from => 'no-reply@example.com',
|
99
|
+
:subject => 'An email sent via SendGridSmtpApi'
|
100
|
+
|
101
|
+
def email_standard_smtp
|
102
|
+
standard_smtp true
|
103
|
+
open_tracking true
|
104
|
+
mail :to => 'email@email.com'
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
== CONTRIBUTORS:
|
109
|
+
|
110
|
+
* Kyle J. Ginavan.
|
111
|
+
* Mauro Torres - http://github.com/chebyte
|
data/Rakefile
ADDED
data/lib/send_grid.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
module SendGridSmtpApi
|
2
|
+
autoload :ApiHeader, 'send_grid/api_header'
|
3
|
+
autoload :MailInterceptor, 'send_grid/mail_interceptor'
|
4
|
+
autoload :VERSION, 'send_grid/version'
|
5
|
+
|
6
|
+
def self.included(base)
|
7
|
+
base.class_eval do
|
8
|
+
include InstanceMethods
|
9
|
+
delegate :substitute, :uniq_args, :category, :add_filter_setting, :standard_smtp, :to => :sendgrid_header
|
10
|
+
alias_method_chain :mail, :sendgrid
|
11
|
+
alias_method :sendgrid_header, :send_grid_header
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
module InstanceMethods
|
16
|
+
def send_grid_header
|
17
|
+
@send_grid_header ||= SendGridSmtpApi::ApiHeader.new
|
18
|
+
end
|
19
|
+
|
20
|
+
def mail_with_sendgrid(headers={}, &block)
|
21
|
+
mail_without_sendgrid(headers, &block).tap do |message|
|
22
|
+
message.instance_variable_set :@sendgrid_header, sendgrid_header
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def open_tracking(enabled = true)
|
27
|
+
add_filter_setting(:opentrack, :enabled, enabled ? 1 : 0) unless enabled.nil?
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
Binary file
|
@@ -0,0 +1,37 @@
|
|
1
|
+
class SendGridSmtpApi::ApiHeader
|
2
|
+
attr_reader :data
|
3
|
+
|
4
|
+
def initialize
|
5
|
+
@data = Hash.new { |h,k| h[k] = Hash.new(&h.default_proc) }
|
6
|
+
end
|
7
|
+
|
8
|
+
def add_recipients(recipients)
|
9
|
+
@data[:to] = [] unless @data[:to].instance_of?(Array)
|
10
|
+
@data[:to] |= Array.wrap(recipients)
|
11
|
+
end
|
12
|
+
|
13
|
+
def substitute(var, val)
|
14
|
+
@data[:sub][var] = Array.wrap(val)
|
15
|
+
end
|
16
|
+
|
17
|
+
def uniq_args(val)
|
18
|
+
@data[:unique_args] = val if val.instance_of?(Hash)
|
19
|
+
end
|
20
|
+
|
21
|
+
def category(cat)
|
22
|
+
@data[:category] = cat
|
23
|
+
end
|
24
|
+
|
25
|
+
def add_filter_setting(fltr, setting, val)
|
26
|
+
@data[:filters][fltr][:settings][setting] = val
|
27
|
+
end
|
28
|
+
|
29
|
+
def to_json
|
30
|
+
@data.to_json
|
31
|
+
end
|
32
|
+
|
33
|
+
def standard_smtp(enabled = false)
|
34
|
+
@standard_smtp = enabled
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module SendGridSmtpApi
|
2
|
+
class MailInterceptor
|
3
|
+
def self.delivering_email(mail)
|
4
|
+
sendgrid_header = mail.instance_variable_get(:@sendgrid_header)
|
5
|
+
standard_smtp = sendgrid_header.instance_variable_get(:@standard_smtp)
|
6
|
+
|
7
|
+
unless standard_smtp
|
8
|
+
sendgrid_header.add_recipients(mail.to)
|
9
|
+
mail.header['to'] = 'dummy@email.com'
|
10
|
+
end
|
11
|
+
mail.header['X-SMTPAPI'] = sendgrid_header.to_json if sendgrid_header.data.present?
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "send_grid/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "sendgrid_smtpapi"
|
7
|
+
s.version = SendGridSmtpApi::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["kylejginavan", "chebyte"]
|
10
|
+
s.email = ["kylejginavan@gmail.com", "maurotorres@gmail.com"]
|
11
|
+
s.homepage = "https://github.com/kylejginavan/sendgrid_smtpapi"
|
12
|
+
s.summary = %q{SendGrid SMTP API gem fo Rails 3}
|
13
|
+
s.description = %q{Gem to extend ActionMailer with SendGrid SMTP API support}
|
14
|
+
|
15
|
+
s.rubyforge_project = "sendgrid_smtpapi"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
s.add_development_dependency "rspec", "~> 2.5.0"
|
22
|
+
s.add_dependency "actionmailer", ">= 3.0.0"
|
23
|
+
s.add_dependency "activesupport", ">= 2.1.0"
|
24
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SendGridSmtpApi::ApiHeader do
|
4
|
+
let(:header) { SendGridSmtpApi::ApiHeader.new }
|
5
|
+
describe "#to_json" do
|
6
|
+
it "returns valid json if no data was set" do
|
7
|
+
header.to_json.should eql "{}"
|
8
|
+
end
|
9
|
+
|
10
|
+
it "contains 1 recipient (as array)" do
|
11
|
+
header.add_recipients 'email@email.com'
|
12
|
+
header.to_json.should eql '{"to":["email@email.com"]}'
|
13
|
+
end
|
14
|
+
|
15
|
+
it "contaions an array of recipients" do
|
16
|
+
header.add_recipients %w(email1@email.com email2@email.com)
|
17
|
+
header.to_json.should eql '{"to":["email1@email.com","email2@email.com"]}'
|
18
|
+
end
|
19
|
+
|
20
|
+
it "contains substitution" do
|
21
|
+
header.substitute :var1, 'Hello'
|
22
|
+
header.to_json.should eql '{"sub":{"var1":["Hello"]}}'
|
23
|
+
end
|
24
|
+
|
25
|
+
it "contains uniq args" do
|
26
|
+
header.uniq_args :arg1 => 'val1'
|
27
|
+
header.to_json.should eql '{"unique_args":{"arg1":"val1"}}'
|
28
|
+
end
|
29
|
+
|
30
|
+
it "contains category" do
|
31
|
+
header.category 'category_name'
|
32
|
+
header.to_json.should eql '{"category":"category_name"}'
|
33
|
+
end
|
34
|
+
|
35
|
+
it "contains filter settings" do
|
36
|
+
header.add_filter_setting :filter1, :setting1, 'val1'
|
37
|
+
header.to_json.should eql '{"filters":{"filter1":{"settings":{"setting1":"val1"}}}}'
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class Mailer < ActionMailer::Base
|
2
|
+
default :from => 'no-reply@sendgrid.com',
|
3
|
+
:subject => 'Test email'
|
4
|
+
|
5
|
+
def email_with_multiple_recipients(recipients)
|
6
|
+
mail :to => recipients, :body => "Hello!"
|
7
|
+
end
|
8
|
+
|
9
|
+
def email_open_tracking(opentrack_enabled = true)
|
10
|
+
open_tracking opentrack_enabled
|
11
|
+
mail :to => 'email@email.com', :body => 'Hello!'
|
12
|
+
end
|
13
|
+
|
14
|
+
def email_standard_smtp(enabled = false)
|
15
|
+
standard_smtp enabled
|
16
|
+
mail :to => 'email@email.com', :body => 'Hello!'
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
data/spec/mailer_spec.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mailer do
|
4
|
+
describe 'email with multiple recipients' do
|
5
|
+
it 'set correct recipients in X-SMTAPI header' do
|
6
|
+
Mailer.email_with_multiple_recipients(%w(em1@email.com em2@email.com)).deliver.header.to_s.
|
7
|
+
should include('X-SMTPAPI: {"to":["em1@email.com","em2@email.com"]}')
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'removes original TO header part' do
|
11
|
+
Mailer.email_with_multiple_recipients(%w(em1@email.com em2@email.com)).deliver.header.to_s.
|
12
|
+
should_not include("To: em1@email.com")
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe '#open_tracking' do
|
17
|
+
it 'set correct open tracking enabled X-SMTAPI header' do
|
18
|
+
Mailer.email_open_tracking.deliver.header.to_s.
|
19
|
+
should include('"filters":{"opentrack":{"settings":{"enabled":1}}}')
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'set correct open tracking disabled X-SMTAPI header' do
|
23
|
+
Mailer.email_open_tracking(false).deliver.header.to_s.
|
24
|
+
should include('"filters":{"opentrack":{"settings":{"enabled":0}}}')
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'set correct open tracking nil X-SMTAPI header' do
|
28
|
+
Mailer.email_open_tracking(nil).deliver.header.to_s.
|
29
|
+
should_not include('"filters":{"opentrack')
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe '#standard_smtp' do
|
34
|
+
it 'enable standard smpt to' do
|
35
|
+
Mailer.email_standard_smtp(true).deliver.header.to_s.
|
36
|
+
should include('To: email@email.com')
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'disable standard smtp to' do
|
40
|
+
Mailer.email_standard_smtp(false).deliver.header.to_s.
|
41
|
+
should include('To: dummy@email.com')
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'disable by default standard smtp to' do
|
45
|
+
Mailer.email_standard_smtp.deliver.header.to_s.
|
46
|
+
should include('To: dummy@email.com')
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
require 'sendgrid_smtpapi'
|
2
|
+
|
3
|
+
ActionMailer::Base.register_interceptor(SendGridSmtpApi::MailInterceptor)
|
4
|
+
ActionMailer::Base.delivery_method = :test
|
5
|
+
ActionMailer::Base.prepend_view_path File.join(File.dirname(__FILE__), "fixtures", "views")
|
6
|
+
|
7
|
+
Dir["#{File.dirname(__FILE__)}/fixtures/mailers/*.rb"].each { |f| require f }
|
metadata
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sendgrid_smtpapi
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- kylejginavan
|
9
|
+
- chebyte
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
|
14
|
+
date: 2012-12-17 00:00:00 Z
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: rspec
|
18
|
+
prerelease: false
|
19
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
|
+
none: false
|
21
|
+
requirements:
|
22
|
+
- - ~>
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: 2.5.0
|
25
|
+
type: :development
|
26
|
+
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: actionmailer
|
29
|
+
prerelease: false
|
30
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: 3.0.0
|
36
|
+
type: :runtime
|
37
|
+
version_requirements: *id002
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: activesupport
|
40
|
+
prerelease: false
|
41
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 2.1.0
|
47
|
+
type: :runtime
|
48
|
+
version_requirements: *id003
|
49
|
+
description: Gem to extend ActionMailer with SendGrid SMTP API support
|
50
|
+
email:
|
51
|
+
- kylejginavan@gmail.com
|
52
|
+
- maurotorres@gmail.com
|
53
|
+
executables: []
|
54
|
+
|
55
|
+
extensions: []
|
56
|
+
|
57
|
+
extra_rdoc_files: []
|
58
|
+
|
59
|
+
files:
|
60
|
+
- .gitignore
|
61
|
+
- .rspec
|
62
|
+
- .travis.yml
|
63
|
+
- Gemfile
|
64
|
+
- README.rdoc
|
65
|
+
- Rakefile
|
66
|
+
- lib/send_grid.rb
|
67
|
+
- lib/send_grid/.DS_Store
|
68
|
+
- lib/send_grid/api_header.rb
|
69
|
+
- lib/send_grid/mail_interceptor.rb
|
70
|
+
- lib/send_grid/version.rb
|
71
|
+
- lib/sendgrid_smtpapi.rb
|
72
|
+
- sendgrid_smtpapi.gemspec
|
73
|
+
- spec/api_header_spec.rb
|
74
|
+
- spec/fixtures/mailers/mailer.rb
|
75
|
+
- spec/mailer_spec.rb
|
76
|
+
- spec/spec_helper.rb
|
77
|
+
- spec/version_spec.rb
|
78
|
+
homepage: https://github.com/kylejginavan/sendgrid_smtpapi
|
79
|
+
licenses: []
|
80
|
+
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: "0"
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
+
none: false
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: "0"
|
98
|
+
requirements: []
|
99
|
+
|
100
|
+
rubyforge_project: sendgrid_smtpapi
|
101
|
+
rubygems_version: 1.8.15
|
102
|
+
signing_key:
|
103
|
+
specification_version: 3
|
104
|
+
summary: SendGrid SMTP API gem fo Rails 3
|
105
|
+
test_files:
|
106
|
+
- spec/api_header_spec.rb
|
107
|
+
- spec/fixtures/mailers/mailer.rb
|
108
|
+
- spec/mailer_spec.rb
|
109
|
+
- spec/spec_helper.rb
|
110
|
+
- spec/version_spec.rb
|