Configure Postmark for opt-out fetching
masterTo use Postmark, add the postmark gem and set the following environment variable:
ENV["POSTMARK_API_KEY"]
gem "postmark"repository·master·Indexed 21 days ago
https://github.com/ankane/mailkickA Ruby on Rails gem for managing email subscriptions. Mailkick provides tools for adding one-click unsubscribe links and RFC 8058 headers to emails, and automatically fetches bounces and spam reports from providers including AWS SES, Mailchimp, Mailgun, Mandrill, Postmark, and SendGrid to keep subscriber lists clean.
To use Postmark, add the postmark gem and set the following environment variable:
ENV["POSTMARK_API_KEY"]gem "postmark"To use Mandrill, add the mandrill-api gem and set the following environment variable:
ENV["MANDRILL_API_KEY"]gem "mandrill-api"Unsubscribe links created before version 1.1.1 (January 2023) will not work by default. To restore support, use the rotate method in an initializer with your previous secret token and Marshal serializer:
Mailkick.message_verifier.rotate(previous_secret_token, serializer: Marshal)In 2.0, Postmark uses the suppressions API and defaults to the broadcast stream. To use the outbound stream instead, configure the service manually:
Mailkick.services = [
Mailkick::Service::Postmark.new(api_key: api_key, stream_id: "outbound")
]To use Mailgun, add the mailgun-ruby gem and set the following environment variable:
ENV["MAILGUN_API_KEY"]gem "mailgun-ruby"Add mailkick to your Gemfile, run the installation generator to create the necessary subscription tables, and migrate your database.
# Gemfile
gem "mailkick"bundle install
rails generate mailkick:install
rails db:migrateMailkick can synchronize opt-outs (bounces, spam reports, unsubscribes) from your email provider.
Mailkick.process_opt_outs_method. This lambda receives an array of opt_outs (each containing :email and :time).Mailkick.fetch_opt_outs to trigger the fetch process.Supported services require specific gems and environment variables (see service-specific records).
# config/initializers/mailkick.rb
Mailkick.process_opt_outs_method = lambda do |opt_outs|
emails = opt_outs.map { |v| v[:email] }
subscribers = User.includes(:mailkick_subscriptions).where(email: emails).index_by(&:email)
opt_outs.each do |opt_out|
subscriber = subscribers[opt_out[:email]]
next unless subscriber
subscriber.mailkick_subscriptions.each do |subscription|
# Destroy subscriptions created before the opt-out event time
subscription.destroy if subscription.created_at < opt_out[:time]
end
end
end
# To execute the fetch:
Mailkick.fetch_opt_outsTo use AWS SES, add the aws-sdk-sesv2 gem and ensure your AWS credentials are configured. The IAM user/role requires the ses:ListSuppressedDestinations permission.
Note: If using SES before November 25, 2019, you may need to manually enable the account-level suppression list feature.
gem "aws-sdk-sesv2"To use SendGrid, add the sendgrid-ruby gem and set the following environment variable:
ENV["SENDGRID_API_KEY"]The API key requires the Suppressions permission.
gem "sendgrid-ruby"To use Mailchimp, add the gibbon gem (version >= 2) and set the following environment variables:
ENV["MAILCHIMP_API_KEY"]ENV["MAILCHIMP_LIST_ID"]gem "gibbon", ">= 2"Use the mailkick_unsubscribe_url helper to generate links for your emails. This works for both HTML and text formats. When clicked, users are directed to a mobile-friendly page where they can confirm unsubscription or resubscribe. You can customize the unsubscription view by running the mailkick:views generator.
# HTML emails
<%= link_to "Unsubscribe", mailkick_unsubscribe_url(@user, "sales") %>
# Text emails
Unsubscribe: <%= mailkick_unsubscribe_url(@user, "sales") %># To customize the unsubscription view:
rails generate mailkick:viewsTo support one-click unsubscribe headers, enable them in a Mailkick initializer. These headers will be automatically added to any email that utilizes the mailkick_unsubscribe_url helper.
# config/initializers/mailkick.rb
Mailkick.headers = trueMailkick provides a Rails generator to automate the installation process, including the creation of the necessary migration file for tracking subscriptions. Running the generator will create a migration file named db/migrate/create_mailkick_subscriptions.rb which sets up the required database schema for Mailkick to function.
rails generate mailkick:install