SendGrid SMTP API gem for Rails <img src=“https://secure.travis-ci.org/kylejginavan/sendgrid_smtpapi.png”/>
SendGrid SMTP API gem provides ActionMailer::Base extensions to use SendGrid API features in you emails. It extends ActionMailer with next methods:
substitute(patters_string, array_of_substitunion_strings) uniq_args(hash_of_unique_args) category(category_string) open_tracking(enabled = true) add_filter_setting(filter_name, setting_name, value) standard_smtp(enabled = false)
In your Gemfile:
gem'sendgrid_smtpapi'
In your config/environment.rb:
ActionMailer::Base.register_interceptor(SendGridSmtpApi::MailInterceptor) ActionMailer::Base.smtp_settings = { :address=>'smtp.sendgrid.net', :port=>'25', :domain=>'example.com', :authentication=>:plain, :user_name=>'login@example.com', :password=>'your password' }
classMailer<ActionMailer::Basedefault:from=>'no-reply@example.com', :subject=>'An email sent via SendGridSmtpApi'defemail_with_multiple_recipientsmail:to=>%w(email1@email.com email2@email.com)endend
Mailer class definition:
classMailer<ActionMailer::Basedefault:from=>'no-reply@example.com', :subject=>'An email sent via SendGridSmtpApi with substitutions'defemail_with_substitutionssubstitute'-user_name-', %w(User1 User2)mail:to=>%w(email1@email.com email2@email.com), :body=>"Hello, -user_name-!"endend
Mailer class definition: class Mailer < ActionMailer::Base default :from => 'no-reply@example.com', :subject => 'An email sent via SendGridSmtpApi with substitutions' def email_with_category category 'SendGridSmtpApiRocks' mail :to => 'email1@email.com' end end
Apps can be applied to any of your email messages and can be configured through SendGridSmtpApi gem.
Add an invisible image at the end of the email to track e-mail opens. If the email recipient has images enabled on the email client, a request to server for the invisible image is executed and an open is logged.
classMailer<ActionMailer::Basedefault:from=>'no-reply@example.com', :subject=>'An email sent via SendGridSmtpApi'defemail_with_open_tracking_enabledopen_trackingtruemail:to=>'email@email.com'endend
if you want to customize the header for sendgrids api but you need keep standard SMTP see below:
classMailer<ActionMailer::Basedefault:from=>'no-reply@example.com', :subject=>'An email sent via SendGridSmtpApi'defemail_standard_smtpstandard_smtptrueopen_trackingtruemail:to=>'email@email.com'endend
Kyle J. Ginavan.
Mauro Torres - github.com/chebyte