sometimes when I am developing, I do not have an internet connection. This results in an error wherever my app is supposed to send an email:
getaddrinfo: nodename nor servname provided, or not known
Is there a simple and quick way where i can change a config value to make ActionMailer just not try to actually send out an email and not throw an error? Maybe something thats scoped to the development environment. Or some other way I can avoid the error being thrown and my code passing wherever I call the actionmailer deliver?
I'm using Rails 3.1
It's common practice to just let Rails ignore the mail errors. In your config/environments/development.rb file add, uncomment or modify:
# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = false
You can also set this:
config.action_mailer.perform_deliveries = false
See the documentation here http://edgeguides.rubyonrails.org/action_mailer_basics.html#action-mailer-configuration
You can also set the delivery method to :test, but I have not actually tried that
config.action_mailer.delivery_method = :test
If you want to disable mail deliveries after your rails app has been initialized (while creating sample data, during migrations, etc.):
ActionMailer::Base.perform_deliveries = false
Related
I'm trying to configure airbrake, but can't figure it out. What I wanna achieve is not getting errors from development and test envs only from production.
With the following setup though, I'm getting all the 3 types of error messages as they were occurred in production. So production error sends production error notification but development/test error also sends production error notification.
How can I configure it properly?
# Configures the environment the application is running in. Helps the Airbrake
# dashboard to distinguish between exceptions occurring in different
# environments. By default, it's not set.
# NOTE: This option must be set in order to make the 'ignore_environments'
# option work.
# https://github.com/airbrake/airbrake-ruby#environment
c.environment = :production
# Setting this option allows Airbrake to filter exceptions occurring in
# unwanted environments such as :test. By default, it is equal to an empty
# Array, which means Airbrake Ruby sends exceptions occurring in all
# environments.
# NOTE: This option *does not* work if you don't set the 'environment' option.
# https://github.com/airbrake/airbrake-ruby#ignore_environments
c.ignore_environments = %w(test, development)
You configure your ignored environments like this:
c.ignore_environments = %w(test, development)
# Which is equivalent to:
c.ignore_environments = ['test,', 'development']
The correct way to configure this option is this:
c.ignore_environments = %w(test development)
# Which is equivalent to:
c.ignore_environments = ['test', 'development']
If you use Ruby's %w syntax for arrays, you don't want to use commas.
Another potential issue is that you specify:
c.environment = :production
It will be more robust to use a String (instead of a Symbol) or Rails.env here.
c.environment = Rails.env
I testing a Rails application that sends emails in some situations. It's an API.
For the testing, I'm using the Airborne gem, which makes API testing pretty easy. All went correct except when I had to test the email deliveries. I tried the following:
it "blah" do
//Code that makes my API send an email
puts ActionMailer::Base.deliveries.inspect
end
But deliveries array is always empty. I also tried with Emails.deliveries.inspect. Emails is my custom Mailer that inherits ActionMailer::Base.
I ended reading the API documentation of ActionMailer and met the interceptor concept. Interceptors doesn't work in :test delivery method so I switched to :smtp. In fact, the emails are being sent correctly, but I can not access them on the tests to make expectations.
My interceptor code is this right now
initializers/email_interceptor.rb
class EmailInterceptor
##msgs = []
def self.delivering_email(message)
puts message
//Rails.logger.debug "Email being sent: " + message.to_s
##msgs << message
Rails.logger.debug "Actual messages array: #{##msgs}"
end
def self.msgs
##msgs
end
end
ActionMailer::Base.register_interceptor(EmailInterceptor)
All OK. The debug messages print the array being populated correctly. But the variable is cleaned before my test statement is executed.
EDIT: The code above is executed when I run my test suite. But the variable is empty accessed from the test itself.
//test code
puts EmailInterceptor.msgs.inspect
=> []
Is there any way to prevent this behavior?
You may have config.action_mailer.perform_deliveries = false in your test.rb config. It seems like you should really be using config.action_mailer.delivery_method = :test since this will allow ActionMailer::Base.deliveries to be populated, which makes for easier and more reliable testing. Do you really need interceptors for your tests?
In my project, I'm using Rails 4.1.1 and Ruby 2.1.1. I was reading the mail gem, but wasn't sure how to check if the deliver failed (for any reason).
result = UserMailer.signup.deliver
if result.action == 'failed' or result.bounced?
# How can you tell if a deliver has failed?
# Do stuff here if failed
end
As described in http://guides.rubyonrails.org/action_mailer_basics.html, you can set
config.action_mailer.raise_delivery_errors = true
this way, Rails will raise an error in case the delivery can't take place
I've been trying to send some test newsletter using my gmail account as smtp, but when i tried to send to multiple recipients - ['emai#laddr#email.com, emailaddr2#email.com'] - in this case the first email address is incorrect - it gives me an error 555 - 5.5.2 Syntax error and the process stops without passing through the next email addreses.
My question is:
is there a possibility to bypass those kind of errors in order to skip the incorrect addresses and to continue sending the emails?
You can set ActionMailer to ignore delivery errors, but that's not really considered best practice in a production environment.
# environment.rb (or development/test etc)
ActionMailer::Base.raise_delivery_errors = false
If you don't have a lot of recipients, you could try looping through the array of addresses and sending an email for each one, rescuing a delivery error and adding a message to the log.
# Model
def send_emails(addresses)
addresses.each do |address|
begin
YourMailer.deliver_method(email)
rescue
logger.error "Could not send email to #{email}"
end
end
end
I'm having trouble sending mail using SMTP from a Rails app. I have a Mailer class:
class Mailer < ActionMailer::Base
def newsletter_confirmation(subscription)
recipients "my-valid-email#gmail.com" # this is set to my email
# just for testing purposes and will
# be changed to subscription.email
from "\"my-valid-helo-domain.net\" <noreply#my-valid-helo-domain.net>"
subject "Confirm your subscription"
body :subscription => subscription
end
end
When I try to send the mail, I get a Net::SMTPSyntaxError:
501 <["noreply#my-valid-helo-domain.net"]>: missing or malformed local part
If I comment out the from field, the mail gets delivered ok, but with the from information missing (obviously). Any ideas on what I'm doing wrong?
Thanks.
Edit: I'm using Rails 2.3.2 and Ruby 1.9.1
The error code and the description of the error states that this is an error on the mail server.
I suggest you check the mail servers to pinpoint the error.
When it comes to ActionMailer it is supposed to raise an exception if the configuration parameter raise_delivery_errors is set (default in Production but not in Development I believe), so you can check that one and try to resend if it triggers.
EDIT:
Here is the solution (it's a Ruby/Rails 1.9 bug):
https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/2340-action-mailer-cant-deliver-mail-via-smtp-on-ruby-191
and the patch:
https://rails.lighthouseapp.com/projects/8994/tickets/2340/a/104008/action_mailer-ruby-1.9-from-address-fix.patch
It is a known bug. https://rails.lighthouseapp.com/projects/8994/tickets/2340