Mailjet's official Ruby wrapper, bootstraped with Mailjetter.
This gem helps you to:
- Send transactional emails through Mailjet API in Rails 3
- Manage your lists, contacts and campaigns
- Track email delivery through event API
Compatibility:
Rails ActionMailer integration designed for Rails 3.X
Add the following in your Gemfile:
# Gemfile
gem 'mailjet'
and let the bundler magic happen
$ bundle install
$ gem install mailjet
You need a proper account with Mailjet. You can get the API key through the Mailjet interface in Account/Master API key
Add the keys to an initializer:
# initializers/mailjet.rb
Mailjet.configure do |config|
config.api_key = 'your-api-key'
config.secret_key = 'your-secret-key'
config.default_from = '[email protected]'
end
domain
is needed if you send emails with :mailjet's SMTP (below)
default_from
is optional if you send emails with :mailjet's SMTP (below)
As easy as:
# application.rb
config.action_mailer.delivery_method = :mailjet
> contacts = Mailjet::Contact.all(status: 'active', start: 100, limit: 2)
=> [#<Mailjet::Contact>, #<Mailjet::Contact>]
> contacts = Mailjet::Contact.all(openers: true, start: 100, limit: 2)
=> [#<Mailjet::Contact>, #<Mailjet::Contact>]
All parameters and attributes at https://eu.mailjet.com/docs/api/contact/list All parameters and attributes for the openers option at https://eu.mailjet.com/docs/api/contact/openers
> contacts[0].infos
=> {blocked: 12, click: 1, email: '[email protected]'}
All parameters and attributes at https://eu.mailjet.com/docs/api/contact/infos
> list = Mailjet::List.create(label: 'My Mailjet list', name: "mymailjetlist")
All parameters and attributes at https://eu.mailjet.com/docs/api/lists/create
> Mailjet::List.all(limit: 10, start: 0, orderby: 'id ASC')
All parameters and attributes at https://eu.mailjet.com/docs/api/lists/all
> list = Mailjet::List.all.find{|l| l.name == 'mymailjetlist' }
> list = list.update(label: 'My updated Mailjet list', name: "myupdatedmailjetlist")
All parameters and attributes at https://eu.mailjet.com/docs/api/lists/update
> list.contacts
=> [#<Mailjet::Contact>, #<Mailjet::Contact>]
All parameters and attributes at https://eu.mailjet.com/docs/api/lists/contacts
> list.add_contacts("[email protected]", "[email protected]", force: true )
=> OK
All parameters and attributes at https://eu.mailjet.com/docs/api/lists/addmanycontacts
> list.remove_contacts("[email protected]", "[email protected]")
=> OK
All parameters and attributes at https://eu.mailjet.com/docs/api/lists/removemanycontacts
> list.email
=> "[email protected]"
All parameters and attributes at https://eu.mailjet.com/docs/api/lists/email
> list.statistics
=> {active: 20, bounce: 1, click: 14, created_at: "2012-02-02 21:59:59", ...}
All parameters and attributes at https://eu.mailjet.com/docs/api/lists/statistics
> list.delete
=> OK
All parameters and attributes at https://eu.mailjet.com/docs/api/lists/delete
> campaign = Mailjet::Campaign.create(title: "My Mailjet Campaign", list_id: Mailjet::List.all.first.id, from: "[email protected]", from_name: "Sender Name", subject: "Our new product", lang: "en", footer: "default")
All parameters and attributes at https://eu.mailjet.com/docs/api/message/createcampaign
> campaigns = Mailjet::Campaign.all(start: 10, limit: 20)
=> [#<Mailjet::Campaign>, #<Mailjet::Campaign>, ...]
All parameters and attributes at https://eu.mailjet.com/docs/api/message/campaigns
> campaigns = Mailjet::Campaign.find(19)
=> #<Mailjet::Campaign>
All parameters and attributes at https://eu.mailjet.com/docs/api/message/campaigns
> campaign.update(title: "My *new* Mailjet Campaign")
All parameters and attributes at https://eu.mailjet.com/docs/api/message/updatecampaign
> campaign.contacts(limit: 2, start: 0, status: 'queued')
=> [#<Mailjet::Contact id: 123, email: '[email protected]', sent_at: "2012-02-02 21:59:59", status: 'queued'>, #<Mailjet::Contact id: 456, email: '[email protected]', sent_at: "2012-02-02 23:13:02", status: 'queued'>]
All parameters and attributes at https://eu.mailjet.com/docs/api/message/contacts
> campaign.send!
=> OK
All parameters and attributes at https://eu.mailjet.com/docs/api/message/sendcampaign
> campaign.test('[email protected]')
=> OK # response status
All parameters and attributes at https://eu.mailjet.com/docs/api/message/testcampaign
> campaign.statistics
=> { total: 200, bounce: 1, bounce_pct: 0.5, click: 10, click_pct: 5, open: 20, open_pct: 10, sent: 200, sent_pct: 100, spam: 1, spam_pct: 0.5, total: 200 }
All parameters and attributes at https://eu.mailjet.com/docs/api/message/statistics
> Mailjet::TemplateCategory.all
=> [#<Mailjet::TemplateCategory id: 2, label: "basic", value: "Basic">, #<Mailjet::TemplateCategory id: 6, label: "design", value: "Design">]
All parameters and attributes at https://eu.mailjet.com/docs/api/message/tplcategories
> Mailjet::TemplateModel.all(category: 2, custom: true, locale: 'fr_FR')
=> [<#Mailjet::TemplateModel id: 4, name: "Text", header_link: "http:\/\/" ... ]
All parameters and attributes at https://eu.mailjet.com/docs/api/message/tplmodels
> campaign.html
=> "<html><head></head><body>Test</body></html>"
All parameters and attributes at https://eu.mailjet.com/docs/api/message/htmlcampaign
> campaign.set_html("<html><head><title>Test</title></head><body>Test <a href=\"[[UNSUB_LINK_EN]]\">[[UNSUB_LINK_EN]]</a></body></html>")
=> OK # response status
All parameters and attributes at https://eu.mailjet.com/docs/api/message/sethtmlcampaign
> new_campaign = campaign.duplicate(title: "Another Mailjet Campaign")
=> #<Mailjet::Campaign title: "Another Mailjet Campaign" ... >
All parameters and attributes at https://eu.mailjet.com/docs/api/message/duplicatecampaign
> clicks = Mailjet::Reporting.clicks(from: "[email protected]")
=> [<#Mailjet::Click id: 4, click_delay: 1234, date: "2012-02-08", link: "", user_agent: "Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0"} ]
> clicks.first.contact
=> #<Mailjet::Contact id: 123, email: "[email protected]">
All parameters and attributes at https://eu.mailjet.com/docs/api/report/click
> Mailjet::Reporting.domains(start: 3, limit: 6)
=> [{bounce_rate: 0.1, clicked_rate: 0.2, ...}, {bounce_rate: 0.8, clicked_rate: 0.1, ...}]
All parameters and attributes at https://eu.mailjet.com/docs/api/report/domain
> Mailjet::Reporting.clients(start: 3, limit: 6)
=> [{client: "Gmail", open_rate: 0.1, platform: "Windows", ...}, {client: "Outlook", open_rate: 0.3, platform: "Windows", ...}]
All parameters and attributes at https://eu.mailjet.com/docs/api/report/emailclients
> Mailjet::Reporting.emails(from_domain: "domain.com", limit: 10)
=> [#<Mailjet::Email>, #<Mailjet::Email>, #<Mailjet::Email>, ...]
All parameters and attributes at https://eu.mailjet.com/docs/api/report/emailsent
> Mailjet::Reporting.statistics(from_domain: "domain.com", limit: 10)
=> { avg_clicked_delay: 123, avg_opened_rate: 0.432, blocked: 13, ...}
All parameters and attributes at https://eu.mailjet.com/docs/api/report/emailstatistics
> Mailjet::Reporting.geolocation(from_domain: "domain.com", limit: 10)
=> [{click: 123, country: "France", open: 234}, {click: 9876, country: "US", open: 12345}]
All parameters and attributes at https://eu.mailjet.com/docs/api/report/geoip
> Mailjet::Reporting.agents(start: 3, limit: 6)
=> [{cnt_clicked: 123, part: 30, platform: "Windows", user_agent: "Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0"}, ...]
All parameters and attributes at https://eu.mailjet.com/docs/api/report/useragents
You can setup your Rack application in order to receive feedback on emails you sent (clicks, etc.)
First notify Mailjet of your desired endpoint (say: 'http://www.my_domain.com/mailjet/callback') at https://www.mailjet.com/account/triggers
Then configure Mailjet's Rack application to catch these callbacks.
A typical Rails/ActiveRecord installation would look like that:
# application.rb
config.middleware.use Mailjet::Rack::Endpoint, '/mailjet/callback' do |params| # using the same URL you just set in Mailjet's administration
email = params['email'].presence || params['original_address'] # original_address is for typofix events
if user = User.find_by_email(email)
user.process_email_callback(params)
else
Rails.logger.fatal "[Mailjet] User not found: #{email} -- DUMP #{params.inspect}"
end
end
# user.rb
class User < ActiveRecord::Base
def process_email_callback(params)
# Returned events and options are described at https://eu.mailjet.com/docs/event_tracking
case params['event']
when 'open'
# Mailjet's invisible pixel was downloaded: user allowed for images to be seen
when 'click'
# a link (tracked by Mailjet) was clicked
when 'bounce'
# is user's email valid? Recipient not found
when 'spam'
# gateway or user flagged you
when 'blocked'
# gateway or user blocked you
when 'typofix'
# email routed from params['original_address'] to params['new_address']
else
Rails.logger.fatal "[Mailjet] Unknown event #{params['event']} for User #{self.inspect} -- DUMP #{params.inspect}"
end
end
Note that since it's a Rack application, any Ruby Rack framework (say: Sinatra, Padrino, etc.) is compatible.
For maximum reliability, the gem is tested against Mailjet's server for some parts, which means that valid credentials are needed. Do NOT use your production account (create a new one if needed), because some tests are destructive.
# GEM_ROOT/config.yml
mailjet:
api_key: YOUR_API_KEY
secret_key: YOUR_SECRET_KEY
default_from: YOUR_REGISTERED_SENDER_EMAIL # the email you used to create the account should do it
Then at the root of the gem, simply run:
bundle
rake
- Fork the project.
- Create a topic branch.
- Implement your feature or bug fix.
- Add documentation for your feature or bug fix.
- Add specs for your feature or bug fix.
- Commit and push your changes.
- Submit a pull request. Please do not include changes to the gemspec, or version file.