paveltyk / sendgrid-rails

SendGrid extensions to Rails 3 ActionMailer::Base

Home Page:http://paveltyk.github.com/sendgrid-rails/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Sendgrid BCC/CC not working correctly

zeeshangulzar opened this issue · comments

I am using Sendgrid in my Rails application, when I add emails in TO and BCC/CC, then after sending the email through my application, TO email is working fine and I got the email, but when I logged in with the same BCC/CC email in my account(e.g. Gmail), then there is no TO email details and my BCC/CC email shifted in the TO field, so that my email (that means BCC/CC email) is showing into the TO field.

As per the default process, for the BCC email user, TO email details should be in the TO field and my email (BCC email) should show into the BCC field section. But it is not working like that.

Please let me know, how can I achieve this process, so that based on the default functionality, my BCC email will display into the BCC email field instead of TO field.

Following are my config in /initialzers/mail.rb

ActionMailer::Base.register_observer(TrackedEmailEvents)

ActionMailer::Base.smtp_settings = {
  address: 'smtp.sendgrid.net',
  port: 587,
  domain: 'syncta.com',
  authentication: :plain,
  enable_starttls_auto: true,
  user_name: ENV['SENDGRID_USERNAME'],
  password: ENV['SENDGRID_SECRET']
}

SendGrid.configure do |config|
  config.dummy_recipient = 'donotreply@syncta.com'
end

If i remove intercept it will work as expected, but i need intercepter as I m using X-SMTPAPI' header in observer

I also tried to move my config into config/environment as mentioned here but no success.
Ruby Version: ruby 2.5.1
Rails Version: 5.1.6

Isn't the behavior you've described is right? Maybe share an example with expected and actual results, please?

I am sharing the cc case. I am sending email at following
to: xeeshansubs@gmail.com
cc xeeshangulzar@gmail.com

For the cc email address (xeeshangulzar@gmail.com), following is the details i received at gmail.
1- Current CC
to & cc fields are same which is incorrect. cc user should be able to see the to address.
Here is the expected behaviour for cc.
3- Expected CC
to field should be populated with xeeshansubs@gmail.com

I got the expected behaviour by removing the ActionMailer::Base.register_interceptor(SendGrid::MailInterceptor) line from mail initializer.
But we need to add interceptor in our code to set X-SMTPAPI header used in webhooks
https://github.com/paveltyk/sendgrid-rails/blob/master/lib/send_grid/mail_interceptor.rb#L8

Here is the sample code used.

    sample = Sample.find(id)
    @body = 'sample text for body'
    company = sample.company
    mail(subject: 'Sample Subject',
         to: ['xeeshansubs@gmail.com'],
         **cc_or_bcc(company),
         from: 'contact@example.com',
         reply_to: 'info@example.com') do |format|
      format.html { render 'shared/emails/body' }
    end
  end

  def cc_or_bcc(company)
    { (company.to_cc? ? :cc : :bcc) => 'xeeshangulzar@gmail.com' }
  end```

@zeeshangulzar I see. It's a design issue. Take a look:

sendgrid_header.add_recipients(mail.cc)

Unfortunately, I do not have time to fix this now. I would highly appreciate a PR to fix this issue, though.

Thanks @paveltyk I will work on it in some free time.

Is there any work-around for this one?