Sending a confirmation email with Rails - ruby-on-rails

I am attempting to send a confirmation email to a newly registered user using Rails 4.0.1 and Ruby 2.0.0.
I am not getting any errors but the mail just is not sending. Here is the relevant code:
config/environments/development.rb
...
config.action_mailer.smtp_settings = {
:authenication=>:plain,
:address=>"smpt.mailgun.org",
:port=>587,
:domain=>"sandboxf4f4c96ebc7b4eb1b6c7475ad4de048c.mailgun.org",
:user_name=>"postmaster#sandboxf4f4c96ebc7b4eb1b6c7475ad4de048c.mailgun.org",
:password=>"6j3c9l35tu33"
}
app/model/user.rb
...
def create
#user=User.new(user_params)
if #user.save
ModelMailer.account_activation(#user).deliver
redirect_to lessons_url
else
render :new
end
end
mailers/model_mailer.rb
class ModelMailer < ActionMailer::Base
default from: "me#sandboxf4f4c96ebc7b4eb1b6c7475ad4de048c.mailgun.org"
def account_activation(user)
#user = user
mail to: "myemail#gmail.com", subject: "Account Activation"
end
end
Any help would be appreciated.

Related

How to resolve ActionMailer ignoring?

Hi I'm making user register application by ruby on rails.
But ActionMailer process ignore my code.
When I try to use
UserMailer::confirmation_email.deliver
on rails s, I could send email.
But when I use bellow process, UserMailer::confirmation_email is ignored.
Do you know how to resolve it?
user_controller.rb
class UsersController < ApplicationController
skip_before_action :verify_authenticity_token
def create
user = User.new(user_params)
result = Users::RegisterService.register_user(user)
render result
end
end
register_service.rb
module Users
module RegisterService
module_function
def register_user(user = {})
if user.save
UserMailer::confirmation_email(user)
result = { json: {status: 'User created successfully'}, status: :created }
else
result = { json: { errors: user.errors.full_messages }, status: :bad_request }
end
return result
end
end
end
user_mailer.rb
class UserMailer < ApplicationMailer
default from: 'email#gmail.com'
def confirmation_email(user)
#user = user
#url = 'http://192.16.8.33.10/user/confirm?confirmation_token=' + #user.confirmation_token
mail(
to: #user.email,
subject: 'Welcome to My Awesome Site',
template_name: 'user_confirm'
)
end
end
Call deliver_now to send email right away and deliver_later to send email asynchronously.
UserMailer::confirmation_email(user).deliver_now

Rails: Why is only one email sent to the last member of a loop?

I am very much a rails novice!
I am trying to write a method for a kind of on-line committee meeting. There are a fixed number(9) of users. When a user proposes a topic for discussion and/or voting the submit button needs to send an email to all members.
in app/mailers/user_mailer.rb I have:-
class UserMailer < ApplicationMailer
def new_topic_alert(topic)
#users = User.all
#users.each do |user|
mail to: user.email, subject: "New topic alert"
end
end
end
as part of app/controllers/topics_controller.rb I have:-
def send_alert
#topic = Topic.new(topic_params)
UserMailer.new_topic_alert(#topic).deliver_now
end
and:-
def create
#topic = Topic.new(topic_params)
if #topic.save
send_alert
flash[:info] = "New Topic alert emails sent."
redirect_to root_url
else
render 'new'
end
end
Please, why does the loop in user_mailer only send an email to the final person of the list. By incorporating "byebug" I have shown that it goes through all the user emails.
Try like below:
def send_alert
#topic = Topic.new(topic_params)
users = User.all
users.each do |u|
UserMailer.new_topic_alert(#topic, u).deliver_now
end
end
and update the mailer like
class UserMailer < ApplicationMailer
def new_topic_alert(topic,user)
mail to: user.email, subject: "New topic alert"
end
end

Sendgrid returns SMTP-AUTH requested but missing user name

I am new to ruby on rails and I follow the book Learn-ruby-on-rails by Daniel Kehoe. I have set up my sengrid login details correctly on the Ubuntu enviroment. echo $ SENDGRID_USERNAME returns my username correctly.
However, I still get "SMTP-AUTH requested but missing user name" error when I submit the contact form. I have tried to hardcode the login details and I still get the same errors. my configuration settings i smtp.sendgrid.net on port 587 and I allow send mails in development.
Please what am I not doing right.
Thanks a lot.
My user_mailer.rb is as shown:
class UserMailer < ActionMailer::Base
#default :from => "do-not-reply#example.com"
def contact_email(contact)
#contact = contact
mail( to: => Rails.application.secrets.owner_email, from: => #contact.email, subject: => "Website Visit")
end
end
while the contacts_controller.rb is shown below:
class ContactsController < ApplicationController
def new
#contact = Contact.new
end
def create
#contact = Contact.new(secure_params)
if #contact.valid?
UserMailer.contact_email(#contact).deliver_now
#TODO send message
flash[:notice] = "Message sent from #{#contact.name}."
redirect_to root_path
else
render :new
end
end
private
def secure_params
params.require(:contact).permit(:name, :email, :content)
end
end
The problem arose from an omission in the config/enviroments/development.rb file
I replaced
user_name: Rails.application.secrets.email_provider
with:
user_name: Rails.application.secrets.email_provider_username
and the problem was solved

Error when trying to send welcome email with Action Mailer?

I generated a action mailer from the ruby guide. However, when I try doing a test run on deployed site through Heroku, I get an error saying something went wrong and to check the logs.
I commented out the method that calls for the delivery of the email and sign up works fine. The email is practically a welcome email. I'm not sure where to go from here.
this is from my ActionMailer
default from: "from#example.com"
def welcome_email(user)
#user = user
#url ='https://inyourshoes.herokuapp.com/signup'
mail(to: #user.email, subject: 'Welcome to InYourShoes!')
end
end
This is from my usercontroller
def create
#user = User.new(user_params)
if #user.save
UserMailer.welcome_email(#user).deliver
sign_in #user
flash[:success] = "Welcome to InYourShoes!"
redirect_to #user
else
render'new'
end
end
Basically,i want a email to be sent once the user successfully signs up on the website. Your help is greatly appreciated
Assuming you're using Ruby >= 2.0, try:
default({from: "from#example.com"})
Otherwise:
default({ :from => "from#example.com" })

RoR - Devise mailer

I am using rails 3.2 with ruby 1.9.3 and devise 2.1.2.
I want an admin to create users with username and email.
How can i randomly generate a password for this user, and send him an email with his password?
First you should need create controller for handle regristration user by admin.
Assumed you name of controller is registrations_controller.rb
Here's app/controller/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
before_filter :resource_name
def resource_name
:user
end
def new
#user = User.new
end
def create
#user = User.new(params[:user])
generated_password = Devise.friendly_token.first(6) #password length 6
#user.password_confirmation = #user.password = generated_password
if #user.save
# Send Password Via Email
UserMailer.password_send(#user).deliver
redirect_to a_path
else
render action: "new"
end
end
Here's app/mailer/user_mailer.rb
class UserMailer < ActionMailer::Base
default :from => "youremail#email.com"
def password_send(user)
#user = user
mail(:to => user.email, :subject => "password", :from => "youremail#email.com")
end
end
Here's app/views/user_mailer/password_send.html.erb
Email : #user.email<br/>
Username : #user.username<br/>
Password : #user.password
Devise - Automatically generate password for users
Hope this help!
Randomly generate a password for a user
In your controller
pwd = Devise.friendly_token[0,16]
#user = User.new(params[:user])
#user.password_confirmation = #user.password = pwd
Send an email with his password
Take a look at this question: Send an actionmailer email upon user registration in devise rails 3 application
In the end it will boils down to something like:
mail(:to => "#{user.email}", :subject => "Welcome to My Awesome Site, your password #{pwd}")

Resources