SendGrid returning error when sending template email

337 Views Asked by At

I am using sendgrid-ruby gem to send an email template but it is returning an error:

Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true

My code is as follows: @sendgrid = SendGrid::API.new(api_key: 'api_key', host: 'localhost') result = @sendgrid.client.mail._('send').post(request_body: template)

My development.rb:

 config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
 config.action_mailer.asset_host = 'http://localhost:3000'

Any idea why it is returning this error? I have been through the gem docs but can't find anything regarding this error.

I am using rails 5 with ruby 2.6.5.

1

There are 1 best solutions below

0
On

In my case, I implemented it like this in config/environments/[xxx].rb:

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.default_url_options = { host: 'https://portal.com.br' }
  # Sendgrid port configuration
  config.action_mailer.smtp_settings = {
      :address              => 'smtp.sendgrid.net',
      :port                 => '587',
      :authentication       => :plain,
      :domain               => 'xxx.com.br',
      :user_name            => 'solutions',
      :password             => 'dns232323',
      :enable_starttls_auto => true
  }

  Rails.application.routes.default_url_options[:protocol] = "https"