How do I create a email confirmation? - ruby

I'm trying to make a simple email notification when a user signs up.
My user sign up works fine, and I followed the "Sending Email" tutorial exactly but can't get it to work. What am I doing wrong?
user_controller.rb
class Admin::UsersController < InheritedResources::Base
before_filter :admin_only
actions :index, :show, :new, :edit, :create, :update, :destroy
respond_to :html
# def new
# #user = User.new(:invitation_token => params[:invitation_token])
# #user.email = #user.invitation.recipient_email
# end
def create
#user = User.new(params[:user])
UserMailer.deliver_registration_confirmation(#user) < -- where I added the mailer
#user.save(false)
respond_to do |format|
format.html{ redirect_to admin_users_path}
end
end
private
def collection
paginate_options ||= {}
paginate_options[:page] ||= (params[:page] || 1)
paginate_options[:per_page] ||= (params[:per_page] || 20)
#search = User.search(params[:search])
#users ||= #search.all.paginate(paginate_options)
end
end
environments/production.rb
# Settings specified here will take precedence over those in config/environment.rb
config.action_mailer.default_url_options = { :host => 'alpine.totaline.com' }
config.action_mailer.raise_delivery_errors = true
# set delivery method to :smtp, :sendmail or :test
config.action_mailer.delivery_method = :smtp
# these options are only needed if you choose smtp delivery
config.action_mailer.smtp_settings = {
:address => 'smtp.gmail.com',
:port => 25,
:domain => 'alpine.totaline.com',
:authentication => :login,
:user_name => 'emailname#gmail.com',
:password => 'thepassword'
}
models/user_mailer.rb
class UserMailer < ActionMailer::Base
def registration_confirmation(user)
recipients user.email
from "webmaster#alpinechallenge.com"
subject "Thank you for Registering"
body "You are now registered on the Alpine Challenge!"
end
end

Looks like for Gmail you need to use port 587:
# these options are only needed if you choose smtp delivery
config.action_mailer.smtp_settings = {
:address => 'smtp.gmail.com',
:port => '587',
:domain => 'alpine.totaline.com',
:authentication => :login,
:user_name => 'emailname#gmail.com',
:password => 'thepassword'
}
This page contains notes about configuring mail clients for usage with Gmail.

Related

Rails 5 Faraday connection for Facebook Omniauth

I have tried all the tricks in the book and all the possible solution in stack over flow.But still couldn't figure out the problem. The program pauses at the call back initiated stage and shows faraday connection : timed out. I have the following code:
gem 'omniauth', '~> 1.3', '>= 1.3.1'
gem 'omniauth-facebook'
Routes.rb
devise_for :users,
:path => '',
:path_names => {:sign_in => 'login', :sign_out => 'logout', :edit => 'profile' },
:controllers => {:omniauth_callbacks => 'omniauth_callbacks'}
/Controller.rb
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
#user = User.from_omniauth(request.env["omniauth.auth"])
if #user.persisted?
sign_in_and_redirect #user, :event => :authentication
set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
else
session["devise.facebook_data"] = request.env["omniauth.auth"].except("extra")
redirect_to new_user_registration_url
end
end
end
/user.rb
class User < ApplicationRecord
def self.from_omniauth(auth)
user = User.where(email: auth.info.email).first
if user
return user
else
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.email = auth.info.email
user.image = auth.info.image
user.password = Devise.friendly_token[0,20]
user.firstname = auth.info.first_name
user.lastname = auth.info.last_name
user.phonenumber == '0000000000'
user.tc == true
user.year == true
end
end
end
end
/devise.rb
config.omniauth:facebook,ENV['FACEBOOK_ID'],ENV['FACEBOOK_SECRET'],scope:'email',info_fields:'email,first_name,last_name'

How to use Actionmailer4 without rails

I have used actionmailer 2.3 without rails and when tried the same code with 4.0 components ruby code that use actionmailer4 is not working. I have the solution now that works and I am going to respond in the answer section.
require 'rubygems'
require 'action_mailer'
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:enable_starttls_auto => :true,
:address => "smtp.gmail.com",
:port => 587,
:domain => "xxxx.com",
:authentication => :plain,
:user_name => "aaaa#xxxx.com",
:password => "ppppp"
}
ActionMailer::Base.view_paths= File.dirname(__FILE__)
class TEST_CLASS_email < ActionMailer::Base
default( {from: "aaaa#xxxx.com"} )
def sendemail(s_email)
#var = 'TESTING VARIABLE PASSING TO VIEW'
mail(
to: "bbbb.cccc#dddd.com",
subject: "Testing out from ACTIONMAILER4",
bcc: s_email
)
end
end
def testemailpubmethod(email)
begin
e=TEST_CLASS_email.sendemail(email)
if e.present?
e.deliver_now
end
rescue Exception => exception
puts "Email:Exception Message: #{exception.message}"
puts "Error sending the email #{exception.backtrace.join("\n")}"
end
end
testemailpubmethod("eeee#ffff.com")
Now create the template files in the following subdirectory.
============================================================
./test_class_email/sendemail.text.erb
=====================================
This is a text email from <%= #var %>
-Have fun using Actionmailer4 :)

Full URL is not coming when using action mailer in Rail 3?

I am trying to implement reset password feature in my app using Ruby on Rails.I am using action mailer to send link to user's email.The email is delivering to user with link but when user is clicking on that link the full URL is not coming(i.e-"localhost:3000/homes/resetpass").Only "/homes/resetpass" is coming.Please check my following codes and help me to resolve this issue.
views/user_mailer/registration_confirmation.html.erb
<p><%= #users.first_name %>,</p>
<p>Thank you for registering!</p>
<p>Edit Your Password <%= link_to "Click Here",homes_resetpass_path(:id => #users.id ),:host => "localhost:3000" %></p>
mailers/user_mailers.rb
class UserMailer < ActionMailer::Base
default from: "rajatpradhan474#gmail.com"
def registration_confirmation(user)
#users = user
mail(:to => user.email, :subject => "Registered")
end
end
config/initializers/setup_mail.rb
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:user_name => "rajatpradhan474#gmail.com",
:password => "**********",
:authentication => "plain",
:enable_starttls_auto => true
}
ActionMailer::Base.default_url_options[:host] = "localhost:3000"
You need to use url_for instead of link_to. The linked page describes how and why.

Ruby: Resque queues instantly

I have the following code. Its job to is to send an email based on data given through the browser (using Sinatra). It sends an email to the address given after 20 seconds. When I run the program, It instantly sends the email, without waiting for the time. Can anyone help me out with this issue.
require 'rubygems'
require 'sinatra'
require 'pony'
require 'resque'
require 'resque_scheduler'
require 'active_support/time'
Resque.redis = 'localhost:6379'
Resque::Scheduler.dynamic = true
def sendMail
Pony.mail({
:to => 'eldurotaduro#gmail.com',
:via => :smtp,
:via_options => {
:address => 'smtp.gmail.com',
:port => '587',
:enable_starttls_auto => true,
:user_name => 'EMAIL',
:password => 'PASSWD',
:authentication => :plain, # :plain, :login, :cram_md5, no auth by default
:domain => "localhost.localdomain" # the HELO domain provided by the client to the server
},
:body => 'roar'
})
end
class Roar
def self.queue; :app; end
end
class ChildJob
#message
#email
def setMess(mes)
#message = mes
end
def setMail(mail)
#email = mail
end
def self.queue; :app; sendMail; end
def self.perform
Pony.mail({
:to => 'eldurotaduro#gmail.com',
:via => :smtp,
:via_options => {
:address => 'smtp.gmail.com',
:port => '587',
:enable_starttls_auto => true,
:user_name => 'EMAILHERE#gmail.com',
:password => 'PASSWD',
:authentication => :plain, # :plain, :login, :cram_md5, no auth by default
:domain => "localhost.localdomain" # the HELO domain provided by the client to the server
},
:body => 'HAHAH'
})
end
end
get '/:email/:message/:time' do
email = params[:email]
message = params[:message]
time = params[:time]
time = time.to_i
Resque.enqueue_in(20.seconds, ChildJob)
end
Keep the :app symbol in self.queue as that's what sets the default queue (see this StackOverflow answer). Put the sendMail into a self.perform method, as that's the thing you want done when the schedule is met. e.g.
def self.queue
:app
end
def self.perform
sendMail
end

JIRA + Savon + Ruby issues

I am trying to get my new application integrated with JIRA for management of our customer's support tickets. What I had envisioned the system doing was in a before_filter gathering a user's account from within JIRA - from that I can pull up a list of accounts and what not, and if they don't have one then we create one based on their details in the Rails application. Thing is I'm having major issues doing things like removing the user from the jira-users group and adding them to a separate group I have for customers called customer-support. This is the code I have currently:
def current_jira_user
# Fetch the current user in JIRA, if we don't exist, create it!
user_try = #jira.request :get_user do |soap|
soap.body = { :token => #token, :username => "#{current_user.username}" }
end
if user_try.to_hash[:get_user_response][:get_user_return][:href].nil?
# We need to create the user
#jira.request :create_user do |soap|
soap.body = {
:token => #token,
:username => "#{current_user.username}",
:password => UUID.new.to_s,
:fullName => current_user.full_name,
:email => "noreply#XXXXX.XXX" #this is such a hack to get around JIRA's "you've got an account" email
}
end
new_user = RemoteUser.find(current_user.username)
#jira.request :remove_user_from_group do |soap|
soap.body = { :token => #token, :group => RemoteGroup.find('jira-users'), :ruser => new_user }
end
#jira.request :add_user_to_group do |soap|
soap.body = { :token => #token, :group => RemoteGroup.find('customer-support'), :ruser => new_user }
end
new_user[:email] = current_user.email
#jira.request :update_user do |soap| # change their email to the valid one
soap.body = { :token => #token, :ruser => new_user }
end
new_user
else
user_try.to_hash[:get_user_response][:get_user_return]
end
end
def verify_jira_connection
# Verify that we can reach the JIRA instance
#jira = Savon::Client.new do
wsdl.document = JIRA_SOAP_URI
end
#jira.http.read_timeout = 300
#jira.http.auth.ssl.verify_mode = :none
#auth = #jira.request :login do |soap|
soap.body = { :username => JIRA_LOGIN, :password => JIRA_PASSWORD }
end
#token = #auth.to_hash[:login_response][:login_return]
end
## REMOTE CLASSES
class RemoteUser
include Savon::Model
client do
http.headers["Pragma"] = "no-cache"
http.auth.ssl.verify_mode = :none
end
namespace "http://beans.soap.rpc.jira.atlassian.com"
endpoint JIRA_SOAP_URI
basic_auth JIRA_LOGIN, JIRA_PASSWORD
actions :get_user
def self.find(username)
get_user(:username => username).to_hash
end
end
class RemoteGroup
include Savon::Model
client do
http.headers["Pragma"] = "no-cache"
http.auth.ssl.verify_mode = :none
end
namespace "http://beans.soap.rpc.jira.atlassian.com"
endpoint JIRA_SOAP_URI
basic_auth JIRA_LOGIN, JIRA_PASSWORD
actions :get_group
def self.find(group)
get_group(:groupName => group).to_hash
end
end
Users are created just fine, but when I get to the removeUserFromGroup call, I get (soapenv:Server.userException) com.atlassian.jira.rpc.exception.RemoteValidationException: group name cannot be null, needs a value. Using the Jira4R gem is out thanks to our using Ruby 1.9.2. Any help is appreciated. Thanks!
Maybe you need to explicitly send the name?
:group => RemoteGroup.find('jira-users').name
instead of this
:group => RemoteGroup.find('jira-users')
If you were willing to do some rewriting, you could try using a Ruby 1.9-compatible fork of jira4r

Resources