Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OverrideRecipientSMTP delivery method #423

Closed
wants to merge 2 commits into from
Closed

Add OverrideRecipientSMTP delivery method #423

wants to merge 2 commits into from

Conversation

croaky
Copy link

@croaky croaky commented Aug 2, 2012

Use the OverrideRecipientSMTP delivery method when you don't want your program
to accidentally send emails to addresses other than the overridden recipient
which you configure.

An example use case is in your web app's staging environment, your development
team will receive all staging emails without accidentally emailing users with
active email addresses in the database.

Use the OverrideRecipientSMTP delivery method when you don't want your program
to accidentally send emails to addresses other than the overridden recipient
which you configure.

An example use case is in your web app's staging environment, your development
team will receive all staging emails without accidentally emailing users with
active email addresses in the database.
@dball
Copy link
Collaborator

dball commented Aug 2, 2012

If you're using ActionMailer, the mail_safe gem does this more flexibly:

https://github.com/myronmarston/mail_safe/

@croaky
Copy link
Author

croaky commented Aug 3, 2012

@dball Thanks for the link. I hadn't seen that one but have seen a number of similar solutions:

https://gist.github.com/138420
https://gist.github.com/2938882
http://blog.smartlogicsolutions.com/2009/04/25/reintroducing-sanitize_email-work-with-production-email-without-fear/
http://henrik.nyh.se/2012/08/abort-mail-delivery-with-rails-3-interceptors/
https://github.com/trampoline/actionmailer_extensions

It's definitely a common need!

It's common enough that I hope it will be accepted into the Mail gem, which I like as the home for this because:

  • it's lower-level than ActionMailer
  • I don't need to install a third-party gem that is more likely to get out of sync with the Mail gem or ActionMailer
  • it's a simple implementation; most of the others involve some dirty monkey-patching

@killthekitten
Copy link

+1! I am the one of "the others involve some dirty monkey-patching"

@bcardarella
Copy link

-1 this doesn't seem like something that should go into the core gem but be extracted into a plugin.

If this is a Rails application you should have a specific config/environments/staging.rb and override the smtp settings so that the production mail server is not being used. I see the reason for the use case, but I would prefer to see the Mail gem modified to allow others to hook into it more easily.

@tinomen
Copy link

tinomen commented Aug 3, 2012

+1

@croaky
Copy link
Author

croaky commented Sep 14, 2012

Nudge @mikel. Your opinions on this pull request?

@mikel
Copy link
Owner

mikel commented Nov 17, 2012

Hi there,

I like this, however, I think we should include the original values some how in the resulting email. ie, setting to, cc and bcc to nil is fine, however, perhaps we should store the original values as X-Override-To, X-Override-Cc and X-Override-Bcc.... that would make for better debugging for the developer as they could open the email and see who the email would have been sent to before it was overridden.

Make this change and appropriate tests and I think we can merge this in.

@croaky
Copy link
Author

croaky commented Nov 17, 2012

Sounds good, @mikel. I've pushed the requested change to 5f01a66. Let me know what you think.

@jeremy
Copy link
Collaborator

jeremy commented Jan 19, 2013

Better yet, leave the message's recipients intact! Check out #477 to set the SMTP envelope recipient directly: mail.smtp_envelope_to = '[email protected]'.

Side note: you can do this on your development machine using Postfix, too. In /etc/postfix/main.cf:

recipient_canonical_classes = envelope_recipient
recipient_canonical_maps = regexp:/etc/postfix/recipient_canonical_map

Create /etc/postfix/recipient_canonical_map containing /./ [email protected] and run postmap /etc/postfix/recipient_canonical_map and postfix reload.

That'll rewrite the recipient on every SMTP envelope to your catchall address.

@jeremy
Copy link
Collaborator

jeremy commented Jan 19, 2013

Also! This is a better fit for an interceptor than a delivery method:

class CanonicalEnvelopeRecipientInterceptor
  def initialize(recipient)
    @recipient = recipient
  end

  def delivering_email(mail)
    mail.smtp_envelope_to = @recipient
  end
end

Mail.register_interceptor CanonicalEnvelopeRecipientInterceptor.new('[email protected]')

@jeremy
Copy link
Collaborator

jeremy commented Jan 23, 2013

Closing in favor of using an interceptor to alter outgoing emails. That can be used with any delivery method, not just SMTP.

@jeremy jeremy closed this Jan 23, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants