Include www subdomain in rails actionmailer urls with Sendgrid - ruby-on-rails

I have a rails 5 app that sends emails using Sendgrid. Those emails have links back into the app, such as <%= link_to "Reply on platform", conversation_url(#conversation) %>.
This all worked fine and dandy, until I added my SSL cert to the site. Now, all of the mailer links point to https://domain.com, instead of simply domain.com. When visiting https://domain.com, the site fails to load and throws an error saying "This site can’t be reached".
This is probably a simply fix that's right under my nose, but I'm having trouble putting my finger on it. Any advice?

Check your mailer configuration in production at config/environments/production.rb.
Make sure you have or add a line that says:
config.action_mailer.default_url_options = {host: "www.domain.com"}

Related

Why are links inside my ActionMailer views not following the default settings?

I have a bunch of ActionMailer emails that are causing some of our users to hit a "Unsafe website" error message in their browser.
In my production.rb I have this line
config.action_mailer.default_url_options = {host: 'app.mybeautifulapp.com', protocol: 'https'}
However, the link that users are receiving in their emails look like this
http://url9522.mybeautifulapp.com/ls/click?upn=CAp2S4s83QBSYKFZSCk5WwfWNUsqJYF6px5T-2BOuGToTGZ8t9qB4P4BTaKBZqlk2vVoJVe9xuVz2PSMMTc7S2AutaZ6j0v5W1XCh75hyix90-3DJuxV_ZtyLTlYa78bQffWNrIlGC7tg8tsMt2Men5eX6g6gxS5tsHKfqDtf3RkdzuylrapUrcYdz5qdOo197RxRznJbD3B9yvPNOfWqgC-2Bh4wL2NohNLpvcJqZw-2FGgQ6LY3XiK3YJIsSWupLZw4LZVPpoJ1fOqV6ugSpk5j8jFmtwCRrxv4UdNgdxksxB6MPGA3NgdMRRNqT1Rla6sBuPyKnZW5thuGtYx4LBqkenbIwTvRMM0-3D
Both the subdomain and the protocol are being ignored. Any way I can fix this?

Rails Heroku Cloudflare Setup - www won't work

I have a rails app that's configured to force SSL:
Rails.application.configure do
config.force_ssl = true
end
It is then deployed to Heroku (free tier), where I configured to add my custom domain.
On Cloudflare, I only have these 2 records:
CNAME example.com example.herokuapp.com
CNAME www example.com
I also set the SSL option to be full.
My understanding is that, when people visit my webiste at www.example.com, they get redirected to the apex (non-www) url.
However, now whenever I visit the www version, it shows me the Heroku's No Such App page.
I have no idea what I did wrong and have tried all sorts of ways I have found online for a whole week. Hope someone can help me out, thanks!
Looking at the response from heroku 'No such App' found, please confirm if www.example.com is added as custom domain under settings?

Redirection in RubyOnRails

I just got an SSL certificate for "example.com". After making some configurations in NGINX I could finally redirect "http://www.example.com" calls and "http://example.com" calls to "https://example.com". Everything works fine, but when I type "https://www.example.com" an SSL error is shown. Is there a way to redirect "https://www.example.com" calls to "https://example.com" using some Ruby code?
Handling the redirect at the Rack level and not inside of the Rails app is my preferred way to solve this requirement.
https://github.com/jtrupiano/rack-rewrite#scheme
The link takes you to the part of the README that addresses this question. I've used this gem in the past and it works really well.

Rails app on heroku - reverse proxy to bluehost WordPress site (https to http) - Chrome/FF won't load assets. How to fix?

I have a Rails 4.1 app running on heroku, and it uses a heroku SSL endpoint and has a valid SSL cert (config.force_SSL = true). SSL on the main site works well. I also have a Wordpress blog hosted at Bluehost, not using SSL. I'm using the rack-reverse-proxy gem to present the blog as my-app.com/blog.
The issue is that Chrome and Firefox both refuse to load assets from the blog because the blog is not secure. How do I address this?
Technical details
Let's say the Rails app is https://my-app.com.
And the Wordpress blog (on Bluehost) is at http://myappblog.com.
In my Wordpress Settings -> General, I have both the Wordpress address and Site address set to https://my-app.com/blog (although this problem also happens if Wordpress address is http://myappblog.com or https://myappblog.com).
Rails routes.rb
get "/blog" => redirect("/blog/")
# I have also tried get "/blog" => redirect("/blog/"), constraints: {protocol: /http/}
config.ru
require ::File.expand_path('../config/environment', __FILE__)
use Rack::ReverseProxy do
reverse_proxy(/^\/blog(\/.*)$/,
'http://myappblog.com$1',
opts = {:preserve_host => true})
end
Console error in Chrome
[blocked] The page at 'https://my-app.com/blog/' was loaded over
HTTPS, but ran insecure content from
'http://my-app.com/blog/wp-content/themes/independent-publisher/js/skip-link-focus-fix.js?ver=20130115':
this content should also be loaded over HTTPS.
I had a similar problem. I mucked around with extra SSL certs and the Wordpress HTTPs plugin, but I could never get everything to play nicely with the rack-reverse-proxy gem.
In the end, I fixed it, with a caveat:
In your routes.rb make sure you explicitly set the full path URL to your blog site:
#OLD get "/blog" => redirect("/blog/")
get "/blog" => redirect("http://my-app.com/blog/")
I've also got "Wordpress Address" and "Site Address" set to http://my-app.com/blog.
Caveat:
With this setup, the missing content/css errors may continue if you're requiring SSL for the wp-admin area. Using define('FORCE_SSL_LOGIN', true); in your wp-config.php will let you secure the login at least, and if there's no CSS on that page, it's only affecting the people that need to log in to post to the site. To the general public, everything will look fine.
I eventually resolved this by getting an SSL certificate for the blog (myappblog.com) as well. Once the blog site and the app site were both secure, this wasn't a problem anymore.

Embedding an absolute URL to a resource in a mail where Rails app is deployed on Heroku

I want to embed a link to an article in a mail send by ActionMailer.
For that, I just want to place the absolute URL to and article in a plain text email.
I am using the following line of code in the view.
<%= article_url(#article) %>
It works correctly on my development machine, i.e. I get http://localhost:3000/articles/14 for some article with ID=14.
But when I deploy to Heroku as production site, it localhost:3000 is not replaced with the URL of my production site. Do I need to configure that somewhere? Any hints? Thanks!
I am using Rails 3.
See this option in your_app/config/environments/production.rb:
config.action_mailer.default_url_options = { :host => 'your-domain.heroku.com' }

Resources