[
  {
    "path": ".github/workflows/ruby.yml",
    "content": "name: ruby\n\non: pull_request\n\njobs:\n  test:\n    runs-on: ubuntu-latest\n    steps:\n      - uses: actions/checkout@v4\n      - name: Set up Ruby\n        uses: ruby/setup-ruby@v1\n        with:\n          ruby-version: head\n      - name: Test\n        run: |\n          bundle\n          bundle exec rspec\n"
  },
  {
    "path": ".gitignore",
    "content": "*.gem\n"
  },
  {
    "path": "Gemfile",
    "content": "source \"https://rubygems.org\"\n\ngemspec\n\ngem \"rspec\"\n"
  },
  {
    "path": "LICENSE",
    "content": "The MIT License\n\nCopyright (c) Dan Croak\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.\n"
  },
  {
    "path": "README.md",
    "content": "# recipient_interceptor\n\nUse this [Ruby gem](https://rubygems.org/gems/recipient_interceptor)\nto avoid emailing your users from non-production environments.\n\n```ruby\n# Gemfile\ngem \"recipient_interceptor\"\n\n# config/environments/staging.rb\nMail.register_interceptor(\n  RecipientInterceptor.new(\"staging@example.com\")\n)\n\n# config/environments/production.rb\nMy::Application.configure do\n  config.action_mailer.delivery_method = :smtp\n  config.action_mailer.smtp_settings = {\n    address: ENV.fetch(\"SMTP_ADDRESS\"), # example: \"smtp.sendgrid.net\"\n    authentication: :plain,\n    domain: ENV.fetch(\"SMTP_DOMAIN\"), # example: \"heroku.com\"\n    enable_starttls_auto: true,\n    password: ENV.fetch(\"SMTP_PASSWORD\"),\n    port: \"587\",\n    user_name: ENV.fetch(\"SMTP_USERNAME\")\n  }\nend\n```\n\nEmail will be intercepted and delivered to the provided address with\nheaders `X-Intercepted-To`, `X-Intercepted-Cc`, and `X-Intercepted-Bcc` added.\n\n## Configuration options and examples\n\nDeliver intercepted email to multiple email addresses:\n\n```ruby\nMail.register_interceptor(\n  RecipientInterceptor.new([\"one@example.com\", \"two@example.com\"])\n)\n```\n\nUse a comma-delimited string:\n\n```ruby\nMail.register_interceptor(\n  RecipientInterceptor.new(\"one@example.com,two@example.com\")\n)\n```\n\nUse an environment variable:\n\n```ruby\n# heroku config:set EMAIL_RECIPIENTS=\"one@example.com,two@example.com\" --app staging\nMail.register_interceptor(\n  RecipientInterceptor.new(ENV[\"EMAIL_RECIPIENTS\"])\n)\n```\n\nPrefix the subject line with static text:\n\n```ruby\nMail.register_interceptor(\n  RecipientInterceptor.new(\n    ENV[\"EMAIL_RECIPIENTS\"],\n    subject_prefix: \"[staging]\",\n  ),\n)\n```\n\nPrefix the subject line with contents from the original message:\n\n```ruby\nMail.register_interceptor(\n  RecipientInterceptor.new(\n    ENV[\"EMAIL_RECIPIENTS\"],\n    subject_prefix: proc { |msg| \"[staging] [#{(msg.to + msg.cc + msg.bcc).sort.join(\",\")}]\" }\n  ),\n)\n```\n\nThe object passed to the proc is an instance of\n[`Mail::Message`](https://www.rubydoc.info/github/mikel/mail/Mail/Message).\n\n## Alternatives\n\n- [Postmark's Sandbox mode](https://postmarkapp.com/developer/user-guide/sandbox-mode/server-sandbox-mode)\n\n## Contributing\n\nFork the repo.\n\n```\nbundle\nbundle exec rspec\n```\n\nMake a change.\nRun tests.\nOpen a pull request.\nDiscuss/address any feedback with maintainer.\nMaintainer will merge.\n"
  },
  {
    "path": "lib/recipient_interceptor.rb",
    "content": "require \"mail\"\n\nclass RecipientInterceptor\n  def initialize(recipients, opts = {})\n    @recipients = if recipients.respond_to?(:split)\n      recipients.split(\",\")\n    else\n      recipients\n    end\n\n    @subject_prefix = opts[:subject_prefix]\n  end\n\n  def delivering_email(msg)\n    if @subject_prefix.respond_to?(:call)\n      msg.subject = \"#{@subject_prefix.call(msg)} #{msg.subject}\"\n    elsif @subject_prefix\n      msg.subject = \"#{@subject_prefix} #{msg.subject}\"\n    end\n\n    msg.header[\"X-Intercepted-To\"] = msg.to || []\n    msg.header[\"X-Intercepted-Cc\"] = msg.cc || []\n    msg.header[\"X-Intercepted-Bcc\"] = msg.bcc || []\n\n    msg.to = @recipients\n    msg.cc = nil if msg.cc\n    msg.bcc = nil if msg.bcc\n  end\nend\n"
  },
  {
    "path": "recipient_interceptor.gemspec",
    "content": "Gem::Specification.new do |spec|\n  spec.add_dependency \"mail\"\n  spec.authors = [\"Dan Croak\"]\n\n  spec.description = <<-STRING\n    Avoid emailing your users from non-production environments.\n  STRING\n\n  spec.files = [\"lib/recipient_interceptor.rb\"]\n  spec.homepage = \"http://github.com/croaky/recipient_interceptor\"\n  spec.license = \"MIT\"\n  spec.name = \"recipient_interceptor\"\n  spec.require_paths = [\"lib\"]\n  spec.summary = \"Intercept recipients when delivering email with the Mail gem.\"\n  spec.version = \"0.3.3\"\nend\n"
  },
  {
    "path": "spec/recipient_interceptor_spec.rb",
    "content": "require File.join(File.dirname(__FILE__), \"..\", \"lib\", \"recipient_interceptor\")\n\ndescribe RecipientInterceptor do\n  before do\n    Mail.defaults do\n      delivery_method :test\n    end\n\n    module Mail\n      @@delivery_interceptors = []\n    end\n  end\n\n  it \"overrides to/cc/bcc fields and adds custom headers\" do\n    Mail.register_interceptor(\n      RecipientInterceptor.new(\"staging@example.com\")\n    )\n\n    mail = Mail.deliver {\n      from \"from@example.com\"\n      to \"to@example.com\"\n      cc \"cc@example.com\"\n      bcc \"bcc@example.com\"\n      subject \"some subject\"\n    }\n\n    expect(mail.to).to eq [\"staging@example.com\"]\n    expect(mail.cc).to eq nil\n    expect(mail.bcc).to eq nil\n    expect(mail.header[\"X-Intercepted-To\"].to_s).to eq \"to@example.com\"\n    expect(mail.header[\"X-Intercepted-Cc\"].to_s).to eq \"cc@example.com\"\n    expect(mail.header[\"X-Intercepted-Bcc\"].to_s).to eq \"bcc@example.com\"\n  end\n\n  it \"overrides to/cc/bcc even if they were already missing\" do\n    Mail.register_interceptor(\n      RecipientInterceptor.new(\"staging@example.com\")\n    )\n\n    mail = Mail.deliver {\n      from \"from@example.com\"\n      to \"to@example.com\"\n    }\n\n    expect(mail.to).to eq [\"staging@example.com\"]\n    expect(mail.cc).to eq nil\n    expect(mail.bcc).to eq nil\n  end\n\n  it \"accepts an array of recipients\" do\n    Mail.register_interceptor(\n      RecipientInterceptor.new([\"one@example.com\", \"two@example.com\"])\n    )\n\n    mail = Mail.deliver {\n      from \"from@example.com\"\n      to \"to@example.com\"\n      cc \"cc@example.com\"\n      bcc \"bcc@example.com\"\n      subject \"some subject\"\n    }\n\n    expect(mail.to).to eq [\"one@example.com\", \"two@example.com\"]\n  end\n\n  it \"accepts a comma-delimited list of recipients\" do\n    Mail.register_interceptor(\n      RecipientInterceptor.new(\"one@example.com,two@example.com\")\n    )\n\n    mail = Mail.deliver {\n      from \"from@example.com\"\n      to \"to@example.com\"\n      subject \"some subject\"\n    }\n\n    expect(mail.to).to eq [\"one@example.com\", \"two@example.com\"]\n  end\n\n  it \"does not prefix subject by default\" do\n    Mail.register_interceptor(\n      RecipientInterceptor.new(\"staging@example.com\")\n    )\n\n    mail = Mail.deliver {\n      from \"from@example.com\"\n      to \"to@example.com\"\n      subject \"some subject\"\n    }\n\n    expect(mail.subject).to eq \"some subject\"\n  end\n\n  it \"prefixes subject with string\" do\n    Mail.register_interceptor(\n      RecipientInterceptor.new(\n        \"staging@example.com\",\n        subject_prefix: \"[staging]\"\n      )\n    )\n\n    mail = Mail.deliver {\n      from \"from@example.com\"\n      to \"to@example.com\"\n      subject \"some subject\"\n    }\n\n    expect(mail.subject).to eq \"[staging] some subject\"\n  end\n\n  it \"prefixes subject with proc\" do\n    Mail.register_interceptor(\n      RecipientInterceptor.new(\n        \"staging@example.com\",\n        subject_prefix: proc { |msg| \"[staging] [#{(msg.to + msg.cc + msg.bcc).sort.join(\",\")}]\" }\n      )\n    )\n\n    mail = Mail.deliver {\n      from \"from@example.com\"\n      to [\"to1@example.com\", \"to2@example.com\"]\n      cc \"cc@example.com\"\n      bcc \"bcc@example.com\"\n      subject \"some subject\"\n    }\n\n    expect(mail.subject).to eq \"[staging] [bcc@example.com,cc@example.com,to1@example.com,to2@example.com] some subject\"\n  end\nend\n"
  }
]