ruby on rails action mailer - confirmation email not being sent - ruby-on-rails-3

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!)

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 }
}

Rails 3 Action Malier renders template and doesn't send emails

It has been asked million times probably but my case is different and I have tried every config option imaginable and tried in production mode as well. The email is rendered but it doesn't appear anywhere. I even bought $15 MockSMPT app to see the emails but nothing goes out it seems.
Here are the configs for MockSMTP:
config.action_mailer.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => "localhost",
:port => 1025,
:domain => "whatever.com"
}
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
I have tried every delivery_method, :test, :file, I am ripping my hair off, nothing helps:
Rendered contact_mailer/contact_email.html.erb (2.1ms)
And this is it.
Code for sending is trivial:
def contact_email(contact, house=nil)
#house=house if house
#contact=contact
mail(to: contact.email, subject: "#{contact.name} new inquiry")
end
Rails 3.2.8
contact_controller.rb:
def create
#contact = Contact.new(params[:contact])
if #contact.valid?
#house=House.find_by_id(params[:house_id])
ContactMailer::contact_email #contact, #house
end
end
You should write:
ContactMailer.contact_email(#contact, #house).deliver
You can read ActionMailer documentation here:
http://api.rubyonrails.org/classes/ActionMailer/Base.html
Your statement just creates Mail::Message object, deliver actually sends the email.

Rails 3 mail not working, however devise mail does works

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.

ActionMailer works on localhost but doesn't in a public server

I built an application with Rails 3 and I made a simple contact us page to send mail through my SMTP server.
The problem is my app can send mail when I run it in my local host (my pc), but it doesn't work when I run the app in the hosted server (hostgator).
The funny stuff is that the smtp server is the same!
This is the config in my localhost(and it works!):
config/environments/developer.rb
# ActionMailer Config
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default :charset => "utf-8"
config/initializers/setup_mail.rb
ActionMailer::Base.smtp_settings = {
:address => "mail.mydomain.org",
:port => 26,
:domain => "app.mydomain.org",
:user_name => "register#app.mydomain.org",
:password => "********",
:authentication => "plain",
:enable_starttls_auto => false
}
The url for my host server is app.mydomain.org, so in the hosted app I changed only this:
config/environments/development.rb
# ActionMailer Config
config.action_mailer.default_url_options = { :host => 'mydomain.org' }
...
In the host server, just for now I run the app with WEBrick in development mode.
And I get a timeout error:
....
Timeout::Error (execution expired):
app/controllers/contact_us_controller.rb:13:in `create'
...
Am I missing somenthing??
EDIT & SOLVED:
Hostgator support staff have just find out the cause of this issue.
In the ActionMailer setup, the :address has to be localhost, and not mail.mydomain.org. So the ActionMailer would be:
ActionMailer::Base.smtp_settings = {
:address => "localhost",
:port => 26,
:domain => "app.mydomain.org",
:user_name => "register#app.mydomain.org",
:password => "********",
:authentication => "plain",
:enable_starttls_auto => false
}
I think you mean development.rb, not developer.rb. This setting only runs in development and assuming you've set RAILS_ENV as production on the server, it will be production.rb, not development.rb, is going to be processed.
If the server is the same on both, you could move this to application.rb (or a script in config/initializers). You might want a different setup for production though, so that it's pointing to localhost. That may also fix the problem here, in case there's some DNS issue or server config preventing the outgoing SMTP request.

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.