Rails应用中Sendgrid BCC功能将停用,当前如何使用该功能?
Hey there,
Based on the notification you got from SendGrid, here's a clear breakdown of what you can do to keep using BCC until its full removal on June 22, 2020:
Current BCC Availability Status
SendGrid has disabled BCC access via their UI, but the feature is still usable only through API calls until the cutoff date. You'll need to adjust your Rails app's email-sending logic to leverage either the SendGrid API directly or ensure your existing Action Mailer setup works with their SMTP endpoint correctly.
Option 1: Stick with Action Mailer + SendGrid SMTP
If you're already using Action Mailer with SendGrid as your SMTP provider, you can keep using BCC just like you always have—SMTP supports BCC, and SendGrid will honor this until the cutoff date.
- First, double-check your SMTP settings in
config/environments/production.rb(or your target environment):
config.action_mailer.delivery_method = :smtp config.action_mailer.smtp_settings = { address: 'smtp.sendgrid.net', port: 587, domain: 'your-app-domain.com', user_name: 'apikey', password: ENV['SENDGRID_API_KEY'], authentication: :plain, enable_starttls_auto: true }
- In your mailer class, add the
bccparameter to your mail call as usual:
class UpdateMailer < ApplicationMailer def critical_alert mail( to: 'primary-user@example.com', bcc: 'admin-alerts@example.com', # This will work via SMTP subject: 'Urgent System Update' ) end end
Option 2: Use the SendGrid Ruby API Client Directly
If you want more control over your email sending, you can use SendGrid's official Ruby API client to build and send emails with BCC recipients.
- Install the
sendgrid-rubygem if you haven't already:
gem install sendgrid-ruby # Or add to your Gemfile and run bundle install gem 'sendgrid-ruby', require: 'sendgrid'
- Build and send your email with a BCC recipient using the API:
require 'sendgrid-ruby' include SendGrid # Initialize the API client with your key sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) # Define email components from_email = Email.new(email: 'notifications@your-app.com') to_email = Email.new(email: 'user@example.com') bcc_email = Email.new(email: 'compliance@your-app.com') subject = 'Monthly Compliance Update' content = Content.new(type: 'text/plain', value: 'This update includes a BCC to our compliance team via SendGrid API.') # Assemble the mail object and attach the BCC recipient mail = Mail.new(from_email, subject, to_email, content) mail.bcc = bcc_email # Send the email response = sg.client.mail._('send').post(request_body: mail.to_json) # Optional: Verify the send status puts "Send status code: #{response.status_code}"
Critical Reminder
Don't forget to plan for the June 22, 2020 cutoff date. After that, SendGrid will fully remove the BCC feature. For long-term alternatives, consider:
- Sending separate email copies to your "BCC" recipients directly from your Rails app
- Using SendGrid's list management or batch sending features to reach multiple recipients without relying on BCC
- Building custom logic in your app to handle hidden recipient needs
内容的提问来源于stack exchange,提问作者webster

