<commit_before>MailGate [![Build Status](https://secure.travis-ci.org/dewski/mail_gate.png)](http://travis-ci.org/dewski/mail_gate)
========

MailGate is an additional delivery method for the [Mail](https://github.com/mikel/mail) gem that lets you restrict the delivery of mail to only whitelisted emails. Ideal for staging environments and other situations where you may not want users of your application to recieve emails. Works with

## Installation

Add this line to your application's Gemfile:

    gem 'mail_gate'

And then run:

    $ bundle

Or install it yourself as:

    $ gem install mail_gate

## Usage

MailGate works as a standalone extension to the [Mail](https://github.com/mikel/mail) gem or as a delivery method within Rails applications.

An example use case would be if you're running say, CNN.com and you have a commenting system in place where once a comment is added to a post the author and other commenters are notified about your comment. You wouldn't want users to see recieve notifications from activity on your staging site, right? Your existing `config/environments/staging.rb` may look something like:

```ruby
CNN::Application.configure do
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    :address        => 'smtp.sendgrid.net',
    :port           => '25',
    :authentication => :plain,
    :user_name      => ENV['SENDGRID_USERNAME'],
    :password       => ENV['<PASSWORD>'],
    :domain         => ENV['SENDGRID_DOMAIN']
  }
end
```

Now just change your `delivery_method` to use `:mail_gate` then move your settings to `:delivery_settings` like shown below.

```ruby
CNN::Application.configure do
  config.action_mailer.delivery_method = :mail_gate
  config.action_mailer.mail_gate_settings = {
    :whitelist        => /cnn.com/,
    :subject_prefix   => '[Staging] ',
    :delivery_method  => :smtp,
    :delivery_settings => {
      :address        => 'smtp.sendgrid.net',
      :port           => '25',
      :authentication => :plain,
      :user_name      => ENV['SENDGRID_USERNAME'],
      :password       => ENV['<PASSWORD>'],
      :domain         => ENV['SENDGRID_DOMAIN']
    }
  }
end
```

## Contributing

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Added some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request

## Copyright
Copyright © 2012 <NAME>. See [MIT-LICENSE](https://github.com/dewski/mail_gate/blob/master/LICENSE) for details.<commit_msg>Update readme to include example within an application<commit_after>MailGate [![Build Status](https://secure.travis-ci.org/dewski/mail_gate.png)](http://travis-ci.org/dewski/mail_gate)
========

MailGate is an additional delivery method for the [Mail](https://github.com/mikel/mail) gem that lets you restrict the delivery of mail to only whitelisted emails. Ideal for staging environments and other situations where you may not want users of your application to recieve emails. Works with

## Installation

Add this line to your application's Gemfile:

    gem 'mail_gate'

And then run:

    $ bundle

Or install it yourself as:

    $ gem install mail_gate

## Usage

MailGate works as a standalone extension to the [Mail](https://github.com/mikel/mail) gem or as a delivery method within Rails applications.

An example use case would be if you're running say, CNN.com and you have a commenting system in place where once a comment is added to a post the author and other commenters are notified about your comment. You wouldn't want users to see recieve notifications from activity on your staging site, right? Your existing `config/environments/staging.rb` may look something like:

```ruby
CNN::Application.configure do
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    :address        => 'smtp.sendgrid.net',
    :port           => '25',
    :authentication => :plain,
    :user_name      => ENV['SENDGRID_USERNAME'],
    :password       => ENV['<PASSWORD>'],
    :domain         => ENV['SENDGRID_DOMAIN']
  }
end
```

Now just change your `delivery_method` to use `:mail_gate` then move your settings to `:delivery_settings` like shown below.

```ruby
CNN::Application.configure do
  config.action_mailer.delivery_method = :mail_gate
  config.action_mailer.mail_gate_settings = {
    :whitelist        => /cnn.com/,
    :subject_prefix   => '[Staging] ',
    :delivery_method  => :smtp,
    :delivery_settings => {
      :address        => 'smtp.sendgrid.net',
      :port           => '25',
      :authentication => :plain,
      :user_name      => ENV['SENDGRID_USERNAME'],
      :password       => <PASSWORD>['<PASSWORD>'],
      :domain         => ENV['SENDGRID_DOMAIN']
    }
  }
end
```

Email now sent within the staging environment will extract any emails that don't match the whitelist. If after being filtered there are no more recipients because they were filtered out, no email will be sent:

```
> Article.first.comments.each do |comment|
>   "Email for: #{comment.user.email}"
>   ArticleMailer.new_comment(article, comment).deliver
> end
=> Email for: <EMAIL>
=> Email for: <EMAIL>
=> Email for: <EMAIL>
=> #<Mail::Message:70236177475420, Multipart: false, Headers: <From: <EMAIL>>, <To: <EMAIL>>, <Cc: >, <Bcc: >, <Subject: [staging] New comment on your article>>
```

## Contributing

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Added some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request

## Copyright
Copyright © 2012 <NAME>. See [MIT-LICENSE](https://github.com/dewski/mail_gate/blob/master/LICENSE) for details.
{'fraction_non_alphanumeric': 0.1155247181266262, 'fraction_numerical': 0.006938421509106678, 'mean_word_length': 3.672609400324149, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 61, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1132608', 'n_tokens_mistral': 1807, 'n_tokens_neox': 1720, 'n_words': 586}