pboling/sanitize_email

Force sanitize doesn't seem to work

iainbeeston opened this issue · 5 comments

force_sanitize isn't working for me, and neither is the unsanitized block method. When I either set force_sanitize to false, or use unsanitized nothing gets sent (in fact when I run unsanitized the block given doesn't seem to run at all, judging by "puts" statements I put in for debugging). I haven't had a chance to look into why this is happening yet.

unsanitized is not a method (in current code, though it may have been in a previous iteration?). There are two methods that take a block in the latest release, and all these methods are fully spec'd.

I hadn't documented these in the readme yet, so I just did, and now copy that here:

Regardless of the Config settings of SanitizeEmail you can do a local override to force unsanitary email in any environment.

  SanitizeEmail.unsanitary do
    Mail.deliver do
      from      'from@example.org'
      to        'to@example.org' # Will actually be sent to the specified address, not sanitized
      reply_to  'reply_to@example.org'
      subject   'subject'
    end
  end

Regardless of the Config settings of SanitizeEmail you can do a local override to send sanitary email in any environment.
You have access to all the same configuration options in the parameter hash as you can set in the actual
SanitizeEmail.configure block.

  SanitizeEmail.sanitary({:sanitized_to => 'boo@example.com'}) do # these config options are merged with the globals
    Mail.deliver do
      from      'from@example.org'
      to        'to@example.org' # Will actually be sent to the override addresses, in this case: boo@example.com
      reply_to  'reply_to@example.org'
      subject   'subject'
    end
  end

SanitizeEmail.force_sanitize is also spec'd and should be working. The usage has changed however from the 0.X line. Now it is called on the SanitizeEmail module, whereas previously it was called on the your mailer.

Also make sure you are on the latest version.

Sorry, I meant unsanitary (not unsanitized).

I'm still investigating why this is happening, but this is what I've found so far. If I send in an environment that's is not using sanitize_email the email gets delivered normally:

Mail.deliver{from 'me@gmail.com'; to 'you@gmail.com'; subject 'subject'}
=> #<Mail::Message:62042480, Multipart: false, Headers: <Date: Wed, 05 Sep 2012 15:48:33 -0700>, <From: me@gmail.com>, <To: you@gmail.com>, <Message-ID: 5047d6c13a274_2735be0845a9@ea06eb3d-3397-4942-9f72-966084d57f8d.mail>, <Subject: subject>, <Mime-Version: 1.0>, <Content-Type: text/plain>, <Content-Transfer-Encoding: 7bit>>

but if I wrap the same call in sanitary, like this

SanitizeEmail.sanitary({:sanitized_to => 'someone.else@gmail.com'}) { Mail.deliver{from 'me@gmail.com'; to 'you@gmail.com'; subject 'subject'} }
=> nil

then nothing arrives (I send with sendgrid and they're not receiving a request so far as I can tell).

I'll spend some time on this when I get a chance (might be a day or two). I'm not ruling out that it might be something about my environment

I'm using v 1.0.0rc3 - not the latest version 1.0.3. I can try upgrading that shortly

Those block helpers were implemented in v1.0.2 (looking at the CHANGELOG). They may just be stubs in the pre-release version. I think you'll find the current version works :)

Upgrading fixed it. Thanks!

I have also just released v1.0.4 which removes the facets dependency. :)