In a Rails application I set up a new staging environment with the following parameters in its environments/
file:
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
However, when the system generates an email, it gets printed to the staging.log
file instead of being sent. My SMTP settings work fine in other environments. What configuration am I missing to get the emails to actually send?
Edit: Yes, the staging box is set up with valid configuration for an SMTP server it has access to. It seems like the problem isn't with the SMTP settings (if it was, wouldn't I get errors in the logs?), but with the Rails configuration. The application is still redirecting emails to the log file (saying "Sent mail: ...") as opposed to actually going through SMTP.
Edit #2: It looks like the emails actually have been sending correctly, they just happen to print to the log as well. I'm trying to use the sanitize_email
gem to redirect the mail to another address, and that doesn't seem to be working, which is why I thought the emails weren't going out. So I think that solves my problem, although I'm still curious what in ActionMailer's settings controls whether emails are sent, logged to the log file, or both.
Edit #3: The problem with sanitize_email
boiled down to me needing to add the new staging environment to ActionMailer::Base.local_environments
. I'll keep this question open to see if anyone can answer my last question (what determines whether ActionMailer's emails get sent out, logged to the log file, or both?)
Regarding your third edit, the logging is a function of which log level you have set for the app itself, not any particular setting in ActionMailer
.
In Rails 2.3, ActionMailer::Base
simply send the email to whatever logger has been configured, if any. The recipient is sent to the info
log and the body of the email is sent to the debug
log. (Comments are mine. The rest is straight out of the source code.)
def deliver!(mail = @mail)
raise "no mail object available for delivery!" unless mail
#
# Logging happens first (or not)
#
unless logger.nil?
logger.info "Sent mail to #{Array(recipients).join(', ')}"
logger.debug "\n#{mail.encoded}"
end
#
# And then we decide if an email should really get sent
#
begin
__send__("perform_delivery_#{delivery_method}", mail) if perform_deliveries
rescue Exception => e # Net::SMTP errors or sendmail pipe errors
raise e if raise_delivery_errors
end
return mail
end
Your environment.rb
or staging.rb
file should have a line that controls the log level. Something like the following:
config.log_level = :debug
This is all completely separate from the mailer configuration that you already found, which controls whether the email is sent or not.
config.action_mailer.perform_deliveries = true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With