Sidekiq ActionMailer working in development but not i produciton (using mailgun) - ruby-on-rails

I have set up Sidekiq to send an email on sign up.
The process works fine in development mode however in production it seems like my smtp settings (or something else) is not working, when i use sidekiq (sendt to redis).
Is there a gotcha to using sidekiq to send mails via Mailgun?
This is my create user action:
def create
#user = User.new(params[:user])
#user.roles << Role.find_by_role("member")
#user.subdomain = #user.subdomain.downcase
#user.generate_token(:confirm_email_token)
#user.confirm_email_sent_at = Time.zone.now
if #user.save
UserMailer.delay.new_user(#user.id)
redirect_to root_url, notice: "Thank you for registering."
else
render "new"
end
end
And this is my mailer:
def new_user(user_id)
#user = User.find(user_id)
mail :to => #user.email, :subject => "Welcome to Theatrical.co"
end
In development this works fine. However in produciton this is not working. I get this error messege:
Net::SMTPFatalError: 550 5.7.1 Relaying denied
It seems to be related to Mailgun not accepting redis or sidekiq to send mail.
Is there some secret setting i need to chenge?

You must be authorized to use a mail server for relaying .Unauthorized users will receive the "Relaying Denied" error. confirm that user has sufficient permission to send mail

Related

How to prevent rails mailer to open new gmail login session each time it sends an email

I have a task scheduled to send thousands of reporting emails to different users of my app. I configured Gmail to send the emails. However, after a few hundred of emails sent, I get the following error:
Net::SMTPAuthenticationError: 454 4.7.0 Too many login attempts,
please try again later.
As mentioned in this question, I set up a DNS record but it still doesn't work.
How can I change the configuration of the rails mailer in order to only open once the Gmail session in order to be able to send thousands emails in a row?
My task:
task :send_weekly_stats => :environment do
User.each do |u|
UserMailer.weekly_report_email(u.id).deliver_now
end
end
My mailer action:
def weekly_report_email(user_id)
#user = User.find(user_id)
#email = #user.email
#subject = 'Weekly Report'
mail(to: #email, subject: #subject)
end

Why are emails not sending in my rails app? Internal Server Error) Ch 20. Learn Rails tutorial by Daniel Kehoe

I am on Ch 20 of the Learn Rails tutorial 'Send Mail'. There is a create contact form and a notification email is supposed to be sent to my email address. I have set up configuration as per the book and the email is being generated correctly i the terminal log. however the email never sends to my email inbox. I am getting an internal server error. see below for details.
Completed 500 Internal Server Error in 30831ms
Net::OpenTimeout (execution expired):
app/controllers/contacts_controller.rb:10:in `create'
contacts_controller.rb
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
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
user_mailer.rb
class UserMailer < ApplicationMailer
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 Contact")
end
end
i also have my email address set in the config/secrets.yml file like so :
owner_email: <%= ENV["my_email_address#*******.com"] %>
I have also added the following to my .bashrc file as per the early chater of the book about configuration:
export SENDGRID_USERNAME="user_name"
export SENDGRID_PASSWORD="password"
export MAILCHIMP_API_KEY="long_random_api_key_here"
export MAILCHIMP_LIST_ID="list_id_here"
export OWNER_EMAIL="**********#*********.com"
so as far as i see i have set everything up according to the tutorial but the mail is not sending. any ideas why?
From the above share code description and log trace, it seems like mailer settings is not configured properly.
Note: You need to set the mailer settings based on the environment on which you are working(eg: development.rb)
Follow the below given link for the configuration of mailer settings:
http://guides.rubyonrails.org/action_mailer_basics.html#example-action-mailer-configuration

User fetching with Rails, BCrypt, Mongoid, and MongoLab fails in deployment

I'm a first-time poster trying to wade through making MongoDB play nice with Rails and Heroku. Right now, I'm getting errors that make me think there is a problem comparing user passwords (that are supposed to be obscured with Bcrypt) during log in. Eager for more advanced eyes, or a better idea of how to troubleshoot this problem.
In the app, I find and create Users like so:
def create
password_key = BCrypt::Password.create(params[:user][:password])
params[:user][:password] = password_key
newUser = User.create(params[:user])
flash[:message] = "Account created! Please log in:"
redirect_to controller: "users", action: "login"
end
def login
end
def find
username = params[:user][:username]
#user = User.where( username: username )[0]
if #user
if BCrypt::Password.new(#user.password) == params[:user][:password]
session[:user_id] = #user.id
redirect_to controller: "application", action: "home"
else
flash[:message] = "Wrong password. Try again."
redirect_to controller: "users", action: "login"
end
else
flash[:message] = "We didn't recognize that username."
redirect_to controller: "users", action: "login"
end
end
This worked fine for me in local dev. When I first deployed to Heroku, I received a "Something went wrong." error instantly upon trying to create a new user, which indicated that I didn't have my production environment configured correctly.
After more reading, I decided to use MongoLab to host my production database. I set that up with an add-on to my heroku repository following this tutorial.
Then I set my heroku env. variable MONGOLAB_URI to the appropriate URI, and included the following in my mongoid.yml file:
production:
sessions:
default:
uri: <%= ENV['MONGOLAB_URI'] %>
options:
consistency: :strong
safe: true
skip_version_check: true
Now, when I access the live app, I no longer get an error when I try to create a user! I am redirected as I would expect and receive my "Account created! Please log in:" flash message.
HOWEVEVER, when I try to login with the credentials I just saved to the database, I'm redirected and my "Wrong password. Try again." flash message fires -- which to me indicates that there is some kind of issue with how passwords are being stored or compared with the combination of BCrypt/Mongoid/MongoLab.
Help, please! Is there something I'm missing about how to use BCrypt with Mongoid? This is only happening to me in production and I'm not sure how to troubleshoot.
Thanks in advance.

How to verify if an account activation email was sent or not in Ruby on Rails web application

I was building a web app with account activation feature. I am following Michael Hartl ROR tutorial and I am not getting any account activation email to my inbox. I tried to search online on how to verify if at all an email was sent. Like there should be an SMTP server we configure to send emails right? Did not find anything helpful. Can someone help pls?
My account_activations_controller.rb
class AccountActivationsController < ApplicationController
def edit
user = User.find_by(email: params[:email])
if user && !user.activated? && user.authenticated?(:activation, params[:id])
user.activate
log_in user
flash[:success] = "Account activated!"
redirect_to user
else
flash[:danger] = "Invalid activation link"
redirect_to root_url
end
end
end
User_mailer.rb
class UserMailer < ApplicationMailer
def account_activation(user)
#user = user
mail to: user.email, subject: "Account activation"
end
end
application_mailer.rb
class ApplicationMailer < ActionMailer::Base
default from: "noreply#example.com"
layout 'mailer'
end
development.rb
config.action_mailer.raise_delivery_errors = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :test
host = 'localhost:3000'
config.action_mailer.default_url_options = { :host=> host }
Sandeep,
Keep reading the tutorial. I used an older version Michael's tutorial to learn rails, but came back when I wanted to send confirmation emails as well. I don't think you should expect an actual email to be sent (at least not in development mode), rails 4.1.x has mailer previews that you can view, which I think is what the tutorial covers.
When you get to a production environment, then you will need to set up an external server to send the actual email, but that will be in your production.rb file. If you are just learning, then you have many options, including setting up a gmail account (separate from your private account) in which to send a limited number of emails per day.
Here is the link I think you should look at:
listing 10.15
My experience with Michael Hartl's tutorial was that it was almost flawless, you may have an issue or two with your particular system setup, but I think for the most part any issues I had were related to me not following the text properly.
Hope this help, good luck!
Reading from the bottom of page 491: "Note that you will not receive an actual email in a development environment, but it will show up in your server logs. Section 10.3 discusses how to send email for real in a production environment."
So if you're wanting to follow the text explicitly, I'd checkout the goodies in Section 10.3. Hope that helps!
Add this to your config/environments/development.rb file
config.action_mailer.raise_delivery_errors = true
so you get notified if there is an error on email delivery.
Other than that you can add a field to the users table like activation_sent_at and populate it when sending the email so you can refer to it later.

Rails 2.3.5: flash[:notice] disappears after redirect_to call

Here I've got two controller methods:
def invite
if request.post?
begin
email = AccountMailer.create_invite(#user,url)
AccountMailer.deliver(email)
flash[:notice] = "Invitation email sent to #{#user.email}"
rescue
#mail delivery failed
flash[:error] = "Failed to deliver invitation"
end
redirect_to :action => :show, :id => #user.id
end
end
and
def show
#title = "User #{#user.full_name}"
end
The problem is, when I send an invitation, and get redirected to ./show, I see no messages at all. If I change redirect_to to render, the message appears. Still, isn't it intended for flash to work in the immediate subsequent requests?
BTW, I'm using Rails+Passenger setup, could it be so that redirected request goes to another application instance?
The rescue block is setting flash[:error], not flash[:notice]. Is your view actually rendering both?
Googled better and found this discussion:
http://www.mail-archive.com/activescaffold#googlegroups.com/msg04284.html
The solution is there: replace the plugin with
script/plugin install git://github.com/ewildgoose/render_component.git -r rails-2.3 --force
Though I don't use ActiveScaffold, there is some legacy code that depends on render_component plugin. Updating plugin to branch version worked, though I'm planning to get rid of it completely.

Resources