Rails 3 mail not working, however devise mail does works - ruby-on-rails-3

I have a rails 3.2.3 application that works fine and delivers devise's created mail messages as, registration confirmation and password forget mail messages to set a new password.
However I have a different places that I want to deliver mail notifications.
This is my Notifiermodel.
# encoding: utf-8
class Notifier < ActionMailer::Base
default from: "no-reply#domain.com"
default to: "admin#domain.com"
def new_post_submitted(post)
#post = post
mail(subject: "Anúncio enviado: #{#post.title}")
end
def new_message(message)
#message = message
mail(subject: "Mensagem de contato: #{#message.subject}")
end
end
and the controller calls:
def create
#message = Message.new(params[:message])
if #message.valid?
Notifier.new_message(#message).deliver
redirect_to(root_path, :notice => "Obrigado. Sua Mensagem enviada com sucesso")
else
flash.now.alert = "Por favor preencha todos os campos."
render :new
end
end
The log output says it was delivered:
Started POST "/contato" for 187.57.102.168 at 2012-08-31 11:28:51 +0900
Processing by ContactController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"TVdmDYCA4x3JVi+9pMcpY7OSU/P1lE9enLiFJlK9W1M=", "message"=>{"name"=>"kleber", "email"=>"test#gmail.com", "subject"=>"teste", "body"=>"hello there"}, "commit"=>"Enviar"}
Rendered notifier/new_message.html.erb (0.1ms)
Sent mail to admin#domain.com (152ms)
Redirected to http://www.domain.com/
Completed 302 Found in 158ms (ActiveRecord: 0.0ms)
And the following configuration on my config/production.rbfile.
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => "localhost",
:port => 25,
:domain => "domain.com",
:authentication => :login,
:user_name => "admin#domain.com",
:password => "mypassword",
:enable_starttls_auto => false
}
Any clue on what is happening here?

Here are the settings that work in my production.rb file.
ActionMailer::Base.smtp_settings = {
:address => 'mail.domain.com',
:port => 587,
:domain => 'domain.com',
:authentication => :login,
:user_name => 'postmaster#domain.com',
:password => 'password'
}
Not sure you have to specify delivery_method?
ActionMailer::Base.delivery_method = :smtp
I don't have that in my config and everything works properly.
In the rails API http://api.rubyonrails.org/ ActionMailer section there is an option to set a delivery error.
raise_delivery_errors - Whether or not errors should be raised if the email fails to be delivered.
You could try that to further troubleshoot the problem.

First, make sure you haven't turned off deliveries somewhere else in the environment file:
# the default is true anyway, so if you don't see it anywhere in
# the file, you should be okay
config.action_mailer.perform_deliveries = true
I would next try changing the delivery method to either :sendmail or :file. If you have sendmail installed and configured on your system (at least a development system), then you should receive the e-mails you send. I was surprised at one point to discover that sendmail worked out of the box on OS X. I'm not sure if you'd find the same on CentOS.
If you don't have sendmail or don't want to configure it, use the :file delivery method to dump the e-mails to a file on the filesystem.
At this point, if the e-mails don't deliver via sendmail or file, you know there's a problem further up the stack. If they do deliver, then the problem is your SMTP configuration. Try using a known working SMTP server (such as your Gmail account). If that works, then you know it's a problem with the SMTP server running on localhost and not with ActionMailer.

Related

exception_notification 4.0.1 and Rails 3.2.8 not working

I'm trying to get exception_notification and rails to work together. I have looked at a bunch of other questions, and read through the ReadMe heavily on the exception_notification gem page. However, most other tutorials and questions all seem to be out of date by a year or so, before the major version switch to 4.0. What's extra weird is that the proper emails seem to be getting sent when in development mode, but NOT in production mode, which is where I really want them.
Here's the relevant code:
I installed with gem 'exception_notification' as it states in the ReadMe.
then... inside config/environments/production.rb
Whatever::Application.configure do
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:user_name => 'blah#blah.com',
:password => 'blahblah',
:authentication => 'plain',
:enable_starttls_auto => true
}
config.action_mailer.perform_deliveries = true
config.action_mailer.default_url_options = {
:host => 'www.blah.com'
}
ActionMailer::Base.default :from => 'BlahApp <reply#blah.com>'
Paperclip.options[:command_path] = "/usr/bin/"
config.middleware.use ExceptionNotification::Rack,
:email => {
:email_prefix => "[Error!] ",
:sender_address => %{<reply#blah.com>},
:exception_recipients => ['"Test Recipient" <tester#blah.com>', '"Test 2" <test2#gmail.com>'],
}
As I said, when I copy/paste this into development.rb, it seems to work properly and send e-mails (though I'm not actually receiving them, Paperclip is catching them and opening them in the browser, and they have what I want in them). Then in production, nothing happens, and when I check the log files, it doesn't appear to be even attempting to send the e-mail. No errors received, except for the one I purposely throw.
I saw there was some issue related to SMTP stuff, so I also tried directly putting the smtp_settings directly into the email config hash of ExceptionNotification (while still having the same settings outside), but no luck with that.
I feel like I'm missing something silly. It's supposed to be dead simple to use. Any thoughts? Much thanks!
Ok, so the problem was that apparently previous devs (I inherited this project) had already set up a different kind of error catching that simply rendered different error pages. That function only ran in production, and it was blocking the call to exception_notification, which is why it just wasn't doing anything in production, but was working fine in development. So... all I did in the end was this...
In the app... this was already in the controllers/application_controller.rb file:
unless Rails.application.config.consider_all_requests_local
#Commenting this would show user a blank page but also show a detailed error message. Don't do this
#Unless absolutely necessary.
rescue_from Exception, with: lambda { |exception| render_error 500, exception }
rescue_from ActionController::RoutingError, ActionController::UnknownController, AbstractController::ActionNotFound, ActiveRecord::RecordNotFound, with: lambda { |exception| render_error 404, exception }
end
And the render_error function had some just well, rendered the error page. So I added in a step to set off the call to exception_notification like so...
def render_error(status, exception)
if (status != 404)
ExceptionNotifier::Notifier
.exception_notification(exception, options.merge(:env => env))
.deliver
end
respond_to do |format|
format.html { render template: "home/error_#{status}", layout: 'layouts/heropage', status: status }
format.all { render nothing: true, status: status }
end
end
Please add in Gemfile gem 'exception_notification', '4.1.0.rc1' and in your production.rb file
config.consider_all_requests_local = false
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options{host:'http://yourUrl.herokuapp.com/'
}
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: 'gmail.com',
authentication: "plain",
enable_starttls_auto: true,
user_name: "youremail#gmail.com",
password: "Password"
}
config.middleware.use ExceptionNotification::Rack,
:email => {
:email_prefix => "[Notifer] ",
:sender_address => %{"Exception Notification" < sender#domain.com>},
:exception_recipients => %w{first#domian.com second#domain.com }
}

ruby on rails action mailer - confirmation email not being sent

I'm working on a ruby on rails project made mostly by someone else, and my rails knowledge is so-so.
The registration process works like this (or used to, but doesn't seem to now, for some reason):
A person registers with name, email, password. (All done with Devise)
The person is sent an email, asking them to click on a link, and when they do, they are then a registered user and can log in to the app with their name and password.
When they do step 1 and click 'Sign Up' a message comes up on the screen, 'Congratulations! Check your email and click the link.'
But no email is being sent. In my App/config/environments/development.rb I have:
QuestionnaireSite::Application.configure do
# Settings specified here will take precedence over those in config/application.rb
# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false
# Log error messages when you accidentally call methods on nil.
config.whiny_nils = true
# Show full error reports and disable caching
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = false
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
# config.action_mailer.delivery_method = :letter_opener
config.action_mailer.smtp_settings = {
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'gmail.com',
:user_name => 'questionnaire.dev#gmail.com',
:password => 'qwerty_123',
:authentication => 'plain',
:enable_starttls_auto => true
}
# Print deprecation notices to the Rails logger
config.active_support.deprecation = :log
# Only use best-standards-support built into browsers
config.action_dispatch.best_standards_support = :builtin
# Raise exception on mass assignment protection for Active Record models
config.active_record.mass_assignment_sanitizer = :strict
# Log the query plan for queries taking more than this (works
# with SQLite, MySQL, and PostgreSQL)
config.active_record.auto_explain_threshold_in_seconds = 0.5
# Do not compress assets
config.assets.compress = false
# Expands the lines which load the assets
config.assets.debug = true
config.cache_store = :mem_cache_store
end
When I comment out the line:
config.action_mailer.delivery_method = :smtp
and uncomment:
# config.action_mailer.delivery_method = :letter_opener
Everything works as planned (letter_opener is a gem that automates the registration process) so I know there's something going on in this file. As I said, it was working before, and I'm pretty sure those are the only lines I've changed. Is there something else I should be doing?
You need the following in your development.rb file as well as all the other stuff:
config.action_mailer.perform_deliveries = true
Without the above, your app won't actually send the email, and instead will simply display it's contents in your server log... Check the command line output (where rails server is running)
Note: this setting is enabled by default in production, so there's no need to specify it there
First of all, thank you all above for your help and guidance.
#Peter Klipfel - in fact, you were right. I was simply refreshing my app, therefore no error messages with:
config.action_mailer.raise_delivery_errors = false
I had to restart webrick for it to take effect. When that was done I got the error:
Net::SMTPAuthenticationError in Devise::RegistrationsController#create
username and password don't match
As I said, I inherited this project from someone else - questionnaire.dev#gmail probably doesn't exist any more. I changed that part of the code to:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.perform_deliveries = true
config.action_mailer.default :charset => "utf-8"
config.action_mailer.delivery_method = :smtp
# config.action_mailer.delivery_method = :letter_opener
config.action_mailer.smtp_settings = {
:address => 'smtp.gmail.com',
:port => 587,
# in previous Stackoverflow I read :domain part wasn't needed, so leave it out
# :domain => 'gmail.com',
:user_name => 'my_gmail_address#gmail.com',
:password => 'my_gmail_password',
:authentication => 'plain',
:enable_starttls_auto => true
}
And now everything works as it should (for the moment anyway!)

ActionMailer not using correct smtp_settings with ExceptionNotification

For some reason it seems like my smtp settings are not being applied. I have them set up in application.rb,
# application.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "blah.com",
:user_name => "user#blah.com",
:password => "pwd",
:authentication => 'plain',
:enable_starttls_auto => true
}
but when I try to send mail (using ExceptionNotification) I'm getting a connect failed error, and some poking around shows me that the settings used by the mail gem are.
{:address=>"localhost", :port=>25, :domain=>"localhost.localdomain", :user_name=>nil, :password=>nil, :authentication=>nil, :enable_starttls_auto=>true, :openssl_verify_mode=>nil, :ssl=>nil, :tls=>nil}
I've never seen this problem with rails (3.2.7) before, so I suspect it's something I'm doing, but I've grepped for other things modifying the settings and I can't find anything. Anybody have any ideas?
As usual, I spend hours trying to figure out what the problem is, but as soon as I post it on SO, I figure out what's going on :S
I had ExceptionNotifier disabled in dev mode as usual, but the gem is still included, so it still tried to do everything. Apparently the mail configuration was one of the things that wasn't being done.
Solution: enable in dev mode or run in prod.

Actionmailer Emails seem to be sent in the console and logs but they never arrive

Rails 3.0.0
Here is my configuration in environments/production.rb:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "<domain>",
:user_name => "<un>#gmail.com",
:password => "<pw>",
:authentication => "plain",
:enable_starttls_auto => true
}
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
I've also tried commenting out the domain as suggested in a different Stack Overflow post.
I've tried sending the mail both through my app and in the console. In the console there are no errors and it returns the mail object but displays nothing else. When I try sending though the app the log says that it rendered my user mailer template.
I'm hosting my app on Linode but they assure me this method "should" work and that Linode shouldn't be the problem. I've also checked to make sure my app was running in "production."
Any ideas for debugging this? I've tried sending to three different common email hosts.
EDIT: Here is my code for sending a mail in the console:
user_mailer.rb
class UserMailer < ActionMailer::Base
default :from => "myaccount#gmail.com"
layout 'user_layout'
def activation(user)
#user = user
mail(:to => #user.email, :subject => 'Activate your account')
end
end
console command
> u = User.last
> u.email
#myotheraccount#gmail.com
> UserMailer.activation(u).deliver
UPDATE 2: When I add a text only view template, that sends fine. So something is only wrong with sending the HTML versions. I've simplified all of my HTML templates to contain simple tags and the standard doctype html but still no dice.

Rails Cast: ActionMailer Email not sent to inbox

The server states that the email was sent to the correct address but I am not getting the message in my inbox.
My Setup_mail.rb file
ActionMailer::Base.smtp_settings ={
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:user_name => "my_user_name#gmail.com",
:password => "my_password",
:authentication => "Plain",
:enable_starttls_auto => true
}
My development.rb file is:
# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true #default value
config.action_mailer.delivery_method = :smtp #default value
My test.rb file is:
config.action_mailer.delivery_method = :smtp
I have tried multiple variations and am lost. I am running on a Windows 7 machine. I am running Ruby 1.8.7 and Rails 3.0.7
Can anyone help?
Here is my create method:
def create
#user = User.new(params[:user])
if #user.save
UserMailer.registration_confirmation(#user).deliver
sign_in #user
redirect_to #user, :flash => { :success => "Welcome to the Sample App!" }
else
#title = "Sign up"
render 'new'
end
end
My user_mailer.rb
class UserMailer < ActionMailer::Base
default :from => "my_user_name#gmail.com"
def registration_confirmation(user)
mail(:to => user.email, :subject => "Thanks for registering")
end
end
Take a look at your server. I'm pretty sure that you can see in your logs that it's actually trying to send the mail.
The problem is that Google doesn't trust your local IP address and your mails won't get delivered (not even to the spam directory). There is no way to work around this but using a whitelisted server.
If you try your app in production this should normally work, for example deploy your app to heroku to test it.
Try putting in .deliver at the end. That fixed this issue for me:
mail(:to .....).deliver!
Try changing the authentication from a string to a symbol.
ActionMailer::Base.smtp_settings ={
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:user_name => "my_user_name#gmail.com",
:password => "my_password",
:authentication => :plain,
:enable_starttls_auto => true
}
You should check that my_user_name#gmail.com has actually sent the email. We have had issues with this in the past when sending verification emails out through Gmail's SMTP server, since sending in bulk end up not sending at all.
I suggest you log into my_user_name#gmail.com and verify that there are no problems and that the emails are sent.
If not, you may want try a service like Send Grid to send outgoing emails.
I had this same issue, I was able to see that in my console mail was sent but in inbox nothing was appearing, one thing is that you can deploy your app on whitelisted server like heroku, or if you want to see just for testing purpose through your local just enable less secure apps in your browser and you should be able to see that email in your inbox
https://myaccount.google.com/lesssecureapps