Skip to content

Commit

Permalink
Testing broken attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkingserious committed Sep 17, 2015
1 parent de393aa commit 7afb94b
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .env_sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SENDGRID_USERNAME=your_sendgrid_username
SENDGRID_PASSWORD=your_sendgrid_password
SENDGRID_APIKEY=your_sendgrid_apikey
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
/test/tmp/
/test/version_tmp/
/tmp/
.env
test.rb

## Specific to RubyMotion:
.dat*
Expand Down
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
gem 'dotenv-rails'
gem 'smtpapi'
gem 'rest-client'
source 'https://rubygems.org'
gemspec
28 changes: 28 additions & 0 deletions example.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require_relative "lib/sendgrid_ruby.rb"

require 'dotenv'
Dotenv.load

sendgrid_username = ENV["SENDGRID_USERNAME"]
sendgrid_password = ENV["SENDGRID_PASSWORD"]
sendgrid_apikey = ENV["SENDGRID_APIKEY"]

# client = SendGrid::Client.new(api_user: sendgrid_username, api_key: sendgrid_password)

client = SendGrid::Client.new do |c|
c.api_user = sendgrid_username
c.api_key = sendgrid_password
end

mail = SendGrid::Mail.new do |m|
m.to = '[email protected]'
m.from = '[email protected]'
m.subject = 'Hello world!'
m.text = 'I heard you like pineapple.'
end
mail.add_attachment('/tmp/report.pdf', 'july_report.pdf')
result = client.send(mail)
puts result.code
puts result.body

# puts client.send(SendGrid::Mail.new(to: '[email protected]', from: '[email protected]', subject: 'Hello world!', text: 'Hi there, testing from Ruby!', html: '<b>Hi there, testing from Ruby!</b>'))
3 changes: 2 additions & 1 deletion lib/sendgrid/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ def send(mail)
end

req.body = payload
puts "final payload= " + req.body.to_s
end

fail SendGrid::Exception, res.body if raise_exceptions? && res.status != 200

SendGrid::Response.new(code: res.status, headers: res.headers, body: res.body)
end

Expand Down
10 changes: 7 additions & 3 deletions lib/sendgrid/mail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,22 @@ def to_h
:text => text,
:html => html,
:'x-smtpapi' => smtpapi.to_json,
:files => ({} unless attachments.empty?)
:files => ({":default"=>"0"} unless attachments.empty?)
# If I don't define a default value, I get a Nil error when
# in attachments.each do |file|
#:files => ({} unless attachments.empty?)
}.reject {|_, v| v.nil? || v.empty?}

payload.delete(:'x-smtpapi') if payload[:'x-smtpapi'] == '{}'

payload[:to] = payload[:from] unless payload[:to].nil? && smtpapi.to.empty?
payload[:to] = payload[:from] unless (not payload[:to].nil?) && smtpapi.to.empty?

return payload if attachments.empty?

attachments.each do |file|
payload[:files][file[:name]] = file[:file]
end
payload[:files].delete(":default")
payload
end
# rubocop:enable Style/HashSyntax
Expand Down

0 comments on commit 7afb94b

Please sign in to comment.