From f23a3fa69d7146a5e18da9eb3148b810b3c91d1d Mon Sep 17 00:00:00 2001 From: Hengjie Date: Wed, 5 Sep 2018 16:58:33 +1200 Subject: [PATCH 1/2] Retain formatted email address in sendouts The interceptor calls 'mail.to' to retrieve the email address. However, the mail gem will only return the address, and not the full formatted string. We can call `.addrs` (https://github.com/mikel/mail/blob/master/lib/mail/fields/common_address_field.rb#L47) but there's no easy way to access it. By removing all of this, we don't mangle the to/cc/bcc fields, and instead pass it straight through to Sendgrid which will handle it correctly. This also fixes a bug where bcc and cc aren't treated like they say, and are converted to 'To' addresses. This also removes the 'dummy_recipient' which has shown to be flagged as spam by GMail. --- lib/send_grid/mail_interceptor.rb | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/send_grid/mail_interceptor.rb b/lib/send_grid/mail_interceptor.rb index 9a3aa79..acbecda 100644 --- a/lib/send_grid/mail_interceptor.rb +++ b/lib/send_grid/mail_interceptor.rb @@ -2,11 +2,7 @@ module SendGrid class MailInterceptor def self.delivering_email(mail) sendgrid_header = mail.instance_variable_get(:@sendgrid_header) - sendgrid_header.add_recipients(mail.to) - sendgrid_header.add_recipients(mail.cc) - sendgrid_header.add_recipients(mail.bcc) mail.header['X-SMTPAPI'] = sendgrid_header.to_json if sendgrid_header.data.present? - mail.header['to'] = SendGrid.config.dummy_recipient end end end From d32ebb9c183ea44aea40394997f954b46b938c96 Mon Sep 17 00:00:00 2001 From: PaulApino Date: Thu, 28 Aug 2025 16:56:19 +1200 Subject: [PATCH 2/2] Rename TopLevel Gem Module to SendGridRails - module name conflicts with the gem sendgrid-ruby - this is the smaller gem and requires less changes --- README.rdoc | 6 +++--- lib/send_grid.rb | 6 +++--- lib/send_grid/api_header.rb | 2 +- lib/send_grid/config.rb | 2 +- lib/send_grid/mail_interceptor.rb | 2 +- lib/send_grid/version.rb | 5 ++--- lib/sendgrid-rails.rb | 2 +- send_grid.gemspec | 2 +- spec/api_header_spec.rb | 4 ++-- spec/mailer_spec.rb | 2 +- spec/spec_helper.rb | 2 +- spec/version_spec.rb | 4 ++-- 12 files changed, 19 insertions(+), 20 deletions(-) diff --git a/README.rdoc b/README.rdoc index e7806d0..9013105 100644 --- a/README.rdoc +++ b/README.rdoc @@ -17,7 +17,7 @@ In your Gemfile: In config/initializers/mail.rb: - ActionMailer::Base.register_interceptor(SendGrid::MailInterceptor) + ActionMailer::Base.register_interceptor(SendGridRails::MailInterceptor) ActionMailer::Base.smtp_settings = { :address => 'smtp.sendgrid.net', @@ -30,7 +30,7 @@ In config/initializers/mail.rb: If you use Heroku, here what the mailer initializer may look like: - ActionMailer::Base.register_interceptor(SendGrid::MailInterceptor) + ActionMailer::Base.register_interceptor(SendGridRails::MailInterceptor) if ENV['SENDGRID_USERNAME'] && ENV['SENDGRID_PASSWORD'] ActionMailer::Base.smtp_settings = { @@ -53,7 +53,7 @@ By default set to 'dummy@email.com' In config/initializers/mail.rb: - SendGrid.configure do |config| + SendGridRails.configure do |config| config.dummy_recipient = 'noreply@example.com' end diff --git a/lib/send_grid.rb b/lib/send_grid.rb index 349621f..38857c3 100644 --- a/lib/send_grid.rb +++ b/lib/send_grid.rb @@ -1,4 +1,4 @@ -module SendGrid +module SendGridRails autoload :ApiHeader, 'send_grid/api_header' autoload :MailInterceptor, 'send_grid/mail_interceptor' autoload :VERSION, 'send_grid/version' @@ -15,7 +15,7 @@ def self.included(base) module InstanceMethods def send_grid_header - @send_grid_header ||= SendGrid::ApiHeader.new + @send_grid_header ||= SendGridRails::ApiHeader.new end def mail(headers={}, &block) @@ -33,7 +33,7 @@ class << self attr_writer :config def config - @config ||= SendGrid::Config.new + @config ||= SendGridRails::Config.new end # Sendgrid.config will be default if block is not passed diff --git a/lib/send_grid/api_header.rb b/lib/send_grid/api_header.rb index e10d031..e7acb76 100644 --- a/lib/send_grid/api_header.rb +++ b/lib/send_grid/api_header.rb @@ -1,4 +1,4 @@ -class SendGrid::ApiHeader +class SendGridRails::ApiHeader attr_reader :data def initialize diff --git a/lib/send_grid/config.rb b/lib/send_grid/config.rb index 09aece9..0279d30 100644 --- a/lib/send_grid/config.rb +++ b/lib/send_grid/config.rb @@ -1,4 +1,4 @@ -class SendGrid::Config +class SendGridRails::Config attr_accessor :dummy_recipient def initialize diff --git a/lib/send_grid/mail_interceptor.rb b/lib/send_grid/mail_interceptor.rb index acbecda..9651205 100644 --- a/lib/send_grid/mail_interceptor.rb +++ b/lib/send_grid/mail_interceptor.rb @@ -1,4 +1,4 @@ -module SendGrid +module SendGridRails class MailInterceptor def self.delivering_email(mail) sendgrid_header = mail.instance_variable_get(:@sendgrid_header) diff --git a/lib/send_grid/version.rb b/lib/send_grid/version.rb index 7cfd272..c853db5 100644 --- a/lib/send_grid/version.rb +++ b/lib/send_grid/version.rb @@ -1,4 +1,3 @@ -module SendGrid - VERSION = "3.1.0" +module SendGridRails + VERSION = '3.1.0' end - diff --git a/lib/sendgrid-rails.rb b/lib/sendgrid-rails.rb index ad35574..572b44d 100644 --- a/lib/sendgrid-rails.rb +++ b/lib/sendgrid-rails.rb @@ -3,4 +3,4 @@ require 'active_support/core_ext' require 'action_mailer' -ActionMailer::Base.send :include, SendGrid +ActionMailer::Base.send :include, SendGridRails diff --git a/send_grid.gemspec b/send_grid.gemspec index cf9c47f..c37bc10 100644 --- a/send_grid.gemspec +++ b/send_grid.gemspec @@ -4,7 +4,7 @@ require "send_grid/version" Gem::Specification.new do |s| s.name = "sendgrid-rails" - s.version = SendGrid::VERSION + s.version = SendGridRails::VERSION s.platform = Gem::Platform::RUBY s.authors = ["PavelTyk"] s.email = ["paveltyk@gmail.com"] diff --git a/spec/api_header_spec.rb b/spec/api_header_spec.rb index 20daaf9..1e1aea8 100644 --- a/spec/api_header_spec.rb +++ b/spec/api_header_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' -describe SendGrid::ApiHeader do - let(:header) { SendGrid::ApiHeader.new } +describe SendGridRails::ApiHeader do + let(:header) { SendGridRails::ApiHeader.new } describe "#to_json" do it "returns valid json if no data was set" do header.to_json.should eql "{}" diff --git a/spec/mailer_spec.rb b/spec/mailer_spec.rb index 4e3717f..2751beb 100644 --- a/spec/mailer_spec.rb +++ b/spec/mailer_spec.rb @@ -54,7 +54,7 @@ it 'should be used in To defined in config dummy_recipient' do # dummy_recipient can be redefined config/initializers - SendGrid.configure do |config| + SendGridRails.configure do |config| config.dummy_recipient = 'noreply@example.com' end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 97d1e43..6a03426 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,6 +1,6 @@ require 'sendgrid-rails' -ActionMailer::Base.register_interceptor(SendGrid::MailInterceptor) +ActionMailer::Base.register_interceptor(SendGridRails::MailInterceptor) ActionMailer::Base.delivery_method = :test ActionMailer::Base.prepend_view_path File.join(File.dirname(__FILE__), "fixtures", "views") diff --git a/spec/version_spec.rb b/spec/version_spec.rb index dad3aba..c766065 100644 --- a/spec/version_spec.rb +++ b/spec/version_spec.rb @@ -1,8 +1,8 @@ require 'spec_helper' -describe 'SendGrid::VERSION' do +describe 'SendGridRails::VERSION' do it "returns string" do - SendGrid::VERSION.should be_an_instance_of(String) + SendGridRails::VERSION.should be_an_instance_of(String) end end