Using Sidekiq with Mailer to send asynchronous emails [RAILS] - ruby-on-rails

So I am currently trying to use the Rails gem 'sidekiq' to send a large number of emails asynchronously, so it does not bog down the load time for the next page.
Everything I have works except for this:
FeedbackMailer.invite_contact(current_user, current_business, contact).deliver()
This is my own mailer and a set of paramters which it must take to create the email for the client.
The error that gets thrown by sidekiq is this:
Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true
The default_url_options[:host] is set in the application.rb, but does not seem to get read here. I also tried setting only_path: true and got the same error still.
I can send emails just find when I am outside one of sidekiq's workers, the problem only exists there. Anyone know of a workaround so that emails can be sent from sidekiq's workers?
Also before you answer, using Mailer.delay doesnt really solve my problem, because there is alot of other code around the mailing that must go off during every iteration of the loop.

This error happens when your mail is not configured properly. Check that you have something like this in the relevant environment file (production.rb or staging.rb)
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => 'smtp.something.com',
:port => 587,
:domain => 'yourdomain.com',
:user_name => 'account-admin#yourdomain.com',
:password => 'your secure password',
:authentication => 'plain',
:enable_starttls_auto => true
}

Related

ruby on rails tutorial ch 10 could get mail but links are wrong also Bitbucket could not add the code

I've partially solved this problem: I could get links with confirmation and resetting password.
The problem is that those links pointing to the wrong app and I need to adjust name of app manually in order to get the right redirection. Heroku representative said that those wrong address related to code....
1.Where in the Michael Harti ruby on rails tutorial could I find and change the code?
I've used Cloud 9 and Bitbucket as repository. I've created the new app on Bitbucket clone my existing app from Bitbucket to Cloud and want to push it to Bitbucket in order to make some changes.
But Bitbucket doesn't allow me to do saying that there is the existing app. clone probably doesn't work in this case.
2.How to create exactly the same app as existing in Bitbucket but with different name on Cloud9 and push it to Bitbucket with different name?
thanks.
//config/environments/production.rb
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
host = 'tatyanaa.herokuapp.com'
config.action_mailer.default_url_options = { host: host }
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'heroku.com',
:enable_starttls_auto => true
}
If you see that output in console, it means your email was actually sent.
By "sent" I mean that Rails in development mode, instead of really sending the email (so you don't have to setup anything), it outputs it to the console like you can see there.
This configuration can be changed in development.rb by setting
config.action_mailer.delivery_method = :stmp
config.action_mailer.perform_deliveries = true
You are then required to properly configure the smtp settings, check the Action Mailer configuration guide for additional details, should be something like:
config.action_mailer.delivery_method = :stmp
config.action_mailer.perform_deliveries = true
config.action_mailer.smtp_settings = {
address: "foo.bar.com",
user_name: "someone",
password: "apassword"
}
However, I highly suggest you to keep the default configuration and let it log in console instead of sending real emails.
Did you properly write the code in production.rb ?
Here is mine (I've followed the same tutorial as well and it worked):
(production.rb)
....
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
host = 'my_heroku_app_name.herokuapp.com'
config.action_mailer.default_url_options = { host: host }
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'heroku.com',
:enable_starttls_auto => true
}
....
Also did you properly configure sendgrid as an addon on heroku?
(terminal window)
heroku addons:create sendgrid:starter
If you are stuck and REALLY don't know how to proceed, I recommend you to do the tutorial again from the start. It may seem inefficient to go through the same process again when (I assume) you haven't finished the entire tutorial itself.
Believe me, I've also had the same situation where I "thought" I've followed the every step of the tutorial correctly before the app stopped working for some reason and when I restarted it again from the beginning and followed the steps again, this time with a more relaxed mind, the code worked perfectly :) And I've gone through the same tutorial "4 times" before I could finally "get it". I think it would be good for you if you repeat the same process again as it helps solidify your knowledge AND solve the problem.

Why is my Heroku app not sending emails in production with Sendgrid?

My devise emails work fine in development.
But now that I have pushed to Heroku and am using the sendgrid add-on, they don't get sent. I don't get an error. It seems like it is sent just fine, it is just that it never actually reaches my inbox.
This is my config/environment/production.rb file:
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.default_url_options = { :host => 'http://myapp.herokuapp.com' }
config.action_mailer.smtp_settings = {
:user_name => ENV["SENDGRID_USERNAME"],
:password => ENV["SENDGRID_PASSWORD"],
:address => 'smtp.sendgrid.net',
:domain => 'myapp.herokuapp.com',
:port => 587,
:authentication => :plain,
:enable_starttls_auto => true
}
I checked those config vars on Heroku, and they return valid results.
Any ideas?
P.S. I don't have my domain pointing to the Heroku App yet, I just want to test it with the stock Heroku settings first.
Everything look good, did you check if the sendgrid account is provisioned?
If you are on heroku, go to the sendgrid panel to finish the setup, just visit the app profile and under resources(which is the default) you should see the sendgrid add-on, click it and just make sure you have everything setup. In this case it will ask you to setup a template, but you can 'skip' this.
Hope this helps!
This was happening to me too, or something like it. I was using the emails to only send to one email address, for my personal notifications.
It turns out that the email address "bounced" the first time and was being "suppressed" at Sendgrid for future sends. I would check the "suppressions" tab to make sure that's not happening to you.

Rails Devise Mailer: No Received Messages in Inbox

I'm using Rails 4 and Devise 3. I need to send confirmation e-mails for production. These are the SMTP configs for my config/environments/production.rb
config.action_mailer.default_url_options = { :host => 'smtp.gmail.com' }
config.action_mailer.raise_delivery_errors = false
config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:tls => true,
:port => '587',
:user_name => 'my_email#gmail.com',
:password => 'my_password',
:authentication => 'plain',
:enable_starttls_auto => true
}
Logs say that the e-mail's been sent. However, I don't see anything in the inbox. (yes, mailcatcher is off)
Another question, do the configs of the development file affect the production's environment's in any ways? They shouldn't, correct?
Another important question: Using the way above, how many e-mails can be handled? For an example, if I used a third party say, Mandrill, would be better because up for tens of thousands of e-mails can be handled. What about this way?
P.S I've already tried Mandrill and it worked just fine. I am requested not to use a third party though so I won't be able to use Mandrill.
Lastly, is there any other way of sending the confirmation e-mails from the Rails Devise that I'm unaware of yet? Or are there any other configurations that I need to do OUTSIDE OF RAILS to make this work since I won't be using a third party?
Please make sure you have enter correct host name .
config.action_mailer.default_url_options = { :host => 'your domain name' }
As Rails configuration standard if your application running on local machine it loads development env. file settings, if it is production then it load production env. file settings.
I prefer to use sendgrid or Mandrill by MailChimp, if you ave large application then it is better to use 3rd party addons, for small application you can use Gmail .
hope this will help you :)

Rails Mailer is not actually sending emails

I have just started learning ruby on rails and this was one of the tutorials i followed:
http://railscasts.com/episodes/206-action-mailer-in-rails-3
I followed it to a T and I have yet to receive anything in my gmail account. Is it fair to assume some updates to ruby have changed how you do things?
My best guess from reading everything over the net, the tutorial is missing actually setting the mailer to use smtp setting.
Here are my smtp settings:
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "alexei.herokuapp.com",
:user_name => "mygmailaccount",
:password => "mygmailpassword",
:authentication => "plain"
:enable_starttls_auto => true
}
Could it be that i am still in local environment when testing this?
In that Railscast, Ryan has a line in there that doesn't send any email in development:
Mail.register_interceptor(DevelopmentMailInterceptor) if Rails.env.development?
Did you put that in? If so, then no email will go out when you run your app locally in development.
Try removing it.
Also open myapp/logs/development.log to see if what shows up there when you try to send an email.

How do I enable Devise to send out confirmation emails on Heroku?

I am on heroku so am not clear where and how to set it up so that devise can send out emails.
I actually have two directions to go:
I am using sendgrid, so am wondering how it works with that.
For my hand-rolled mailers, I use PostageApp, which I'd prefer because it allows me to see what's going on with my email. The way I use PostageApp is my Mailers are a class of PostageApp's mailer.
Thanks.
In Rails 3 I used the following settings in config/environments/production.rb
# Disable delivery errors, bad email addresses will be ignored
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options = { :host => '##YOUR_PROJECTNAME##.heroku.com' }
ActionMailer::Base.smtp_settings = {
:address => "smtp.sendgrid.net",
:port => 25,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => ENV['SENDGRID_DOMAIN'],
:authentication => :plain
}
Note: you'll need to substitute in your project name - but all those ENV variables are populated for you automatically by heroku.
I just wanted to let you guys know that with the help of one of our customers, we have been able to add integration instructions for Devise with Postage to our documentation. He has also told us that he is updating the code to work with the newer, modularized version of Devise, and he will give us the code as soon as it is ready.

Resources