Sending confirmation emails to registered users in ROR app via localhost - ruby-on-rails

In my ROR app, I am trying to send confirmation emails to my registered users when they signup, my website is on localhost currently. I am getting this error:
"undefined method `recipients' for #<UserMailer:0x3d841a0>"
Here is my code;
development.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options = { :host => "localhost:3000"}
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => "587",
:authentication => :login,
:user_name => "myemailid#gmail.com",
:password => "myrealpassword"
}
Users_controller.rb
def new
UserMailer.registration_confirmation(#user).deliver
end
def create
#user = User.new(params[:user])
if #user.save
UserMailer.registration_confirmation(#user).deliver
sign_in #user
flash[:success] = "Welcome!"
redirect_to #user
else
render 'new'
end
end
user_mailer.rb
class UserMailer < ActionMailer::Base
def registration_confirmation(user)
recipients user.email
from "myemailid#gmail.com"
subject "Thank you for registration"
body :user => user
end
end

** development.rb**
config.action_mailer.delivery_method = :sendmail
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
** Users_controller.rb **
def new
UserMailer.registration_confirmation(#user).deliver
end
def create
#user = User.new(params[:user])
if #user.save
UserMailer.registration_confirmation(#user).deliver
sign_in #user
flash[:success] = "Welcome!"
redirect_to #user
else
render 'new'
end
end
** User_mailer.rb **
def registration_confirmation(user)
#message = 'whatever you want to say here!'
mail(:from => "myemailid#gmail.com", :to => user.email, :subject => "Thank you for registration")
end
**/app/views/user_mailer/registration_confirmation.text.erb *
<%= #message %>
That's what I've done in my development mode, and it works

What version of Rails are you using?
Sending of email changed in version 3.2 (I believe)
http://api.rubyonrails.org/classes/ActionMailer/Base.html
Try:
UserMailer.registration_confirmation(#user).deliver

This is your user_mailer.rb
user_mailer.rb
class UserMailer < ActionMailer::Base
def registration_confirmation(user)
recipients user.email
from "myemailid#gmail.com"
subject "Thank you for registration"
body :user => user
end
Try instead:
class UserMailer < ActionMailer::Base
default from: "myemailid#gmail.com"
def registration_confirmation(user)
mail(to: user.email, subject: "Thank you for registration")
end
end
And set up the appropriate view in views/user_mailer e.g. registration_confirmation.html.erb

Related

Cannot sent reset password mails in rails application with a timeout situation

I'm very new to Rails. Now I have a problem with sending reset password emails on Rails website.
Right now the problem is when I enter a registered email to reset the password, the email cannot be sent.
Net::OpenTimeout in PasswordResetsController#create
I think the problem is about the stmp setting.
Here is some of my code. By the way, I'm doing all these in the development environment.
development.rb
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.default_url_options = { :host =>
"http://localhost:3000" }
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => "587",
:domain => "Here I put my intern company",
# :domain => "gmail.com",
:authentication => :plain,
:user_name => "Here I put my actual email",
:password => "The password of my actual email",
:enable_starttls_auto => true
}
password_resets_controller.rb
def create
# #user = User.find_by(email: params[:password_resets]
[:email].downcase)
#user = User.find_by(email: params[:password_reset]
[:email].downcase)
if #user
#user.create_reset_digest
#user.send_password_reset_email
flash[:info] = "Email sent with password reset instructions"
redirect_to root_url
else
flash.now[:danger] = "Email address not found"
render 'new'
end
end
user.rb
def send_password_reset_email
UserMailer.password_reset(self).deliver_now
end
user_mailer.rb
class UserMailer < ApplicationMailer
default from: "naomi<noreply#MYINTERNCOMPANY.com>"
def password_reset(user)
#user = user
mail(to: #user.email, subject: 'Password reset')
end
end
application_mailer.rb
class ApplicationMailer < ActionMailer::Base
default from: 'noreply#MYINTERNCOMPANY.com'
layout 'mailer'
end
Can anyone help with this?
And also, after this how can I set the production environment so that the website can be deployed on Heroku?

Sending IP address to an email in ruby on rails

I want to be able to send a new user's ip address to my admin account when a new user signs up. I have:
class UserMailer < ActionMailer::Base
default :from => '"Admin" <support#mysite.com>'
def registration_confirmation(user)
#user = user #needed in order to have access to user variables
def client_ip
#client_ip = request.remote_ip
end
mail(:to => "#{user.name} <#{user.email}>", :subject => "Welcome to MySite").deliver!
mail(:to => '"Admin" <support#mysite.com>',
:subject => "New Member",
:body => "New member #{user.name} with email #{user.email} and ip: #{#client_ip} has just signed up!",
:content_type => "text/html")
end
end
I'm getting the error:
NameError in UsersController#create
undefined local variable or method `request' for #
users_controller.rb
def create
#user = User.new(params[:user])
if #user.save
UserMailer.registration_confirmation(#user).deliver
sign_in #user
redirect_to #user
else
render 'new'
end
end
Something like:
def create
#user = User.new(params[:user])
if #user.save
UserMailer.registration_confirmation(#user, request.remote_ip).deliver
sign_in #user
redirect_to #user
else
render 'new'
end
end
def registration_confirmation(user, client_ip = '0.0.0.0')
#user = user #needed in order to have access to user variables
mail(:to => "#{user.name} <#{user.email}>", :subject => "Welcome to MySite").deliver!
mail(:to => '"Admin" <support#mysite.com>',
:subject => "New Member",
:body => "New member #{user.name} with email #{user.email} and ip: #{client_ip} has just signed up!",
:content_type => "text/html")
end
You only have access to the request object in the controller/view. So you need to pass the value along to the mailer if that is where you want to have access to it.

action mailer rails ArgumentError in Password_resets#create

I set up a basic sign in, login, sign out format for a rails app and I was going to give it a function so that if a person forgot their password they could get a email back. when I went click on submit password reset I got
Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true
Extracted source (around line #3):
1: To reset your password click the URL below.
2:
3: <%= edit_password_reset_url(#user.password_reset_token) %>
4:
5: If you did not request your password to be reset please ignore this email and your password will stay as it is.
Rails.root: /Users/cheatermoves/nightclass/mainproject/project
Application Trace | Framework Trace | Full Trace
app/views/user_mailer/password_reset.text.erb:3:in `_app_views_user_mailer_password_reset_text_erb__3613112772785486465_70118994937040'
app/mailers/user_mailer.rb:6:in `password_reset'
app/models/user.rb:17:in `send_password_reset'
app/controllers/password_resets_controller.rb:7:in `create'
just completed rails cast 250 and was doing 274. Everything was fine until I got this problem.
here is my controllers. Password resets:
class PasswordResetsController < ApplicationController
def new
end
def create
user = User.find_by_email(params[:email])
user.send_password_reset if user
redirect_to root_url, :notice => "Email sent with password reset instructions."
end
end
sessions:
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by_email(params[:email])
if user && user.authenticate(params[:password])
if params[:remember_me]
cookies.permanent[:auth_token] = user.auth_token
else
cookies[:auth_token] = user.auth_token
end
redirect_to root_url, :notice => "Logged in!"
else
flash.now.alert = "Invalid email or password"
render "new"
end
end
def destroy
cookies.delete(:auth_token)
redirect_to root_url, :notice => "Logged out!"
end
end
users:
class UsersController < ApplicationController
def new
#user = User.new
end
def create
#user = User.new(params[:user])
if #user.save
session[:user_id] = #user.id
redirect_to root_url, notice: "Thank you for signing up!"
else
render "new"
end
end
end
and application:
class ApplicationController < ActionController::Base
protect_from_forgery
private
def current_user
#current_user ||= User.find_by_auth_token( cookies[:auth_token]) if cookies[:auth_token]
end
helper_method :current_user
end
in environments/development.rb I have
config.action_mailer.default_url_options = { :host => "localhost:3000" }
here is my user model
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
has_secure_password
validates_presence_of :password, :on => :create
before_create { generate_token(:auth_token) }
def generate_token(column)
begin
self[column] = SecureRandom.urlsafe_base64
end while User.exists?(column => self[column])
end
def send_password_reset
generate_token(:password_reset_token)
self.password_reset_sent_at = Time.zone.now
save!
UserMailer.password_reset(self).deliver
end
end
user_mailer.rb file
class UserMailer < ActionMailer::Base
default from: "from#example.com"
def password_reset(user)
#user = user
mail :to => user.email, :subject => "Password Reset"
end
end
anyone know what the problem is and how to fix it? I'm using rails 3.2.14 if anyone is wondering. Thanks!
Didn't read the part where you said that you already set your
config.default_url_options[:host] = "localhost:3000"
my bad, sorry

Rails 3 Contact Form, undefined method?

I'm building a contact form in Rails 3 using this Railscast: http://railscasts.com/episodes/326-activeattr?view=asciicast
I know that the only thing I'm doing wrong is filling out where the message should be sent. I used the following code to send the message:
mail(:to => "me#myemail.com")
However, this doesn't seem to work, because everytime I submit the form, I get this error:
NoMethodError in MessagesController#create
undefined method `mail' for #<MessagesController:0x00000103734bd8>
Application Trace | Framework Trace | Full Trace
app/controllers/messages_controller.rb:10:in `create'
What should I replace this line with to send the message?
messages_controller.rb
class MessagesController < ApplicationController
def new
#message = Message.new
end
def create
#message = Message.new(params[:message])
if #message.valid?
UserMailer.contact_message(#message).deliver
redirect_to root_url, notice: "Message sent! Thank you for contacting us."
else
render "new"
end
end
end
user_mailer.rb
class UserMailer < ActionMailer::Base
def contact_message(message)
#message = message
mail(:to => "myemail#mymail.com", :subject => "New Message")
end
end
setup_mail.rb
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "mywebsite.com",
:user_name => "myemail",
:password => "secret",
:authentication => "plain",
:enable_starttls_auto => true
}
ActionMailer::Base.default_url_options[:host] = "localhost:3000"
there's no object created on the mail variable on your controller. Actualy to user Mailer on rails you don't have to instantiate a variable just call you mailer with the method. Like:
YourMailer.send(params)
You can always go to rails guides to see how some stuff works:
http://guides.rubyonrails.org/action_mailer_basics.html
The Message object you're creating is just an object that can be used in a form with validations. You still need to actually create a Mailer object, and fill it with the name/email/message that you get from the #message object.
If you create a Mailer (see Episode 206 - Action Mailer), and you'd do something like this in your controller after creating a MessageMailer
def create
if #message.valid?
MessageMailer.contact_message(#message).deliver
redirect_to root_url, notice: "Message sent! Thank you for contacting us."
else
render "new"
end
end
I just answered a similar question here: https://stackoverflow.com/a/17883328/307308
You are missing the from attribute in your mail method.
mail(:from => 'system#mymail.com', :to => "myemail#mymail.com", :subject => "New Message")

Rails SMTPAuthenticationError Action Mailer

I've developed a Rails 3 app and I've recently added an action mailer for when a user signs up.
Testing it locally and it sends an email perfectly, but when I move it to the server at test it for a new user signing up, I get the following error.
I am running Nginx on the server with the original configurations, both local machine and server are running ubuntu 11.10
Net::SMTPAuthenticationError in UsersController#create
535-5.7.1 Username and Password not accepted.
app/controllers/users_controller.rb:26:in 'create'
In users_controller.rb - create - Line 26 is UserMailer.welcome_email(#user).deliver
def create
#user = User.new(params[:user])
if #user.save
sign_in #user
flash[:success] = "Welcome to University Sports!"
redirect_to #user
UserMailer.welcome_email(#user).deliver
else
#title = "Sign up"
render 'new'
end
end
In development.rb I have the following code:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'www.mydomain.co.uk',
:user_name => 'email#mydomain.co.uk',
:password => 'email_password',
:authentication => 'plain',
:enable_starttls_auto => true }
user_mailer.rb
class UserMailer < ActionMailer::Base
default :from => "univeristy.sports#louisgriffiths.co.uk"
def welcome_email(user)
#user = user
#url = "http://www.mydomain:3000"
mail(:to => user.email, :subject => "Welcome to University Sports")
end
end
I had the exact same error and found that my application.yml file was overriding it so I deleted that and now it works fine!

Resources