sending mail in production env - ruby-on-rails

I am trying to send mail in production environment as follow:
in user-mailer.rb:
class UserMailer < ActionMailer::Base
default to: "a.b#gmail.com"
def feedBack_mail(email,message)
#mess=message
#url = 'http://www.google.com'
mail(from: email, subject: 'new_feedback')
end
end
in feedback_mail.text.erb
<%= #mess %>
in user_controller.rb
def create
respond_to do |format|
#client= params[:client]
if UserMailer.feedBack_mail(params[:email],params[:message]).deliver
format.json { render json: #client, status: :created, location: #client }
else
format.json {render json: #client.errors, status: :unprocessable_entity}
end
end
end
in production.rb I add this configuration:
if config.respond_to?(:action_mailer)
# config.action_mailer.raise_delivery_errors = false
config.action_mailer.default_url_options = { :host => 'monsite.com' }
# ActionMailer Config
# Setup for production - deliveries, no errors raised
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 = {
address: "smtp.gmail.com",
port: 25,
domain: "monsite.com",
authentication: "plain",
enable_starttls_auto: true,
user_name: ENV["GMAIL_USERNAME"],
password: ENV["GMAIL_PASSWORD"]
}
end
what I got in production.log
I, [2015-02-17T15:38:05.436159 #2003552] INFO -- : Started POST "/feedback/send.json" for 197.14.10.26 at 2015-02-17 15:38:05 +0100
I, [2015-02-17T15:38:05.473826 #2003552] INFO -- : Processing by UsersController#create as JSON
I, [2015-02-17T15:38:05.473943 #2003552] INFO -- : Parameters: {"email"=>"c.d#gmail.com", "message"=>"qerfsertq", "user"=>{"email"=>"c.d#gmail.com", "message"=>"qerfsertq"}}
I, [2015-02-17T15:38:05.524701 #2003552] INFO -- : Rendered user_mailer/feedBack_mail.text.erb (0.5ms)
I, [2015-02-17T15:38:08.673302 #2003552] INFO -- :
Sent mail to a.b#gmail.com (755.5ms)
I, [2015-02-17T15:38:08.674260 #2003552] INFO -- : Completed 201 Created in 3200ms (Views: 0.3ms | ActiveRecord: 0.0ms)
But I do not get any mail sent to a.b#gmail.com not even as spam
I really could not understand where's the problem

I solved the problem by creating a Mandrill account and changing the configuration like follow:
config.action_mailer.smtp_settings = {
:port => '587',
:address => 'smtp.mandrillapp.com',
:user_name => 'xxxxxxxx',
:password => 'xxxxxx',
:domain => 'domain.com',
:authentication => :plain
}
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { host: "monsite.com" }

Related

Rails Net::OpenTimeout Mailer Namecheap Heroku Unable to send mailer 500 error

I attempted to send reset password email through a namecheap domain. I have reviewed every solution offered within StackOverflow and have not been able to get a viable solution. Let me know if I am missing any details below.
My Rails application is an API only.
It was working via gmail connection/smtp and when I switched it over to the namecheap/privateemail smtp it worked once.
After it worked locally I uploaded the code to heroku and that's when it started to fail.
# config/environments/development.rb
config.action_mailer.delivery_method = :sendmail
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_options = {from: 'noreply#domainemail.com'}
config.action_mailer.default_url_options = { :host => '587'}
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'mail.privatemail.com',
port: 587,
domain: 'domainname.com',
user_name: ENV['EMAIL'],
password: ENV['EMAIL_PW'],
authentication: :plain,
enable_starttls_auto: true,
openssl_verify_mode: 'none',
ssl: true
}
Production:
config.cache_classes = true
config.action_mailer.delivery_method = :sendmail
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_options = {from: 'noreply#domainname.com'}
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'mail.privatemail.com',
port: 587,
domain: 'domainname.com',
user_name: ENV['EMAIL'],
password: ENV['EMAIL_PW'],
authentication: :plain,
enable_starttls_auto: true,
openssl_verify_mode: 'none'
}
NotifierMailer class
class NotifierMailer < ApplicationMailer
default_url_options[:host] = ENV['BACKEND_URL']
default from: 'noreply#domainemail.com'
def create
admin = Admin.find_by(email: params[:email])
admin.generate_password_reset_token!
Notifier.password_reset(admin).deliver
end
def password_reset(admin)
#admin = admin
#url = "#{ENV['BACKEND_URL']}/password/reset?token=#{#admin.reset_password_token}&email=#{#admin.email}"
mail(to: "#{#admin.first_name} #{#admin.last_name} <#{#admin.email}>",
subject: "Ion Portal - Password Reset")
end
end
Password controller
class PasswordController < ApplicationController
protect_from_forgery with: :null_session
# include ActionController::RequestForgeryProtection
# protect_from_forgery with: :exception, unless: -> { request.format.json? }
def forgot
puts params
if params[:email].blank? # check if email is present
render json: {
error: "Email not present"
}
end
admin = Admin.find_by(email: params[:email]) # if present find admin by email
if admin.present?
admin.generate_password_token! #generate pass token
NotifierMailer.password_reset(admin).deliver_now
render json: { status: 'ok' }
else
render json: { error: ["Email address not found. Please check and try again."]}, status: :not_found
end
end
def reset
token = params[:token].to_s
if params[:email].blank?
return render json: {error: "Token not present"}
end
admin = Admin.find_by(reset_password_token: token)
if admin.present? && admin.password_token_valid?
if admin.reset_password!(params[:password])
redirect_to "#{ENV['ION_URL']}"
else
render json: {error: admin.errors.full_messages}, status: :unprocessable_entity
end
else
render json: { error: ["Link not valid or expired. Try generating a new link."]}, status: :not_found
end
end
def update
if !params[:password].present?
render json: {error: 'Password not present'}, status: :unprocessable_entity
return
end
if current_user.reset_password(params[:password])
render json: {status: 'ok'}, status: :ok
else
render json: {errors: current_user.errors.full_messages}, status: :unprocessable_entity
end
end
def successful_reset
render success_path
end
end
These settings worked for me. Turned out I also had left the settings for MailCatcher as well, that was my initial problem. Double check as well that what the domain setting and server address match, which in development, would mean setting the domain to 'localhost:3000'. Good luck!
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => 'mail.privateemail.com',
:port => 587,
:domain => 'localhost:3000',
:user_name => 'email#email.com',
:password => 'xxxxxxxxxxxxxxx',
:authentication => :plain,
:enable_starttls_auto => true,
}
After 24 hours of trial and error I had to remove a couple of lines of code to make it work.
config.action_mailer.smtp_settings = {
# For Gmail
# :address => "smtp.gmail.com",
# :port => "587",
# :domain => "gmail.com",
# :user_name => "noreply#gmail.com",
# :password => "pasword!",
# :authentication => "plain",
# :enable_starttls_auto => true
# For Namecheap
:enable_starttls_auto => true, #this is the important stuff!
:address => 'smtp.privateemail.com',
:port => 587,
:domain => 'privateemail.com',
:authentication => :plain,
:user_name => 'noreply#domainname.com',
:password => 'password!'
}
I removed the following:
enable_starttls_auto: true,
openss
l_verify_mode: 'none'

ActionMailer Successful outbound email but not receiving

I looked around for similar issues but nothing seems to work for me.
I'm using ActionMailer
to send emails. The email outbound went through successfully but I I'm not receiving it in my email.
I have enabled POP mail service.
config/environments/development.rb
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.smtp_settings = {
:authentication => :plain,
:address => "smtp.mailgun.org",
:port => 587,
:domain => "tvtt.co",
:user_name => "postmaster#tvtt.co",
:password => "password"
}
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
mailers/notification_mailer.rb
class NotificationMailer < ActionMailer::Base
default from: 'support#tvtt.com'
def acquisition_email(listing)
#neighborhood = listing.neighborhood
#tenant_fee = listing.tenant_fee
#listing = listing
mail(
to: "yseremail#gmail.com",
subject: "List your place on tvtt!"
)
end
end
And here is the "successful" output for the email outbound:
Sent mail to yseremail#gmail.com (23.1ms)
Date: Sat, 12 Dec 2015 20:05:13 -0500
From: support#tvtt.com
To: yseremail#gmail.com
Message-ID: <566cc44930cf0_64f23fc8933831dc28566#yseremail-MacBook-Pro.local.mail>
Subject: List your place on tvtt!
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: quoted-printable

530-5.5.1 Authentication Required. Learn more at

In order_mailer.rb:
default from: 'notifications#example.com'
def welcome_email(order)
#user = "Uday kumar das"
#url = 'http://example.com/login'
mail(to: 'dasudaykumar017#gmail.com', subject: 'Welcome to My Awesome Site')
end
In orders_conroller:
def delivery
#order1 = Order.where(:orderId=>params[:orderId])
#order = Order.find(#order1)
OrderMailer.welcome_email(#order).deliver
end
In environments/development.rb:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
I am new to mails in rails.I am referring http://guides.rubyonrails.org/action_mailer_basics.html to learn. I am getting error like:
Net::SMTPAuthenticationError in OrdersController#delivery`
530-5.5.1 Authentication Required. Learn more at`
I did the same using my gmail, following are my configurations, try and see it if works
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:authentication => :plain,
:user_name => "<my gmail>#gmail.com",
:password => "<my gmail password>",
:openssl_verify_mode => 'none' }
Please note the:
:openssl_verify_mode => 'none'
section to skip the ssl errors

emails: server sends but a user doesn't receive

I'm facing a weird bug.
Trying to set up welcome email after a user's registration.
According to server logs, my code sends an email but a user doesn't receive it.
Here is my code:
models/user.rb
after_create :welcome_message
private
def welcome_message
UserMailer.welcome_email(self).deliver
end
and mailers/user_mailer.rb
def welcome_email(user)
#user = user
mail to: #user.email, subject: t("mailer.welcome_email.welcome")
end
here are environments. The error occurs in both of them
config/environments/development.rb
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.mandrillapp.com',
port: '587',
domain: 'localhost:3000',
user_name: 'welcome#mysite.com',
password: 'blahblahblah',
authentication: 'plain',
enable_starttls_auto: true
}
and config/environments/production.rb
config.action_mailer.default_url_options = { :host => 'mysite.com' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.mandrillapp.com',
port: '587',
domain: 'mysite.com',
user_name: 'welcome#mysite.com',
password: 'blahblahblah',
authentication: 'plain',
enable_starttls_auto: true
}
I'm using Devise to register users. So, also tried to do that via :confirmable. Outcome is the same – test user doesn't get a welcome email.
Gemfile
gem 'rails', '3.2.12'
gem 'devise', '3.0.3'
gem "mail"
and here are server logs
Sent mail to user#hismail.com (1920ms)
Date: Sat, 10 May 2014
From: welcome#mysite.com
Reply-To: welcome#mysite.com
To: user#hismail.com
Message-ID: <aaa#aaa.mail>
Subject: Confirmation instructions
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<p>Welcome user#hismail.com!</p>
Yet eventually there is no welcome email at user#hismail.com
Any ideas?
UPD:
on the internet I see that nobody else faces the same problem
So, I guess I'm just missing something. But cannot figure out what and where. Please help me.
Try this:-
Create a file named as setup_mail.rb in config/initializers/ folder so that it looks like
config/initializers/setup_mail.rb
and put the below code in that file:-
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.smtp_settings = {
:address => "smtp.mandrillapp.com",
:domain => "mysite.com",
:port => 80,
:user_name => "username",
:password => "blahblahblah",
:authentication => :plain
}
ActionMailer::Base.default_url_options[:host] = "mysite.com"
Delete this from config/environments/development.rb and config/environments/production.rb
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.mandrillapp.com',
port: '587',
domain: 'localhost:3000',
user_name: 'welcome#mysite.com',
password: 'blahblahblah',
authentication: 'plain',
enable_starttls_auto: true
}

Rails mailer undefined method `encoding' for #<Hash:0x774b2d51>

I am trying to send email in Rails.
The code is
class UserMailer < ActionMailer::Base
default :from => "from#example.com"
def send_password_reset(player)
Rails.logger.info "Password email"
#player = player
delivery_options = {
user_name: 'sites.smtp#mymail.com',
password: 'password',
address: 'smtpout.secureserver.net'
}
mail(to: player.email,
subject: "Recover your Password.",
body: "Password email",
delivery_method_options: delivery_options)
end
end
Controller
if player.valid?
player.send_password_reset_token #(Line: 209)
render json: {SENT: 'YES'}
else
render json: {error: "Player does not exist."}, status: :not_found
end
Model
def send_password_reset_token
password_reset_token = SecureRandom.urlsafe_base64(nil, false)
self.update_attribute(:password_reset_token, password_reset_token)
UserMailer.send_password_reset(self).deliver #(line: 25)
end
The logger says
Sent mail to ranasaani#gmail.com (25ms)
Completed in 433ms
NoMethodError (undefined method `encoding' for #<Hash:0x774b2d51>):
app/models/player.rb:25:in `send_password_reset_token'
app/controllers/players_controller.rb:209:in `password_forgot'
But I am not receiving any email and here is not any method encoding. Then what is causing this error
have u setup your mailer.rb like this
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "your.domain.com",
:user_name => ENV['MY_GMAIL_USER_NAME'],
:password => ENV['MY_GMAIL_PASSWORD'],
:authentication => "plain",
:enable_starttls_auto => true }

Resources