Code Monkey home page Code Monkey logo

mandrill_dm's Introduction

Mandrill DM

Build Status Gem Version security Code Climate

Mandrill DM allows you to use ActionMailer with the Mandrill API. Created by Jonathan Berglund and maintained by John Dell, and Kirill Shnurov and various contributors.

!!! MIGRATE from Mandrill IMMEDIATELY!

On 15.03.2022 Mailchimp blocked tens of thousands of accounts for one reason: their nationality. There's no law that required that. See the email

They didn't give any prior notice, time to migrate, or an option to download our data and email lists. We're unable to log in despite using Mailchimp & Mandrill for 8 years and an excellent account score.

WE'VE LOST ALL OUR DATA and our operations were disrupted.

Mailchimp's actions are completely unlawful, violate business ethics and moral norms. This is pure racism and Nazism. It should not be tolerated.

We urge you to MIGRATE to other services IMMEDIATELY until your account is blocked because of your race, sex, nationality, or T-shirt color.

This gem will continue to work, but will log error on every sent message.

You're free to fork it and keep using Mandrill if you support punishing innocent people for being born in a particular country, no matter what they think about their government and recent events.

Rails Setup

First, add the gem to your Gemfile and run the bundle command to install it.

gem 'mandrill_dm'

Second, set the delivery method in config/environments/production.rb.

config.action_mailer.delivery_method = :mandrill

Third, create an initializer such as config/initializers/mandrill.rb and paste in the following code:

MandrillDm.configure do |config|
  config.api_key = ENV['MANDRILL_APIKEY']
  # config.async = false
end

NOTE: If you don't already have an environment variable for your Mandrill API key, don't forget to create one.

Rails 3: see Rails 3 (Mail 2.5) support

Available configuration options

Option Default value Description
api_key Mandrill API key.
async false Enable a background sending mode that is optimized for bulk sending.

Mandrill Templates

If you want to use this gem with mandrill templates you just have to add the template param to the mail function and set the body param to empty string ''.

We use template instead of template_name as described in mandrill documentation since template_name it's used by ActionMailer.

class MyMailer < ActionMailer::Base
  def notify_user(email)
    headers['Reply-To'] = '[email protected]'
    mail(
      to: email,
      from: '[email protected]',
      body: '',
      template: 'your-mandrill-template-slug',
      template_content: [ # optional
        {
          name: 'header', # the name of the mc:edit editable region to inject into
          content: 'string to replace a mc:edit="header" in your template', # the content to inject
        },
        {
          name: 'content',
          content: 'string to replace a mc:edit="content" in your template'
        }
      ]
    )
  end
end

Development & Feedback

Questions or problems? Please use the issue tracker. If you would like to contribute to this project, fork this repository. Pull requests appreciated! Please ensure all specs and rubocop checks pass locally (run rake) and verify the travis build matrix passes.

This gem was inspired by the letter_opener and mandrill-delivery-handler gems. Special thanks to the folks at MailChimp and Mandrill for their Starter service and Ruby API.

Interactive Usage

$ irb -I . -r 'lib/mandrill_dm'

require 'pry'

mandrill_dm's People

Contributors

ankurvyas-btc avatar beorc avatar brushbox avatar dmatheron avatar dorongutman avatar genaromadrid avatar januszm avatar jlberglund avatar kshnurov avatar norbertszivos avatar piotrze avatar ropiku avatar rossjourdain avatar spovich avatar tensho avatar tomasc 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

Watchers

 avatar  avatar  avatar  avatar

mandrill_dm's Issues

๐Ÿ™‡ Thank you very much for this gem ๐Ÿ’Ž

It saved me a lot of time with Mail::Message conversion to MandrillDm::Message, especially as mail gem doesn't have consistent/intuitive public API for simple things like mail.from and mail.to.
Much appreciate with your work and will definitely contribute if I find something useful ๐Ÿ‘

merge_vars support

merge_vars and global_merge_vars will allow to send customized messages to each recipient.

Readme syntax for template variables incorrect

The ability to use Mandrill templates is excellent, but the Readme example seems to be a bit off-- specifically this bit:

template_content: [ # optional
    {
      name: 'header',
      content: 'string to replace a *|header|* in your template',
    },
    {
      name: 'content',
      content: 'string to replace a *|content|* in your template'
    }
  ]
)

The template_content key is used by editable content areas, which insert content into tags with an associated mc:edit property, while *|variable|* syntax is for merge variables, which can be specified either with the global_merge_vars key (for all recipients) or the merge_vars key (per recipient).

Are you alive? :)

Maybe you'll transfer this repo to me? I'll continue support and merge pull requests.

BCC

I was surprised, that MandrillDm::Message doesn't have #bcc method. After digging some code I noticed, that we accumulate to, cc and bcc inside #combine_address_fields and handle BCC this way. But I can't understand, what is the difference between classic bcc field and custom Mandrillbcc_address parameter?
Example:

class AlphabetMailer < ActionMailer::Base
  def a
    mail(from: '[email protected]',
         to: '[email protected]',
         bcc: '[email protected]',
         subject: 'Alpha',
         body: '',
         delivery_method: :mandrill)
  end

  def b
    mail(from: '[email protected]',
         to: '[email protected]',
         bcc_address: '[email protected]',
         subject: 'Beta',
         body: '',
         delivery_method: :mandrill)
  end
end

# AlphabetMailer.a.deliver_now and AlphabetMailer.b.deliver_now work the same as expected.

Error with new logger message

NoMethodError: undefined method `error' for nil:NilClass
/bundle/gems/mandrill_dm-1.3.10/lib/mandrill_dm.rb:20:in `configure'

I'm not using rails

global_merge_vars doesn't work

Hello,

my code looks like this:

class RemindersMailer < ActionMailer::Base
  def feedback_email
    mail(
      to: '[email protected]',
      body: '',
      subject: 'Your feedback is important to us!',
      template: 'your-feedback-is-important-to-us',
      global_merge_vars: [
          {
              'name' => 'FNAME',
              'content' => 'Jamie'
          },
          {
              'name' => 'CONSULTANT_NAME',
              'content' => 'Grace'
          }
      ]
    )
  end
end
RemindersMailer.feedback_email.deliver_now

But "global_merge_vars" are in header instead in message. So vars aren't replaced. Could you help me? Request looks like this:

    "template_name": "your-feedback-is-important-to-us",
    "template_content": null,
    "message": {
        "auto_html": null,
        "auto_text": null,
        "bcc_address": null,
        "from_email": "[email protected]",
        "from_name": null,
        "global_merge_vars": null,
        "headers": {
            "Date": "Mon, 18 Dec 2017 17:32:32 +0100",
            "From": "[email protected]",
            "To": [
                "[email protected]"
            ],
            "Bcc": "[email protected]",
            "Message-ID": "<[email protected]>",
            "Subject": "Your feedback is important to us!",
            "Mime-Version": "1.0",
            "Content-Type": "text/plain",
            "Content-Transfer-Encoding": "7bit",
            "template": "your-feedback-is-important-to-us",
            "global-merge-vars": "{\"name\"=>\"FNAME\", \"content\"=>\"Jamie\"}, {\"name\"=>\"CONSULTANT_NAME\", \"content\"=>\"Grace\"}"
        },
        "html": null,
        "important": false,
        "inline_css": null,
        "merge": null,
        "merge_language": null,
        "merge_vars": null,
        "metadata": null,
        "preserve_recipients": null,
        "return_path_domain": null,
        "signing_domain": null,
        "subaccount": null,
        "subject": "Your feedback is important to us!",
        "tags": {},
        "text": "",
        "to": [
            {
                "email": "[email protected]",
                "name": null,
                "type": "to"
            },
            {
                "email": "[email protected]",
                "name": null,
                "type": "bcc"
            }
        ],
        "track_clicks": null,
        "track_opens": null,
        "tracking_domain": null,
        "url_strip_qs": null,
        "view_content_link": null
    },
    "async": false,
    "ip_pool": null,
    "send_at": null,
    "key": "xyz"
}
mandrill_dm 1.3.4
rails 5.1.4
mail 2.7.0
mandrill-api 1.0.53

Maybe I'm using it in wrong way? Could you help me?

New release ?

Can we please get a new release with the latest ip_pool feature ?

instance_variable_get issue on mail 2.5.4

Hi,

sorry it took some time to figure out what was the real issue what I mentioned. At last I caught it when I tested this new things under our webpage. I didn't realise it when I downloaded the new codebase that the mail gem was updated and that's why I couldn't see the issue anymore. So here is some debugging:

mail 2.5.4

Run options: include {:locations=>{"./spec/mandrill_dm/message_spec.rb"=>[120]}}
>>>>>>>>>>>>>>mail: #<Mail::Message:70360916361720, Multipart: false, Headers: <global-merge-vars: {"name"=>"TESTVAR", "content"=>"testcontent"}, {"name"=>"TESTVAR2", "content"=>"testcontent2"}>>
>>>>>>>>>>>>>>mail[field]: #<Mail::Field:0x007ffc5a1987b0 @field=#<Mail::OptionalField:0x007ffc5a15a438 @errors=[], @charset=#<Encoding:UTF-8>, @name="global-merge-vars", @length=nil, @tree=nil, @element=nil, @value="{\"name\"=>\"TESTVAR\", \"content\"=>\"testcontent\"}, {\"name\"=>\"TESTVAR2\", \"content\"=>\"testcontent2\"}">>
>>>>>>>>>>>>>>mail[field].instance_variable_get('@value'): nil
F

Failures:

  1) MandrillDm::Message#global_merge_vars takes an array of multiple global_merge_vars
     Failure/Error: expect(message.global_merge_vars).to eq(global_merge_vars)

       expected: [{"name"=>"TESTVAR", "content"=>"testcontent"}, {"name"=>"TESTVAR2", "content"=>"testcontent2"}]
            got: nil

       (compared using ==)
     # ./spec/mandrill_dm/message_spec.rb:127:in `block (3 levels) in <top (required)>'

Finished in 0.01995 seconds (files took 0.32437 seconds to load)
1 example, 1 failure
mail 2.6.3

Run options: include {:locations=>{"./spec/mandrill_dm/message_spec.rb"=>[120]}}
>>>>>>>>>>>>>>mail: #<Mail::Message:70212615828160, Multipart: false, Headers: <global-merge-vars: {"name"=>"TESTVAR", "content"=>"testcontent"}, {"name"=>"TESTVAR2", "content"=>"testcontent2"}>>
>>>>>>>>>>>>>>mail[field]: #<Mail::Field 0x7fb74c0dfdf8 @name="global-merge-vars" @value=[{"name"=>"TESTVAR", "content"=>"testcontent"}, {"name"=>"TESTVAR2", "content"=>"testcontent2"}] @raw_value=nil @charset=nil @field=#<Mail::OptionalField:0x007fb74c0de6d8 @errors=[], @charset=#<Encoding:UTF-8>, @name="global-merge-vars", @length=nil, @tree=nil, @element=nil, @value="{\"name\"=>\"TESTVAR\", \"content\"=>\"testcontent\"}, {\"name\"=>\"TESTVAR2\", \"content\"=>\"testcontent2\"}">>
>>>>>>>>>>>>>>mail[field].instance_variable_get('@value'): [{"name"=>"TESTVAR", "content"=>"testcontent"}, {"name"=>"TESTVAR2", "content"=>"testcontent2"}]
.

Finished in 0.00257 seconds (files took 0.24934 seconds to load)
1 example, 0 failures

So on mail 2.5.4 the [] is missing from the @vaule what is cause the problem. Or actually here the value just a json like version, but on the new mail you get the normal value and a json like one as well.

The real problem is that the actionmailer still using mail 2.5 (Rails 4.2.5 >> mail >= 2.5.4, ~> 2.5). So this issue is affected in all Rails version!

Sending HTML emails broken

I ran into an issue when sending emails generated from HTML-only templates (they are being delivered as plain text) and tracked it back to this commit:

2a80783

I will try to make a PR to fix this in coming days.

Mailer is looking for templates despite I want to use templates in Mandrill [Rails 5]

I have a template in Mandrill that I want to use as a replacement for template from Devise. I created a 'UserMailer' and set mandrill_dm gem.

MandrillDm.configure do |config|
  config.api_key = ENV['MANDRILL_API_KEY']
end
  config.action_mailer.perform_deliveries = true
  config.action_mailer.delivery_method = :mandrill
class UserMailer < Devise::Mailer   # or ActionMailer::Base, still doesn't work
  helper :application
  include Devise::Controllers::UrlHelpers

  def reset_password_instructions(user, token, options = {})
    mail(
      to: user.email,
      from: "no-reply@#{ENV['SMTP_DOMAIN']}",
      template: 'reset-password-message',
      merge_language: 'mailchimp',
      global_merge_vars: [
        {
          name: 'reset_password_url',
          content: edit_password_url(user, reset_password_token: token)
        }
      ]
      # or per rcpt merge_vars: [ rcpt: "", vars: [{name:'',content:''},{name:'',content:''}]]
    )
  end
end
Devise.setup do |config|
    config.mailer = 'UserMailer'
end

I have a template published in Mandrill: 'reset-password-message'

I get this error message:

An ActionView::MissingTemplate occurred in passwords#create:

  Missing template user_mailer/reset_password_instructions with "mailer". Searched in:
  * "user_mailer"

I obviously don't have this template in my app, the whole point is to use template in Mandrill.

Mail UnstructuredField undefined method `formatted'

I'm a bit lost about where to report this. It's only happening if using Rails ActionMailer with an email that includes commas.

This is how to simulate the error:

MandrillDm::Message.new(Mail::Message.new(to: "Juan Monigote, Pepe <[email protected]>")).send :combine_address_fields
=> NoMethodError: undefined method `formatted' for #<Mail::UnstructuredField:0x007facda5daa50>
MandrillDm::Message.new(Mail::Message.new(to: "Juan, Pepe <[email protected]>")).send :combine_address_fields
=> ...Works

It happens if you do this:

class ApplicationMailer < ActionMailer::Base
  def generic_mail(email)
    mail(
      to: "Juan Monigote, Pepe <[email protected]>",
      subject: "test"
    )
  end

The value from to comes from an string. I'm replying to a received email, so the email in fact arrives with a ,.

Logging

Guys, what do you think if we add logging system to this library? Sometimes it's worth to now what's going on at low level interaction with Mandrill API. Unfortunately, the api-mandrill gem itself doesn't have ability to log it's work. I know that's not very appropriate abstraction level to do such things in mandrill_dm, but maybe we could at least log api call parameters and concise result.
If you find this idea reasonable, let's proceed the discussion.
Also I'm happy to contribute, but I'd like to be referred to good example of logging in other libraries.

Async?

How does the async = true config work? Is it supposed to batch the new message POSTs to Mandrill?

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.