Test fails when sending email: An SMTP To address is required to send a message. Set the message smtp_envelope_to, to, cc, or bcc address

actionmailer, capybara, rspec, ruby-on-rails

Solution

The error says you're missing a `to` address. Since you're sending an email to admin, I'm guessing that this email is different based on the Rails env and you have this configured somewhere. Make sure that there's one configured for the test environment.

Problem

I have an after_create callback for the User model that will send an email to the admin so that he can approve the account. It works perfectly in development mode but when I run my Capybara/RSpec tests, it fails with the following exception: ``` ArgumentError: An SMTP To address is required to send a message. Set the message smtp_envelope_to, to, cc, or bcc address. ``` I do have have config.action_mailer.delivery_method set to :test in config/environments/test.rb. Why does it attempt to use SMTP while it is in the test environment? And why does this happen for only this email delivery (which is in the model) but not with other deliveries (using devise gem)? Here is a snippet from the User model: ``` class User < ActiveRecord::Base after_create :send_admin_mail protected def send_admin_mail puts ActionMailer::Base.delivery_method #prints test! ReviewMailer.new_user_waiting_for_approval(self).deliver end end ```

Original source