Rails App on Heroku Sending Email Error - ruby-on-rails-3

I have my own domain name hosted on Google Apps.
My email sending works in development mode on my laptop.
In production on Heroku (cedar stack) I get this error:
Net::SMTPAuthenticationError (535-5.7.8 Username and Password not accepted. Learn more at app/controllers/applicants_controller.rb:16:in `create'):
Here is my production.rb file:
config.action_mailer.default_url_options = { :host => 'mydomain.com' }
# ActionMailer Config
# Setup for production - deliveries, no errors raised
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.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "mydomain.com",
authentication: "plain",
enable_starttls_auto: true,
user_name: "info#mydomain.com",
password: "mypassword"
}
Here is the create action in the contacts_controller:
def create
#contact = Contact.new(params[:contact])
respond_to do |format|
if #contact.save
ContactMailer.new_contact_notice(#contact).deliver
format.html { redirect_to(:root, :notice => 'Thanks, Your information was successfully sent.') }
else
format.html { render :action => "show" }
end
end
end
Here is the full section of Heroku's logs indicating the error:
2013-05-07T05:01:54.773576+00:00 app[web.1]: Started POST "/applicants" for 108.208.197.181 at 2013-05-07 05:01:54 +0000
2013-05-07T05:01:55.132454+00:00 app[web.1]: app/controllers/applicants_controller.rb:18:in `block in create'
2013-05-07T05:01:55.130426+00:00 app[web.1]:
2013-05-07T05:01:55.132454+00:00 app[web.1]:
2013-05-07T05:01:55.130426+00:00 app[web.1]: Sent mail to name#gmail.com (185ms)
2013-05-07T05:01:55.132454+00:00 app[web.1]:
2013-05-07T05:01:55.132454+00:00 app[web.1]: Net::SMTPAuthenticationError (535-5.7.8 Username and Password not accepted. Learn more at
2013-05-07T05:01:55.132454+00:00 app[web.1]: app/controllers/applicants_controller.rb:16:in `create'
2013-05-07T05:01:55.132454+00:00 app[web.1]: ):
2013-05-07T05:01:55.132454+00:00 app[web.1]:
I've tried some of the things I have read about this error, such as loggin into the gmail account first. I don't see anywhere in settings where it asks you if you are trying to login so I can confirm that.
Any other ideas?

It took my three days but I found the answer. I had checked and rechecked the mail settings everywhere except in the setup_mail.rb in config/initializers. That was it. Even though I had all the mail settings correct in my production.rb, application.yml and mailer files. I only had the username "name" instead of the full email address "name#domain.com".

Related

Net::SMTPAuthenticationError on Heroku with Google Apps and Rails 3 Inconstant Failure

I have been attempting to set up my rails application on heroku with automated mailings using ActionMailer. My setup for the mail initialization is as follows: Note that the domain and usernames are dummies.
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "domain.edu",
:user_name => "name#wesleyan.edu",
:password => ENV['EMAIL_PASS'],
:authentication => "plain",
:enable_starttls_auto => true
}
The odd thing is, when I try to send mail with one account, I get no issues, then when I try with another, I get the error. I never went through any authentication process with either. Both reports are as follows:
Success:
2013-06-12T00:59:34.801877+00:00 app[web.1]: Sent mail to recipient#domainn.edu (2464ms)
2013-06-12T00:59:34.804391+00:00 app[web.1]: Redirected to http://test.herokuapp.com/
2013-06-12T00:59:34.806552+00:00 app[web.1]: Completed 302 Found in 3394ms (ActiveRecord: 289.6ms)
Failure:
2013-06-12T01:29:25.124493+00:00 app[web.1]: Rendered agreement_mailer/welcome_email.html.erb (23.7ms)
2013-06-12T01:29:26.079394+00:00 app[web.1]: app/controllers/agreements_controller.rb:106:in `block in create'
2013-06-12T01:29:26.079394+00:00 app[web.1]: ):
2013-06-12T01:29:26.078426+00:00 app[web.1]: Sent mail to madelman#wesleyan.edu (929ms)
2013-06-12T01:29:26.078426+00:00 app[web.1]: Completed 500 Internal Server Error in 1137ms
2013-06-12T01:29:26.079394+00:00 app[web.1]:
2013-06-12T01:29:26.079394+00:00 app[web.1]: Net::SMTPAuthenticationError (535-5.7.8 Username and Password not accepted. Learn more at
Any thoughts about why I would get this discrepancy? Thanks so much.
Apparently the issue was that the second email address wasn't a real address at all, but an alias, so I could never get to the real password. I contacted my administrator, and they are fixing the problem.

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.

rails 3.1, Why I can't send email using ActionMailer?

I have problem in my heroku app, I am trying to send email using my Godaddy email account, here is my settings in the environment/production.rb:
config.active_support.deprecation = :notify
config.action_mailer.default_url_options = { :host => 'coursebuilder2.heroku.com' }
config.action_mailer.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => "smtpout.secureserver.net",
:port => 25,
:user_name => "myemail#domain.com",
:password => "password",
:authentication => :login
}
config.action_mailer.raise_delivery_errors = true
but, here is what I get:
Rendered instructors/mailer/confirmation_instructions.html.erb (1.1ms)
Completed 500 Internal Server Error in 666ms
SocketError (getaddrinfo: Name or service not known):
cache: [POST /instructors] invalidate, pass
So, what's I am missing here ?
EDIT
I have fixed the settings above, but, now, I have a timeout error below:
2012-03-04T04:44:20+00:00 app[web.1]: Rendered instructors/mailer/confirmation_instructions.html.erb (0.4ms)
2012-03-04T04:44:50+00:00 heroku[router]: Error H12 (Request timeout) -> POST coursebuilder2.herokuapp.com/instructors dyno=web.1 queue= wait= service=30000ms status=503 bytes=0
2012-03-04T04:45:20+00:00 app[web.1]: Completed 500 Internal Server Error in 60414ms
2012-03-04T04:45:20+00:00 app[web.1]:
2012-03-04T04:45:20+00:00 app[web.1]:
2012-03-04T04:45:20+00:00 app[web.1]: Timeout::Error (Timeout::Error):
2012-03-04T04:45:20+00:00 app[web.1]:
2012-03-04T04:45:20+00:00 app[web.1]:
2012-03-04T04:45:20+00:00 app[web.1]: cache: [POST /instructors] invalidate, pass
Any one can advice ?
That looks like you're using the wrong connection details to GoDaddy - to be honest, I'd be inclined to use the SendGrid free addon which is widely used and documented.

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