Ruby client for the Mailgun API.
$ gem install mailgunner
require 'mailgunner'
mailgun = Mailgunner::Client.new({
domain: 'samples.mailgun.org',
api_key: 'key-3ax6xnjp29jd6fds4gc373sgvjxteol0'
})
response = mailgun.get_stats(limit: 5)
Best practice for storing credentials for external services is to use environment variables, as described by 12factor.net/config. Mailgunner::Client defaults to extracting the domain and api_key values it needs from the MAILGUN_API_KEY and MAILGUN_SMTP_LOGIN environment variables. These will exist if you are using Mailgun on Heroku, or you can set them manually.
Mailgunner integrates with ActionMailer.
If you are using Rails, you can use Mailgunner to send mail via Mailgun by adding
the following line to config/environments/production.rb
:
config.action_mailer.delivery_method = :mailgun
If for some reason you can't set the required ENV variables, you can configure Mailgunner through ActionMailer settings:
config.action_mailer.mailgun_settings = {
domain: 'test.com'
api_key: 'your-api-key'
}
Outside of Rails you can set ActionMailer::Base.delivery_method
directly.
If you only need to use Mailgun's email address validation service, you can instead use your Mailgun public key to authenticate like this:
require 'mailgunner'
public_key = 'pubkey-5ogiflzbnjrljiky49qxsiozqef5jxp7'
mailgun = Mailgunner::Client.new(api_key: public_key)
response = mailgun.validate_address('[email protected]')
If you want to track your emails based on their name in the Mailgun dashboard, you can use the tagging functionality like this:
# In your mailer
include Mailgunner::TagHelper
after_filter :add_tag
so when you call Mailer.welcome(user).deliver_now, the email sent will receive the 'welcome' tag.