2

I am using SendGrid mailer on Ruby and Rails framework. In password reset email template we are sending a password reset link which looks like the following format (https://subdomain.domainname.com/password_reset/token/?some_other_params). Most of the time the password reset link is emailed to recipient in correct format but for some customer it is not sending the proper link. The issue we noticed is "the dot is missing between (subdomain and domainname) or (domainname and com) randomly and the resulting password reset link to customer looks like (https://subdomaindomainname.com/password_reset/token/?some_other_params) which is a wrong link. This issue is happening only on production and occur very random.

I verified our variable which have domain name in our source code and verified the code which is generating the url and also thoroughly tested this and their is no issue on our source code. On google i see this question. I did't understand how to programatically solve this on SendGrid email client.

mailer.rb

class Mailer < ActionMailer::Base
  sendgrid_category Rails.env
  default_url_options[:host] = APP_URL

end

config/environments/production.rb

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
    :address => "smtp.sendgrid.net",
    :user_name => ENV['SENDGRID_USERNAME'],
    :password => ENV['SENDGRID_PASSWORD'],
    :domain => "subdomain.domainname.com",
    :port => 587,
    :authentication => "plain",
    :enable_starttls_auto => true
}

APP_URL = "subdomain.domainname.com"

mailer_helper.rb

  def link_to_with_ga(*args)
    args[1] = append_ga args[1], 'html'
    link_to *args 
  end

  def append_ga(link, utm_content)
    link << "#{link.include?('?') ? '&' : '?' }#{@ga_tag}&utm_content=#{utm_content}"
  end

_reset_password_view.rb

  <%= link_to_with_ga(t("users.reset_password"), reset_password_url(:token => @token, :locale=>@user_locale,:protocol => ( Rails.env.eql?('development') ? 'http' : 'https' )),:id => 'reset_link') %>

Please help me in solve this issue.

Thank you

user2086641
  • 4,331
  • 13
  • 56
  • 96
  • How do you set the domain in the first place? Is it hardcoded in the mailer body or did you configure `config.action_mailer.default_url_options`? – spickermann Feb 22 '20 at 15:26
  • Domain name is set at environment/production.rb file as APP_URL = "subdomain.domainname.com". I am using the APP_URL through out the application where ever required. – user2086641 Feb 22 '20 at 15:34
  • And how do you set the host in the email? Something like: `<%= link_to ... _url(host: APP_URL) %>`? – spickermann Feb 22 '20 at 16:06
  • @spickermann Yes, In view i have something like you mentioned. But that actually calls the Mailer.rb to get the full URL with password reset token. – user2086641 Feb 22 '20 at 16:19
  • And how do you set the domain in the method in the mailer? How does that helper method look like? – spickermann Feb 22 '20 at 17:14
  • Can you show what code generates that link or how it's forwarded to SendGrid? – tadman Feb 23 '20 at 04:43
  • @spickermann I have updated the source code of mailer and helper method in question. Please review and help me. Thank you. – user2086641 Feb 24 '20 at 10:04
  • @tadman I have updated the source code of mailer and helper method which generate the link in question. Please review and help me. Thank you. – user2086641 Feb 24 '20 at 10:05
  • Do you [set your default `host` for ActionMailer](https://stackoverflow.com/questions/798917/how-do-i-configure-the-hostname-for-rails-actionmailer)? I don't see any host settings here. – tadman Feb 24 '20 at 16:23
  • @tadman default_url_options[:host] value was included on Mailer class. Please review mailer.rb on question. Is that is good enough or do i need to set that to production.rb, but using pry i see the host name is printing correctly in dev env. – user2086641 Feb 25 '20 at 05:45
  • That's certainly closer. ActionMailer can log the output of the mail contents, so it's worth checking that's rendered correctly. – tadman Feb 25 '20 at 17:12

0 Answers0