Skip to content

Commit

Permalink
Added code to check for existence of html or text body when attachmen…
Browse files Browse the repository at this point in the history
…ts are included [#29]
  • Loading branch information
mdesjardins committed Jan 11, 2012
1 parent 1b0faa9 commit 67002a5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lib/aws/ses/send_email.rb
Expand Up @@ -96,8 +96,9 @@ def send_email(options = {})
# @option args [String] :to alias for :destinations
# @return [Response]
def send_raw_email(mail, args = {})
message = mail.is_a?(Hash) ? Mail.new(mail).to_s : mail.to_s
package = { 'RawMessage.Data' => Base64::encode64(message) }
message = mail.is_a?(Hash) ? Mail.new(mail) : mail
raise ArgumentError, "Attachment provided without message body" if message.has_attachments? && message.text_part.nil? && message.html_part.nil?
package = { 'RawMessage.Data' => Base64::encode64(message.to_s) }
package['Source'] = args[:from] if args[:from]
package['Source'] = args[:source] if args[:source]
if args[:destinations]
Expand Down
6 changes: 6 additions & 0 deletions test/helper.rb
Expand Up @@ -54,3 +54,9 @@ def generate_base
Base.new(:access_key_id=>'123', :secret_access_key=>'abc')
end
end

# Deals w/ http://github.com/thoughtbot/shoulda/issues/issue/117, see
# http://stackoverflow.com/questions/3657972/nameerror-uninitialized-constant-testunitassertionfailederror-when-upgradin
unless defined?(Test::Unit::AssertionFailedError)
Test::Unit::AssertionFailedError = ActiveSupport::TestCase::Assertion
end
20 changes: 19 additions & 1 deletion test/send_email_test.rb
Expand Up @@ -46,6 +46,24 @@ class SendEmailTest < Test::Unit::TestCase
assert_equal 'xyz-123', result.request_id
end

should 'throw ArgumentException when attachment supplied without a body' do
#mock_connection(@base, :body => %{
# <SendRawEmailResponse xmlns="http://ses.amazonaws.com/doc/2010-12-01/">
# <SendRawEmailResult>
# <MessageId>abc-123</MessageId>
# </SendRawEmailResult>
# <ResponseMetadata>
# <RequestId>xyz-123</RequestId>
# </ResponseMetadata>
# </SendRawEmailResponse>
#})
message = Mail.new({:from => 'jon@example.com', :to => 'dave@example.com', :subject => 'Subject1'})
message.attachments['foo'] = { :mime_type => 'application/csv', :content => '1,2,3' }
assert_raise ArgumentError do
result = @base.send_raw_email message
end
end

should 'send a raw e-mail with a hash object' do
mock_connection(@base, :body => %{
<SendRawEmailResponse xmlns="http://ses.amazonaws.com/doc/2010-12-01/">
Expand All @@ -64,4 +82,4 @@ class SendEmailTest < Test::Unit::TestCase
assert_equal 'xyz-123', result.request_id
end
end
end
end

0 comments on commit 67002a5

Please sign in to comment.