Actionmailer Rails 3 - ruby-on-rails-3

I have added a contact form to my site and am having a problem, when the message is sent I get my flash message, "successfully sent", however the email never arrives in my inbox. I am in development mode at the moment and my app/config file looks like this
class Application < Rails::Application
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:user_name => "myemail#gmail.com",
:password => "example",
:authentication => :plain,
:enable_starttls_auto => true
}
config.action_mailer.default_url_options = {
:host => "gmail.com"
}
My contact Controller is like this
def new
#message = Message.new
end
def create
#message = Message.new(params[:message])
if #message.valid?
NotificationsMailer.new_message(#message).deliver
redirect_to(root_path, :notice => "Message was successfully sent.")
else
flash.now.alert = "Please fill all fields."
render :new
end
end
end
and finally my Notification Mailer
class NotificationsMailer < ActionMailer::Base
default :from => "myemail#gmail.com"
default :to => "myemail#gmail.com"
def new_message(message)
#message = message
if message.file
attachment_name = message.file.original_filename
attachments[attachment_name] = message.file.read
end
mail(:subject => "[myemail#gmail.com] #{message.subject}")
end
end
Am I missing anything obvious here as I have implemented this in another site which worked fine, just cant figure out what is going on
Any help appreciated

I know you set it in your app/config.rb, but I would ensure config.action_mailer.perform_deliveries isn't being overridden in your config/environments/development.rb

Related

ActionMailer does nothing, is set up correctly

In development.rb I have these ActionMailer settings:
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.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "gmail.com",
authentication: "plain",
enable_starttls_auto: true,
user_name: "username#gmail.com",
password: "password"
}
In notifier.rb (my mailer) I have these definitions:
def user_email(user)
#user = User.find(user)
mail(:to => user.email, :subject => "Welcome to me.")
end
def test_email
mail(:to => "here#there.com", :subject => "Test mail", :body => "Ain't I shapely?")
end
And running Notifier.test_email.deliver in the console gives me this: wrong number of arguments (0 for 1).
And running Notifier.user_email(2).deliver in the console gives me this: undefined method 'user_email' for Notifier:Class.
Am I missing something totally obvious here? The Gmail settings are all correct, but obviously the issue is before that.
Restarting the server fixed it. Nonsense.

Authlogic - how to set password_confirmation only for update?

I am trying to set up the password confirmation only on the page, where the user change his password.
My model looks this way:
class User < ActiveRecord::Base
attr_accessor :password_confirmation
acts_as_authentic do |c|
c.validate_login_field = false
c.validate_password_field = false
c.require_password_confirmation = true
c.logged_in_timeout(15.minutes)
end
validates :name, :presence => {:message => 'cannot be blank.'}, :allow_blank => true, :length => {:minimum => 3, :maximum => 40}, :on => :create
validates :email, :presence => {:message => 'address cannot be blank.'}, :allow_blank => true, :format => {:with => /\A[A-Za-z0-9._%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]+\z/, :message => 'address is not valid. Please, fix it.'}, :uniqueness => true
validates :password, :presence => {:message => 'cannot be blank.'}, :allow_blank => true, :length => { :minimum => 6, :maximum => 40}, :on => :create
validates :password_confirmation, :presence => {:message => 'cannot be blank.'}, :allow_blank => true, :length => { :minimum => 6, :maximum => 40 }, :on => :update
end
and my method that saving new password:
def change_password
#user = current_user
if #user.valid_password?(params[:user][:old_password])
if #user.update_attributes(params[:user].reject{|key, value| key == "old_password"})
flash[:notice] = 'Your password was successfuly changed.'
redirect_to :back
else
flash[:warning] = 'You did not fill twice your new password correctly. Please, fix it.'
redirect_to :back
end
else
flash[:warning] = 'Your old password is WRONG! What is your malfunction!?!'
redirect_to :back
end
end
My problem is, that if I set the form the old password, then new password (eg. new_password) and then the confirmations of the new password (eg. new_password1), so the new password is changed & saved into the database - but it shouldn't, because the new password and the confirmation of the new password aren't the same...
How I should set up the validation rules or, where could be a problem?
Thanks for advices
You need to validate the password only if it's being changed. If it's not being changed, then the validation for the password field should be skipped.
Railscasts.com episode #41 shows you how to do this.

Rails3 ActionMailer deliveries in development environment

Is it possible to send mailers in the development environment?
I've added this to my development.rb file:
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.smtp_settings = {
:address => "mail.email.com",
:port => 25,
:domain => 'email.com',
:user_name => 'email#email.com',
:password => 'password',
:authentication => 'plain',
:enable_starttls_auto => true }
Then I run UserMailer.welcome_email(#user).deliver in rails console which returns #<Mail::Message:2265713480, Multipart: true, Headers: <Date: Thu... but I never actually receive the email. Is there something else I need to configure?
Oh, and if I check ActionMailer::Base.deliveries it returns an empty hash => [].
Needed to add the following to environments/development.rb:
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.perform_deliveries = true

facing problem in action mailer

facing problem in action mailer.
my mailer is
def registration_confirmation(user)
subject "Password Reset Instructions"
from "sender_email_id#test.com"
recipients user.email
content_type "text/html"
body "RESET your Password"
end
Settings for mailer are
require 'development_mail_interceptor'
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "domain.in",
:user_name => "admin#domain.in",
:password => "PASSWORD",
:authentication => "plain",
:enable_starttls_auto => true
}
ActionMailer::Base.default_url_options[:host] = "localhost:3000"
Mail.register_interceptor(DevelopmentMailInterceptor) if Rails.env.development?
and my controller is UserMailer.registration_confirmation(#new_user).deliver
The mail is not going to the user.email , it is always going to admin#domain.in
I am using mail-v 2.2.19 and rails 3.0.9
please help.
try adding this line(works for me):
mail(:to => user.email, :subject => "Password Reset Instructions")
to your registration_confirmation method
def registration_confirmation(user)
from "sender_email_id#test.com"
content_type "text/html"
body "RESET your Password"
mail(:to => user.email, :subject => "Password Reset Instructions")
end

Devise with Rails sending mails

I am a newbie to sending mails through rails. I have implemented devise in my project and now I want to send a welcome email and/or a password-reset email. What changes do I need to make in Devise views?
No errors are displayed, but still I don't receive any email.
I have followed the links specified below and finally my devise.rb, development.rb and production.rb files are as follows:
devise.rb
config.mailer_sender = "abc#gmail.com"
development.rb
config.action_mailer.raise_delivery_errors = false
config.action_dispatch.best_standards_support = :builtin
config.active_support.deprecation = :notify
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = false
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default :charset => "utf-8"
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.active_support.deprecation = :log
config.action_mailer.smtp_settings ={
:enable_starttls_auto => true,
:address => 'smtp.gmail.com',
:port => 587,
:tls => true,
:domain => 'gmail.com',
:authentication => :plain,
:user_name => 'abc#gmail.com',
:password => '123456'
}
production.rb
config.action_mailer.default_url_options = { :host => 'gmail.com' }
config.active_support.deprecation = :notify
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors =false
config.action_mailer.default :charset => "utf-8"
config.action_mailer.smtp_settings = {
:enable_starttls_auto => true,
:address => 'smtp.gmail.com',
:port => 587,
:tls => true,
:domain => 'gmail.com',
:authentication => :plain,
:user_name => 'abc#gmail.com',
:password => '123456'
}
When trying to send an email using rails in development environment, you'll see that in the configuration file for development, there is a line config.action_mailer.perform_deliveries = false which specifies whether mail will actually be delivered.
When you create a new rails project that parameter is automatically set to false, and if you want to actually send an email in development mode, you must (among other things) set that parameter to true