aws-ses icon indicating copy to clipboard operation
aws-ses copied to clipboard

Bcc doesn't work through rails 3

Open codev opened this issue 13 years ago • 26 comments

I'm trying to deliver mail through Rails 3 Action Mailer with:

mail :to => "[email protected]",
      :bcc => ['[email protected]', '[email protected]'],
      :subject => 'Blah'

The mail only gets sent to the :to address, the :bcc list is ignored. I believe the destinations are being reconstructed by Amazon as send_raw_email in lib/aws/ses/send_email.rb doesn't extract anything from the Mail object apart from the encoded message. It need something like this (but better, I just wrote this here and haven't tested it) to replace the current destinations code:

destinations = []
if args[:destinations]
  destinations.concat args[:destinations].to_a
elsif args[:to]
  destinations.concat args[:to].to_a
else
  destinations.concat mail.to.to_a
  destinations.concat mail.cc.to_a
  destinations.concat mail.bcc.to_a
end
add_array_to_hash!(package, 'Destinations', destinations) if destinations.length > 0

codev avatar Apr 23 '11 11:04 codev

https://github.com/codev/aws-ses/commit/4b087dfa34b38b819b01d673518b5fcad2a9fbeb

codev avatar May 02 '11 15:05 codev

Hi.

It's gonna be a great help for me if I could know whether this issue happens with rails2 or not. Do you have any information about it?

Since I'm not using aws-ses from rails and facing this issue, I want to know if downgrading some gems wil help.

i7a avatar May 11 '11 17:05 i7a

I'm haven't tested with rails 2 so I don't know I'm afraid. It uses a slightly different code path so might be ok.

codev avatar May 11 '11 18:05 codev

OK,I see. Thanks anyway.

If I could take a time I'll try it by myself.

i7a avatar May 12 '11 02:05 i7a

thank you!

my workaround: override the send_raw_email method the initializers

AWS::SES::SendEmail.class_eval do
  #TN: made bbc work with ses
  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) }
    package['Source'] = args[:from] if args[:from]
    package['Source'] = args[:source] if args[:source]
    
    # Extract the list of recipients based on arguments or mail headers
    destinations = []
    if args[:destinations]
      destinations.concat args[:destinations].to_a
    elsif args[:to]
      destinations.concat args[:to].to_a
    else
      destinations.concat mail.to.to_a
      destinations.concat mail.cc.to_a
      destinations.concat mail.bcc.to_a
    end
    add_array_to_hash!(package, 'Destinations', destinations) if destinations.length > 0
    
    request('SendRawEmail', package)
  end
  
  alias :deliver! :send_raw_email
  alias :deliver  :send_raw_email
    
end

TinNT avatar May 26 '11 12:05 TinNT

Is this issue resolved? I'm having the same issue...

benlieb avatar Aug 11 '11 20:08 benlieb

I'm use TinNT's monkey patch for now. Thanks!

benlieb avatar Aug 11 '11 20:08 benlieb

I tried using TinNT's monkey patch, but when the emails are delivered, the addresses sent in the Destinations are visible in the TO field. I added logging and verified the TO fields aren't populated in the raw message and only appear as part of the Destinations section of the package.

Any ideas?

curtp avatar Sep 05 '11 02:09 curtp

@curtp

I think this happens to me as well. I'm not sure how the ses api works. It might not support this feature. For now, I'm just looping through each recipient and sending individual mails out. Good luck.

benlieb avatar Sep 05 '11 02:09 benlieb

I found this post on the AWS SES forum which describes this. Supposedly they fixed it in the service.

https://forums.aws.amazon.com/thread.jspa?messageID=277021&#277021

curtp avatar Sep 05 '11 02:09 curtp

@pixelterra

Ugh.... that will test the sending limits on our app. We really need the BCC capability.

curtp avatar Sep 05 '11 02:09 curtp

+1 just discovered this bug today as well

marknadig avatar Oct 14 '11 23:10 marknadig

Just took a quick glance at the AWS SES docs...

It appears that the sendRawEmail API endpoint doesn't support BCC but the sendEmail one does. Is this true? Anyone notice this as well or am I just reading the docs incorrectly?

kvirani avatar Nov 04 '11 15:11 kvirani

+1 here too... discovered and had to roll back to old provider....

jeberly avatar Nov 04 '11 15:11 jeberly

+1 I think this is fairly important. Can we make this work or should we shelve SES decision until later? BCC is very important to us in our current project.

asanghi avatar Jan 27 '12 08:01 asanghi

+1 from me as well.

joshuapinter avatar Aug 10 '12 16:08 joshuapinter

Turns out, the official aws-sdk supports bcc. Setup was as simple as this gem, FYI.

http://docs.amazonwebservices.com/AWSRubySDK/latest/AWS/SimpleEmailService.html

Thanks @drewblas for helping us to this date, though!

kenn avatar Sep 22 '12 04:09 kenn

I'm facing the same issue as well. The monkey patch seems to be working fine, so thank you @TinNT

Is it possible to have the pull request applied?

constantine-nikolaou avatar Sep 25 '12 18:09 constantine-nikolaou

Like @kenn mentioned - ended up moving to the aws-sdk gem. Fixed the issue.

Quick port:

# Gemfile
# gem "aws-ses", "~> 0.4.4", :require => 'aws/ses' 
gem 'aws-sdk',                       '1.7.0'

# initializer
# ActionMailer::Base.add_delivery_method :ses, AWS::SES::Base, {access_key_id: ENV['SES_ACCESS_KEY_ID'], secret_access_key: ENV['SES_SECRET_ACCESS_KEY']}
AWS.config(access_key_id: ENV['SES_ACCESS_KEY_ID'], secret_access_key: ENV['SES_SECRET_ACCESS_KEY'])

# production.rb
# config.action_mailer.delivery_method = :ses
config.action_mailer.delivery_method = :amazon_ses

# Want to test in dev first?  add to development.rb
config.action_mailer.delivery_method = :amazon_ses
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true

Ship it. Hope it helps.

barmstrong avatar Nov 06 '12 02:11 barmstrong

Thanks @barmstrong. I just used your comment and I'm bcc'ing.

kayluhb avatar Nov 09 '12 16:11 kayluhb

This gem still doesn't support BCCing? I just tried today with ruby 1.9.3 and Rails 3.2.13 and it didn't work.

waynehoover avatar Jul 08 '13 19:07 waynehoover

This gem uses what is essentially the exact same code for send_raw_email as the aws-sdk gem for handling destinations:

https://github.com/drewblas/aws-ses/blob/master/lib/aws/ses/send_email.rb#L98

https://github.com/aws/aws-sdk-ruby/blob/master/lib/aws/simple_email_service.rb#L333

Some people have reported it working while others have said one works and the other doesn't. Unfortunately, I haven't yet gotten a detailed enough error report to be able to find the root cause. If anyone can capture the API call that both gems are making and show what the difference is that is causing BCC not to work, I'd be happy to make an adjustment.

drewblas avatar Jul 08 '13 19:07 drewblas

The gem still does not support BCC. Tried with ruby 1.9.3 p392 and Rails 3.2.12.

Shehbaz avatar Oct 09 '13 11:10 Shehbaz

I have used @barmstrong solution. aws-sdk works perfectly.

Shehbaz avatar Oct 09 '13 14:10 Shehbaz

I was having the same problem still in Rails 3.2.16 using Ruby 1.9.3. Using @barmstrong solution worked

rdetert avatar Jan 14 '14 08:01 rdetert

thanks @barmstrong :thumbsup:

guyisra avatar Apr 29 '14 11:04 guyisra