Skip to content

Commit

Permalink
Implement support for the Mail module.
Browse files Browse the repository at this point in the history
  • Loading branch information
Elad Meidar authored and freerobby committed Nov 1, 2011
1 parent 9fe9553 commit 67635be
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/sendgrid_toolkit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require 'sendgrid_toolkit/spam_reports'
require 'sendgrid_toolkit/bounces'
require 'sendgrid_toolkit/invalid_emails'
require 'sendgrid_toolkit/mail'

module SendgridToolkit
BASE_URI = "sendgrid.com/api"
Expand Down
11 changes: 11 additions & 0 deletions lib/sendgrid_toolkit/mail.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module SendgridToolkit
class Mail < AbstractSendgridClient
def send_mail(options = {})
options.assert_valid_keys :to, :api_headers, :subject, :from, :text, :html, :reply_to, :from_name, :to_name, :date, :headers
options["X-SMTPAPI"] = options[:api_headers]
response = api_post('mail', 'send', options)
raise(SendEmailError, "SendMail API refused to send email: #{response["errors"].inspect}") if response["message"] == "error"
true
end
end
end
1 change: 1 addition & 0 deletions lib/sendgrid_toolkit/sendgrid_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class NoAPIKeySpecified < StandardError; end
class NoAPIUserSpecified < StandardError; end
class UnsubscribeEmailAlreadyExists < StandardError; end
class UnsubscribeEmailDoesNotExist < StandardError; end
class SendEmailError < StandardError; end
class EmailDoesNotExist < StandardError; end
class SendgridServerError < StandardError; end
class APIError < StandardError; end
Expand Down
19 changes: 19 additions & 0 deletions spec/lib/sendgrid_toolkit/mail_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require File.expand_path("#{File.dirname(__FILE__)}/../../helper")

describe SendgridToolkit::Mail do
before do
FakeWeb.clean_registry
@obj = SendgridToolkit::Mail.new("fakeuser", "fakepass")
end

describe "#send" do
it "returns an error when some attributes are missing" do
FakeWeb.register_uri(:post, %r|https://sendgrid\.com/api/mail\.send\.json\?|, :body => '[{"email":"user@domain.com"},{"email":"user2@domain2.com"},{"email":"user3@domain2.com"}]')
emails = @obj.send_mail(:to => "elad@fiverr.com", :from => "testing@fiverr.com", :subject => "Hi there!", :text => "testing body", :x_smtpapi => {:category => "Testing", :to => ["elad@fiverr.com"]})
emails[0]['email'].should == "user@domain.com"
emails[1]['email'].should == "user2@domain2.com"
emails[2]['email'].should == "user3@domain2.com"
end
end
end
end

0 comments on commit 67635be

Please sign in to comment.