UserMailer with Devise - ruby-on-rails-3

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

Related

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

email not sent with a dot in the host name default_url_options[:host]

This is a particularly weird problem...
I use Devise and I would like to receive emails when I ask to reset my password.
So I added this line to my development.rb file :
ActionMailer::Base.default_url_options[:host] = "localhost:3000"
This is working great...
But if I change the address and write something with a dot in it, like my production address "xxx.herokuapp.com" no mail is sent. I get no errors, in the log I see a message telling the mail has been sent, but I receive nothing.
I only receive an email if there is no dot ('.') in the host name I provide.
Edit
If I use the default Devise:Mailer, everything works fine.
It must be a problem with my custom Mailer.
Here it is :
Mailer.rb
class Mailer < Devise::Mailer
include Devise::Mailers::Helpers
def headers_for(action, opts={})
headers = {
:subject => t("devise.mailer.#{action}.subject"),
:from => mailer_sender(devise_mapping),
:to => resource.email,
:bcc => "myadresse",
:template_path => template_paths
}
end
def confirmation_instructions(record, opts={})
devise_mail(record, :confirmation_instructions, opts)
end
def reset_password_instructions(record, opts={})
devise_mail(record, :reset_password_instructions, opts)
end
def unlock_instructions(record, opts={})
devise_mail(record, :unlock_instructions, opts)
end
end
As explained here, you probably have to call super like this
def headers_for(action, opts={})
headers = super.merge({
:subject => t("devise.mailer.#{action}.subject"),
:from => mailer_sender(devise_mapping),
:to => resource.email,
:bcc => "myadresse",
:template_path => template_paths
})
end

rails send empty message

Mailer:
class CustomerHistoryMailer < ActionMailer::Base
default :from => "notifications#example.com"
def log_email(to)
mail(:to => to,
:subject => 'Report')
end
end
Controller:
class Admin::ReportsController < ApplicationController
def index
CustomerHistoryMailer.log_email("me#example.com").deliver
end
end
View (app/views/customer_history_mailer/log_email.text.erb):
Hello, world!
In my mailbox I receive empty message with right subject. Why?
Is it possible that you have generated a .html.rb view as well, and rails is detecting both, thus sending a multipart message (being the HTML empty) thus you are not seeing anything because your email client defaults to the HTML view?
Can you verify that?
--- What about specifying the order for html/text in the multipart msg?
class UserMailer < ActionMailer::Base
def welcome_email(user)
#user = user
#url = user_url(#user)
mail(:to => user.email,
:subject => "Welcome to My Awesome Site") do |format|
format.html
format.text
end
end
end
I removed config.action_mailer.deprecation = :log line from config/environments/development.rb and it works now.

How can I customize Devise to send password reset emails using PostMark mailer

I'm trying to get all of my system's email notifications under one umbrella using PostMarkApp and utilizing the Rails gems (postmark-rails, postmark-gem, and mail). I have successfully created a mailer that handles sending receipts for purchases but I haven't been able to receive emails for forgotten passwords. My development logs show that Devise sent the message but no email is received in my inbox and the PostMark credits are not decremented.
What's the best or easiest way to have Devise's mailers send through my PostMark account?
Snippet from config/environments/development.rb
config.action_mailer.delivery_method = :postmark
config.action_mailer.postmark_settings = { :api_key => "VALID_API_KEY_WAS_HERE" }
config.postmark_signature = VALID_POSTMARK_SIGNATURE_WAS_HERE
My Mailer that uses Postmark
class Notifier < ActionMailer::Base
# set some sensible defaults
default :from => MyApp::Application.config.postmark_signature
def receipt_message(order)
#order = order
#billing_address = order.convert_billing_address_to_hash(order.billing_address)
mail(:to => #order.user.email, :subject => "Your Order Receipt", :tag => 'order-receipt', :content_type => "text/html") do |format|
format.html
end
end
end
EDIT: SOLUTION to my question is below
Solved it by having my Notifier mailer extend Devise::Mailer and specifying Devise to use my Notifier as the mailer within config/initializers/devise.rb
snippet from config/initializers/devise.rb
# Configure the class responsible to send e-mails.
config.mailer = "Notifier"
My Notifier Mailer now
class Notifier < Devise::Mailer
# set some sensible defaults
default :from => MyApp::Application.config.postmark_signature
# send a receipt of the Member's purchase
def receipt_message(order)
#order = order
#billing_address = order.convert_billing_address_to_hash(order.billing_address)
mail(:to => #order.user.email, :subject => "Your Order Receipt", :tag => 'order-receipt', :content_type => "text/html") do |format|
format.html
end
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
Using the latest version of Devise, the methods above didn't help me. This is my solution.
In config/application.rb:
config.action_mailer.delivery_method = :postmark
config.action_mailer.postmark_settings = { :api_key => "your-API-key-here" }
In config/initializers/devise.rb:
config.mailer = "UserMailer" # UserMailer is my mailer class
In app/mailers/user_mailer.rb:
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
Finally, make sure you have generated custom devise views
rails generate devise:views
and move the email templates from app/views/devise/mailer/ to app/views/user_mailer/
mv app/views/devise/mailer/* app/views/user_mailer/
If you also want to specify 'tags' in postmark headers you have to do this in your mailer:
# this override method is from Devise::Mailers::Helpers
def headers_for(action)
headers = {
:subject => translate(devise_mapping, action),
:from => mailer_sender(devise_mapping),
:to => resource.email,
:template_path => template_paths,
:tag => action.dasherize # specify the tag here
}
if resource.respond_to?(:headers_for)
headers.merge!(resource.headers_for(action))
end
unless headers.key?(:reply_to)
headers[:reply_to] = headers[:from]
end
headers
end
I also had to generate the views for devise and copy the mail templates into the right place for my mailer. Something like this -
rails generate devise:views
cp app/views/devise/mailer/* app/views/notification_mailer/

Rails 3 ActionMailer Google Apps: Timeout::Error

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.