Rails 3 ActionMailer Google Apps: Timeout::Error - ruby-on-rails-3

I've been trying to fix this for way too many hours. I looked at Railscast, official Rails Guides, lots of blog posts and none of them help.
I'm trying to send email from my Rails 3 app using ActionMailer 2.2.5 via my Google Apps account. I verified the username and password. I can see on the console the message to be sent. I'm getting a Timeout::Error from the deliver call in my controller.
Can someone shed any light on this?
Here's the code:
# config/environments/development.rb
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:enable_starttls_auto => true,
:tls => true,
:address => 'smtp.gmail.com',
:port => "587",
:authentication => :plain,
:domain => 'test.com',
:user_name => 'user#test.com',
:password => 'mypass'
}
# app/mailers/test_mailer.rb
class PostMailer < ActionMailer::Base
default :from => "no-reply#test.com"
def activate_email( post )
#post = post
mail( :to => post.email,
:subject => "Testing" )
end
end
# app/controllers/test_controller.rb
class TestController < ApplicationController
def create
#post = Post.new( params[:post] )
TestMailer.activate_email( #post ).deliver
end
end

I would try two things. First off make port a number instead of a string:
:port => 587
Also in my code I have the following line before the actionmailer config:
Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
Is there any reason that you are using an old version of actionmailer with rails 3? I would recommend using the version of actionmailer that corresponds to your rails version.
Hope this helps.

Related

Rails Zoho configuration sending mails via console but not from mailer class

I have following smtp configuration on my development.rb
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => "smtp.zoho.com",
:port => 465,
:user_name => "xxx#xxx.com",
:password => "xxxx",
:authentication => :login,
:ssl => true,
:tls => true,
:enable_starttls_auto => true
}
I works when I send via rails console , but gives error of
Net::SMTPAuthenticationError (535-5.7.8 Username and Password not accepted. Learn more at
):
when I send via mailer class.
It seems like gmail is blocking you temporarily while you are trying to reach through controller.
Kindly have a check with this link
https://support.google.com/mail/answer/7126229?visit_id=1-636342470835359266-2568809045&rd=1#cantsignin
Hope this helps.

Rails mailer sending Empty Body on EMail

I have following on config/application.rb
config.action_mailer.default_url_options = { :host => 'xxxxx.com' }
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = false
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.mandrillapp.com",
:port => 587,
:domain => 'xxxxx.com',
:user_name => 'xxx.xxxxx#gmail.com',
:password => 'xxxxxxxxxxxxxxxx',
:authentication => 'plain',
:enable_starttls_auto => true }
And in app/mailers/welcome_mailer.rb
def welcome_email(user)
#user = user
#lang=I18n.locale
if #user.email.present?
begin
headers = {
:subject => welcome_email_subject(#user).to_s,
:from => ADMINISTRATIVE_EMAIL,
:to => user.email
}
mail(headers)
rescue Exception => e
abort
end
end
end
I have a template on /app/views/welcome_mailer/welcome_email.html.erb
I am using this mailer action for sending welcome emails along with confirmation link by using devise.For that I have done the following on /config/initializers/welcome_mailers.rb
module Devise::Models::Confirmable
def send_on_create_confirmation_instructions
if self.email
WelcomeMailer.welcome_email(self).deliver
end
end
def send_reset_password_instructions
generate_reset_password_token! if should_generate_reset_token?
WelcomeMailer.generate_password(self).deliver
end
end
Even though the development I have used same smtp configurations I am getting empty body for the mail sent on production and the same working fine in development(local).By the way my production environment is Amazon EC2. Initally 15 days before I have got the same issue and I solved by changing the smtp account.Now it is not happening in any order.Suggest with your feedback or comments.
You can overwrite the template route in the mailer config:
class WelcomeMailer < ActionMailer::Base
...
self.template_root = "#{RAILS_ROOT}/app/views/mailers" # Rails.root in 3.x
...
end

UserMailer with Devise

am using my custom mailer UserMailer to send emails. It works perfect in development, but in production i get error 500, that is when am trying to retrieve my password on forgetting.
here is what is in the production logs in the production mode
Processing by Devise::PasswordsController#create as HTML
Parameters: {"utf8"=>"✓","authenticity_token"=>"xxxxxxxxxxxxxxx", "user"=>{"email"=>"sam#gmail.com"}, "commit"=>"Send me r$
Completed 500 Internal Server Error in 2ms
NameError (uninitialized constant Devise::UserMailer):
activesupport (3.2.8) lib/active_support/inflector/methods.rb:230:in `block in constantize'
activesupport (3.2.8) lib/active_support/inflector/methods.rb:229:in `each'
activesupport (3.2.8) lib/active_support/inflector/methods.rb:229:in `constantize'
devise (2.1.2) lib/devise.rb:256:in `get'
devise (2.1.2) lib/devise.rb:279:in `mailer'
my mailer configuration. user_mailer.rb
class UserMailer < Devise::Mailer
default :from => "info#xxxxxxxx.com"
def signup_confirmation(user)
#user = user
mail :to => user.email, :subject=> "Thank you for signing with us"
end
# send password reset instructions
def reset_password_instructions(user)
#resource = user
mail(:to => #resource.email, :subject => "Reset password instructions", :tag => 'password-reset', :content_type => "text/html") do |format|
format.html { render "devise/mailer/reset_password_instructions" }
end
end
end
production.rb file
config.action_mailer.default_url_options = { :host => 'https://xxxxxxxxxx.com' }
config.action_mailer.raise_delivery_errors = false
config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => 'info#xxxxxxxx.com',
:user_name => 'yyyyyy',
:password => 'zzzzzzzz',
:authentication => 'plain',
:openssl_verify_mode => 'none',
:enable_starttls_auto => true
}
your assistance will be highly appreciated
Okay from the looks of it your mailer looks wrong. Especially the set up. By default you can create a mailer like this:
class UserMailer < ActionMailer::Base
default :from => DEFAULT_FROM
def registration_confirmation(user)
#user = user
#url = "http://localhost:3000/login"
mail(:to => user.email, :subject => "Registered")
end
end
What I did notice in your example is that your doing:
class UserMailer < Devise::Mailer
Your inheriting from Devise's Mailer when in actual fact you shouldn't have do any of this! You may also want to check your config/initalizers/devise.rb and set theconfig.mailer_sender=example#gmail.com` if you haven't. So what I suggest you doing is make your mailer look like the following:
class UserMailer < ActionMailer::Base
default :from => "info#xxxxxxxx.com"
def signup_confirmation(user)
#user = user
mail :to => user.email, :subject=> "Thank you for signing with us"
end
Also another thing... I noticed that your default url is: config.action_mailer.default_url_options = { :host => 'https://xxxxxxxxxx.com' } there is no need for https so it should really look like config.action_mailer.default_url_options = { :host => 'xxxxxxxxxx.com' }. Because when you try to fire anything what will happen is that it will be doing https://https://https://xxxxxxxxxx.com. This is an easy mistake for people to make.
And I also believe the cause of this may be due to the fact you have not set the class that is responsible for sending your e-mails.
Other possible solution that may fix your problem
Notice that in config/intializers/devise.rb that there is the following line which is commented out:
# Configure the class responsible to send e-mails.
# config.mailer = "Devise::Mailer"
Uncomment this and set this to your class you are using which in your example so that it would be
config.mailer = "UserMailer" # UserMailer is your mailer class
Also in app/mailers/user_mailer.rb you should have:
class UserMailer < ActionMailer::Base
include Devise::Mailers::Helpers
default from: "default#mydomain.com"
def confirmation_instructions(record)
devise_mail(record, :confirmation_instructions)
end
def reset_password_instructions(record)
devise_mail(record, :reset_password_instructions)
end
def unlock_instructions(record)
devise_mail(record, :unlock_instructions)
end
# you can then put any of your own methods here
end
May also want to generate your own views:
rails generate devise:views
also move the email templates from app/views/devise/mailer/ to app/views/user_mailer/
mv app/views/devise/mailer/* app/views/user_mailer/
Do everthing #david has said. except change this for the Devise > 3.2.4
class UserMailer < ActionMailer::Base
include Devise::Mailers::Helpers
def confirmation_instructions(record, token, opts={})
#token = token
devise_mail(record, :confirmation_instructions, opts)
end
def reset_password_instructions(record, token, opts={})
#token = token
devise_mail(record, :reset_password_instructions, opts)
end
def unlock_instructions(record, token, opts={})
#token = token
devise_mail(record, :unlock_instructions, opts)
end

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