Reputation: 1524
I have a Rails app hosted in 'www.example.com'. My objective is to send an email containing a link to a url pointing to a specific place in my app.
The issue: In Development everything works OK but in Production my mailer link redirects to a Server DNS that can't be found.
When I checked the URL generated I noticed it points to: 'http://email.mail.example.com' instead of 'http://example.com' (where I'd have expected to point out to) but I haven't been able to change this or figure out why it happens. My current code is:
config/environments/production.rb
config.action_mailer.default_url_options = { host: 'example.com' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:authentication => :plain,
:address => "smtp.mailgun.org",
:port => 587,
:domain => ENV['MAILGUN_DOMAIN'],
:user_name => ENV['MAILGUN_USERNAME'],
:password => ENV['MAILGUN_PASSWORD']
}
the link in my mailer view
<%= link_to 'click here', url_for(@post) %>
I've tried
Given the 'email.mail' bit in the URL I'm assuming there's an issue with my domain DNS configuration. Following Mailgun's advice (when adding a domain with them) I'm using a subdomain ('mail') for transactional emails. As suggested I added a CNAME record (Host: email.mail.example.com, Value: mailgun.org) when setting this up.
So I've tried changing this record or even deleting it all together without success.
Also, I've changed the host value like below but it seems that regardless what I do the URL always starts with 'email.mail'
config.action_mailer.default_url_option = { host: 'mail.example.com' }
I've also tried changing the link to see if that provided other path like:
post_url(@post)
I wonder if you can help me with this. Should the host settings at Production.rb be different given I'm using a subdomain 'mail' for transactional emails? I've been trying to find an answer online but I haven't been able to find any similar cases.
Additional info the app is supposed to send an email to some users every time there's a new post
My mailer code
class PostsMailer < ApplicationMailer
def new_post_notification(user, post)
@user = user
@post = post
mail to: "#{@user.email}", subject: "#{@post.user.username.capitalize} just posted"
end
end
My mailer view
<body>
<h2><%= @post.user.username.capitalize %> has just posted</h2>
<p>Check <%= @post.user.username.capitalize %>'s <%= link_to 'new post here', url_for(@post) %></p>
</body>
Upvotes: 0
Views: 578
Reputation: 1524
I was completely wrong on what was happening here. The urls generated by my helpers were OK the problem is that Mailgun (as most transactional email providers) will generate some made-up hashed URLs that direct the user to your site while being able to track if the users are clicking or opening the emails.
In Mailgun those hashed URLs are appended, by default, with any subdomains set up in your email domain. So even with prefix appended the URL should reach the site.
It still not clear what is the problem but it's definitely a Mailgun one. I moved to SendGrid added a white-labeled domain with a subdomain and it worked just fine. Not to mention it was easier to verify.
SendGrid has the added benefit that you can white-label the link with a different subdomain which worked as well perfectly.
I asked Mailgun for a reason why this happened and still waiting an answer. I'm assuming there's probably something else to add in the Domain's DNS settings.
I'll post here as soon as I get an answer in case someone ran into a similar problem.
Upvotes: 0