GithubHelp home page GithubHelp logo

mailgun_rails's Introduction

mailgun_rails

Build Status

mailgun_rails is an Action Mailer adapter for using Mailgun in Rails apps. It uses the Mailgun HTTP API internally.

Installing

In your Gemfile

gem 'mailgun_rails'

Usage

To configure your Mailgun credentials place the following code in the corresponding environment file (development.rb, production.rb...)

config.action_mailer.delivery_method = :mailgun
config.action_mailer.mailgun_settings = {
		api_key: '<mailgun api key>',
		domain: '<mailgun domain>'
}

Now you can send emails using plain Action Mailer:

email = mail from: '[email protected]', to: '[email protected]', subject: 'this is an email'
or 
email = mail from: 'Your Name Here <[email protected]>', to: '[email protected]', subject: 'this is an email'
email.mailgun_variables = {name_1: :value_1, name_2: value_2}
email.mailgun_recipient_variables = {'[email protected]' => {id: 1}, '[email protected]' => {id: 2}}
email.mailgun_headers = {foo: 'bar'}

Mailgun options

To provide option parameters like o:campaign or o:tag.

email.mailgun_options = {campaign: '1'}

Pull requests are welcomed

mailgun_rails's People

Contributors

aviflombaum avatar bigardone avatar bolasblack avatar iainbeeston avatar jorgemanrubia avatar knwang avatar onemanstartup avatar rgraff avatar skelz0r avatar techsin avatar tylerrick avatar xxuejie avatar zedtux avatar zzet avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

mailgun_rails's Issues

Unknown Mailgun Error

I have follow the usage guide by setting config.action_mailer to mailgun and using my api_key and my domain, but I get this error message that unfortunately doesn't help.

Mailgun::DeliveryError: Unknown Mailgun Error
    from /home/fahmi/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/mailgun-rails-0.1.1/lib/mailgun-rails.rb:51:in `deliver!'
    from /home/fahmi/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/mail-2.6.3/lib/mail/message.rb:252:in `deliver!'
    from /home/fahmi/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/actionmailer-4.2.3/lib/action_mailer/message_delivery.rb:77:in `deliver_now!'
    from /home/fahmi/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/actionmailer-4.2.3/lib/action_mailer/message_delivery.rb:95:in `deliver!'
    from (irb):3
    from /home/fahmi/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.2.3/lib/rails/commands/console.rb:110:in `start'
    from /home/fahmi/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.2.3/lib/rails/commands/console.rb:9:in `start'
    from /home/fahmi/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.2.3/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /home/fahmi/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.2.3/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /home/fahmi/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.2.3/lib/rails/commands.rb:17:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

Am I missing something?

RestClient::ResourceNotFound (404 Resource Not Found)

I have deployed my app to Digital Ocean and while it seems to work correctly in development, in production I keep getting that message.

Any ideas as to why that might be happening?

I should also mention, this is happening with Devise, upon confirming the account.

rails3 issues?

does anybody have rails3 issues?

we are getting RestClient::BadRequest: 400 Bad Request with same code that works in rails5

Another typo found.

In the readme file:

rails_mailgun is an Action Mailer adapter for using Mailgun in Rails apps. It uses the Mailgun HTTP API internally.

rails_mailgun should be mailgun_rails.

This gem doesn't support Rails 4?

Bundler could not find compatible versions for gem "rails":
In Gemfile:
mailgun_rails (> 0.5.3) ruby depends on
rails (
> 3.2.13) ruby

rails (4.0.0)

Pretty frustrated...

Would you be willing to publish a new version of this to rubygems?

The recent addition of the mailgun message id in the mail object was really useful for me to track/store message replies. Unfortunately, my production server isn't set up to retrieve gems from github. Would you be able to publish a minor version update to rubygems?

Thank you!

Receive email

Hi,

Is it possible to configure mailgun_rails to receive email to app ?

Let me know the steps for the same.

Thanks.

Development environment: RestClient::ResourceNotFound (404 Resource Not Found)

I'm using this gem to work with Devise.

in develoment.rb file, I have

config.action_mailer.delivery_method = :mailgun
config.action_mailer.mailgun_settings = {
  api_key: 'key-944123xxxxxxxxxxxxxxxxxxxxxxxaa0',
  domain: 'sandboxd34xxxxxxxxxxxxxxxxxxxxxxx20a105'
}

I have a Mailer Class, mailgun_devise_mailer.rb

class MailgunDeviseMailer < Devise::Mailer
    helper :application
    include Devise::Controllers::UrlHelpers
    default template_path: 'devise/mailer'
end

and the proper link to devise, in devise.rb:

config.mailer = 'MailgunDeviseMailer'

But when I register a user, I get a

RestClient::ResourceNotFound (404 Resource Not Found) error

Completed 500 Internal Server Error in 1202ms

RestClient::ResourceNotFound (404 Resource Not Found):
app/controllers/clients/registrations_controller.rb:15:in `create'

Rendered /home/aleksandrus/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_source.erb (9.0ms)
Rendered /home/aleksandrus/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (3.8ms)
Rendered /home/aleksandrus/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.3ms)
Rendered /home/aleksandrus/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (25.4ms)
Rendered /home/aleksandrus/.rvm/gems/ruby-2.2.1/gems/web-console-2.3.0/lib/web_console/templates/_markup.html.erb (0.3ms)
Rendered /home/aleksandrus/.rvm/gems/ruby-2.2.1/gems/web-console-2.3.0/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.3ms)
Rendered /home/aleksandrus/.rvm/gems/ruby-2.2.1/gems/web-console-2.3.0/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.3ms)
Rendered /home/aleksandrus/.rvm/gems/ruby-2.2.1/gems/web-console-2.3.0/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.3ms)
Rendered /home/aleksandrus/.rvm/gems/ruby-2.2.1/gems/web-console-2.3.0/lib/web_console/templates/console.js.erb within layouts/javascript (13.0ms)
Rendered /home/aleksandrus/.rvm/gems/ruby-2.2.1/gems/web-console-2.3.0/lib/web_console/templates/main.js.erb within layouts/javascript (0.2ms)
Rendered /home/aleksandrus/.rvm/gems/ruby-2.2.1/gems/web-console-2.3.0/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.3ms)
Rendered /home/aleksandrus/.rvm/gems/ruby-2.2.1/gems/web-console-2.3.0/lib/web_console/templates/index.html.erb (25.0ms)

Why would it happen?

Issue adding a campaign to mail

Hi there - thanks so much for the gem. I'm having issues adding a campaign tag:

email = mail(to: subscription.email, subject: "This week's update!")
email.mailgun_options = {campaign: 'weekly_digest'}

But mailgun logs showing:

"campaigns": []

Where have I gone wrong? Thanks

mocking response from mailgun in my rspec test suite

given:

# app/models/user.rb
def send_daily_request
    self.team_members.each do |member|
      if member.email != self.email
        mail = UserMailer.daily_request(self, member)
        #mail.mailgun_special_variables = {deliverytime:Util.send_question_time(self.timezone, self.daily_update_time)}
        mail.deliver_now
        response = mail.mailgun_api_response

        if response.present?
          save_outgoing(member, response["id"])
          true
        else
          false
        end
      end
    end
  end

...

def save_outgoing(member, message_id)
    message = member.messages.create
    message.build_out_going_message(question:member.user.question)
    message.message_id = message_id
    message.save
  end

which is tested with:

describe 'send_question' do
    it 'saves outgoing message' do
      user = FactoryGirl.create(:team_admin)

      #2 because members include the team admin
      expect{user.send_daily_request}.to change(Message, :count).by(3)
    end
  end

and the forked code retrieving the response from mailgun:

def deliver!(rails_message)
      response = mailgun_client.send_message build_mailgun_message_for(rails_message)
      rails_message.mailgun_api_response = JSON.parse response

      rails_message
    end

how do I test this properly? It seems like my test cannot properly retrieve the response from Mailgun (this works when not on test). Should I use VCR gem for this? or maybe mock the response from Mailgun?

Default mailgun_headers option

I am loving this gem and the configurability of it. In particular I like how you can set mailgun_headers: "email.mailgun_headers = {foo: 'bar'}".

Going a step further, I'd like to default to mailgun_headers having testmode enabled. Only in production I want to override that to actually send emails for real. Is there a way to set config.default_mailgun_headers so that it defaults to being in test mode?

Thanks again for the great gem and let me know if there's anything I can do to help (I could maybe submit a PR if needed).

sending bulk email with singular to recipient

I am trying to send bulk emails through mailgun using this gem, following the example here. While the recipient_variables are present and are inserted into the page content, the to field lists all recipients, which isn't my desired or expected behavior.

I tried sending an email with out mail to and only setting the recipient_variables, but I recieve the error:

NoMethodError: undefined method formatted' for nil:NilClass
mailgun_rails-0.8.0/lib/mailgun/deliverer.rb:48:in build_basic_mailgun_message_for

Any suggestion for sending bulk email with a singular to recipient?

Set custom headers

Awesome gem Jorge!

Mailgun allows setting of custom MIME headers through the API calls, with the syntax of "h:X-My-Header". In our use case we need to set the "h:In-Reply-to" header to thread reply email together as a gmail "conversation", very similar to this:

http://blog.mailgun.com/post/tracking-replies-in-mailgun-or-any-other-email/

This gem doesn't support setting the "h:xxx" header yet, so I went and added that. See my pull request for that.

I didn't add any tests yet, but if you OK this I can do that.

mailer api config

the api_url should be constructed by a settings option, instead of hardcoded (v2). currently v3 is available.

https://github.com/jorgemanrubia/mailgun_rails/blob/master/lib/mailgun/client.rb#L22

Would prefer this:

    def api_url
      "https://api:#{api_key}@#{api_base_url}/#{domain}"
    end

To be set like this...

config.action_mailer.mailgun_settings = {
    api_key: ENV['MAILGUN_API_KEY'],
    domain: ENV['MAILGUN_DOMAIN'],
    api_base_url: ENV['MAILGUN_API_BASE_URL']
}

api_base_url is api.mailgun.net/v3

License missing from gemspec

RubyGems.org doesn't report a license for your gem. This is because it is not specified in the gemspec of your last release.

via e.g.

spec.license = 'MIT'
# or
spec.licenses = ['MIT', 'GPL-2']

Including a license in your gemspec is an easy way for rubygems.org and other tools to check how your gem is licensed. As you can imagine, scanning your repository for a LICENSE file or parsing the README, and then attempting to identify the license or licenses is much more difficult and more error prone. So, even for projects that already specify a license, including a license in your gemspec is a good practice. See, for example, how rubygems.org uses the gemspec to display the rails gem license.

There is even a License Finder gem to help companies/individuals ensure all gems they use meet their licensing needs. This tool depends on license information being available in the gemspec. This is an important enough issue that even Bundler now generates gems with a default 'MIT' license.

I hope you'll consider specifying a license in your gemspec. If not, please just close the issue with a nice message. In either case, I'll follow up. Thanks for your time!

Appendix:

If you need help choosing a license (sorry, I haven't checked your readme or looked for a license file), GitHub has created a license picker tool. Code without a license specified defaults to 'All rights reserved'-- denying others all rights to use of the code.
Here's a list of the license names I've found and their frequencies

p.s. In case you're wondering how I found you and why I made this issue, it's because I'm collecting stats on gems (I was originally looking for download data) and decided to collect license metadata,too, and make issues for gemspecs not specifying a license as a public service :). See the previous link or my blog post about this project for more information.

Not sending emails

When I first set this up I was able to send mail with it (see #14). But now it's not working for some reason. There are no errors in my development log, so you would think the mail went out but there's no mention of it in my Mailgun logs and I never receive the test email.

app/mailers/notification_mailer.rb

class NotificationMailer < ActionMailer::Base
  default to: "[email protected]",  # My actual email is used in the app
              from: "<Notifications> [email protected]"

  def test_mail
    mail subject: 'test email'
  end
end

log/development.log

  Rendered notification_mailer/test_mail.html.haml (4.4ms)
  Rendered notification_mailer/test_mail.text.haml (1.2ms)

NotificationMailer#test_mail: processed outbound mail in 33.9ms

Sent mail to [email protected] (11.2ms)
Date: Tue, 03 Jun 2014 12:39:12 -0400
from: <Notifications> [email protected]
To: [email protected]
Message-ID: <[email protected]>
Subject: test email
Mime-Version: 1.0
Content-Type: multipart/alternative;
 boundary="--==_mimepart_538dfa30d506c_75553ff2eb7c3328763d5";
 charset=UTF-8
Content-Transfer-Encoding: 7bit


----==_mimepart_538dfa30d506c_75553ff2eb7c3328763d5
Content-Type: text/plain;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

This is a test
============
Hello!

----==_mimepart_538dfa30d506c_75553ff2eb7c3328763d5
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

<!DOCTYPE html>
<html>
  <head>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type'>
  </head>
  <body>
    <h2>Header</h2>
    <p>This is a paragraph</p>
  </body>
</html>

----==_mimepart_538dfa30d506c_75553ff2eb7c3328763d5--

However, sending mails directly via Mailgun's HTTP API does work.

Nil CC issue with latest rest-client

I recently upgraded to rest-client 2.0.0 and found the encoding changes broke my mailer.

Specifically I sometimes had an nil-valued Cc field.

With rest-client v1.8.0 this results in no Cc field in the POST.
With rest-client v2.0.0 this results in an empty Cc field in the POST.
The latter throws a 400 Bad Request from mailgun.

my fix: don't include the nil Cc parameter in the call to mail.

However that fix is not good general advice. ActionMailer::Base documentation specifically mentions setting fields to nil to clear them. So mailgun_rails should handle this case and remove the field entirely.

How to send mail?

I'm a new Rails dev so bear with me. Typically in a Rails 4 app you would create and send email like so

# app/mailers/my_mailer.rb
class MyMailer < ActionMailer::Base
  def test_email
    mail to: '[email protected]', from: '[email protected]', subject: 'test email'
  end
end
# app/models/user.rb
class User < ActiveRecord::Base
  after_create :send_welcome_mail

  def send_welcome_mail
    MyMailer.test_email.deliver
  end
end

So using mailgun_rails, I expected the following to work in Rails console

(mail from: '[email protected]', to: '[email protected]', subject: 'test email').deliver

but got the error

ArgumentError: An SMTP From address is required to send a message. Set the message smtp_envelope_from, return_path, sender, or from address.

which is confusing because I'm trying to send mail using Mailgun's HTTP API, not via SMTP.

Am I using mailgun_rails correctly?

I set up ActionMailer like so

config.action_mailer.delivery_method = :mailgun
config.action_mailer.mailgun_settings = {
  api_key: ENV['MAILGUN_API_KEY'],
  domain: ENV['OUTGOING_EMAIL_DOMAIN']
}

Build fails on rubinius

Right now the build fails on rubinius, but this seems to be an instance of this bug. The fix for that is due to arrive in the next version of the mail gem, but I'm not sure when that will arrive.

The options are:

  1. Make travis ignore rubinius failures
  2. Make the Gemfile use the master version of mail straight from github
  3. Leave it failing

I'd imagine 1 is probably the best solution for now, until the underlying issue can be resolved

Can not send mail with attachments

Hey jorgemanrubia, , so glad to find mailgun_rails support attachments now.

Today I tried to send a mail with attachment, but I failed……

Then I found the code https://github.com/jorgemanrubia/mailgun_rails/blob/master/lib/mailgun/attachment.rb#L13 will raise a TypeError, message is no implicit conversion of Hash into String

I'm not sure what's wrong……

My ruby version is 2.2.0, and my code is here:

  def destroyed user
    @user = user
    attachments['exported_data.json'] = {
      mime_type: 'application/json',
      content: user.export_data.to_json
    }
    mail from: '...', to: '...'
  end

gem name error

the corrent gem name should be mailgun_rails, not rails_mailgun

Conflicts with the official mailgun_ruby gem

Since you are both declaring a Mailgun::Client class I can't use both gems at the same time.
I really like the way this gem uses ActionMailer for sending api email, but mailgun_ruby is nice for other api access, which isn't supported by this gem.

Any chance you're willing to move to MailgunRails or Mailgun::Rails module?

Fails to send email if to: field is missing

When I send emails with just a bcc: key and no to: key will fail with undefined method formatted for nilClass.

Is there a way around this or should i submit a PR for this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.