# Mailkick :bullettrain_side: Email subscriptions made easy - Add one-click unsubscribe links to your emails - Fetch bounces and spam reports from your email service Gracefully handles email address changes :postbox: Check out [Ahoy Email](https://github.com/ankane/ahoy_email) for analytics ## Installation Add this line to your application’s Gemfile: ```ruby gem 'mailkick' ``` And run the generator. This creates a model to store opt-outs. ```sh rails generate mailkick:install rake db:migrate ``` ## How It Works Add an unsubscribe link to your emails. #### Text ```erb Unsubscribe: <%= mailkick_unsubscribe_url %> ``` #### HTML ```erb <%= link_to "Unsubscribe", mailkick_unsubscribe_url %> ``` When a user unsubscribes, he or she is taken to a mobile-friendly page and given the option to resubscribe. To customize the view, run: ```sh rails generate mailkick:views ``` which copies the view into `app/views/mailkick`. ## Sending Emails Before sending marketing emails, make sure the user has not opted out. Add the following the method to your user model. ```ruby class User < ActiveRecord::Base mailkick_user end ``` Get all users who have opted out ```ruby User.opted_out ``` And those who have not (send to these people) ```ruby User.not_opted_out ``` Check one user ```ruby user.opted_out? ``` Unsubscribe ```ruby user.opt_out ``` Resubscribe ```ruby user.opt_in ``` ## Bounces and Spam Reports Fetch bounces, spam reports, and unsubscribes from your email service. ```ruby Mailkick.fetch_opt_outs ``` #### Sendgrid Add the gem ```ruby gem 'sendgrid_toolkit' ``` Be sure `ENV["SENDGRID_USERNAME"]` and `ENV["SENDGRID_PASSWORD"]` are set. #### Mandrill ```ruby gem 'mandrill-api' ``` Be sure `ENV["MANDRILL_APIKEY"]` is set. #### Mailchimp ```ruby gem 'gibbon' ``` Be sure `ENV["MAILCHIMP_API_KEY"]` and `ENV["MAILCHIMP_LIST_ID"]` are set. #### Other Will gladly accept pull requests. ### Advanced For more control over services, set them by hand. ```ruby Mailkick.services = [ Mailkick::Service::Sendgrid.new(api_key: "API_KEY"), Mailkick::Service::Mandrill.new(api_key: "API_KEY") ] ``` ## Multiple Lists You may want to split your emails into multiple categories, like sale emails and order reminders. Set the list in the mailer. ```ruby class UserMailer < ActionMailer::Base def order_reminder(user) header[:mailkick_list] = "order_reminders" # ... end end ``` Pass the `list` option to methods. ```ruby # scopes User.opted_out(list: "order_reminders") User.not_opted_out(list: "order_reminders") # instance methods user.opted_out?(list: "order_reminders") user.opt_out(list: "order_reminders") user.opt_in(list: "order_reminders") ``` Omitting list (`nil` list) means all lists - including future lists (think “Unsubscribe All”). ```ruby # opted out of all lists? user.opted_out? # opted out of the order reminder list *or* all lists? user.opted_out?(list: "order_reminders") ``` ### Opt-In Lists For opt-in lists, you’ll need to manage the subscribers yourself. Mailkick stores opt-outs, which you can combine with opt-ins. ```ruby # opt-ins minus opt-outs User.where(send_me_sales: true).not_opted_out(list: "sales") ``` Check one user ```ruby # opted in and didn't opt out user.send_me_sales && !user.opted_out?(list: "sales") ``` ## Bonus More great gems for email - [Roadie](https://github.com/Mange/roadie) - inline CSS - [Letter Opener](https://github.com/ryanb/letter_opener) - preview email in development ## Reference Change how the user is determined ```ruby Mailkick.user_method = proc {|email| User.where(email: email).first } ``` ## History View the [changelog](https://github.com/ankane/mailkick/blob/master/CHANGELOG.md) ## Contributing Everyone is encouraged to help improve this project. Here are a few ways you can help: - [Report bugs](https://github.com/ankane/mailkick/issues) - Fix bugs and [submit pull requests](https://github.com/ankane/mailkick/pulls) - Write, clarify, or fix documentation - Suggest or add new features