lundi 13 avril 2020

How to use SendGrid to send email in Rails

I have a problem with SendGrid and Rails where my email do not arrive.

user_mailer.rb:

class UserMailer < ApplicationMailer
  default from: 'xxxxxxxxxx@gmail.com'
  layout 'mailer'
  def daily_products(user, products)
    @user = user
    @products = products
    mail(to: @user.email, subject: "Los productos top de ayer")
  end
end

View daily_products.html.erb:

<!DOCTYPE html>
<html lang="es">
<head>
  <meta charset="UTF-8">
</head>
<body>
  <p>Hola,</p>
  <p>Estos fueron los productos más votados el dia de ayer:</p>
  <ol>
    <% @products.each do |product| %>
      <li><%= link_to product.name, product_url(product) %>: <%= product.description %></li>
    <%  end %>
  </ol>
  <p>Esperamos que tengas un dia lleno de productos digitales</p>
</body>
</html>

tasks/email.rake:

namespace :email do
  desc "Sends the most voted products created yesterday"
  task daily_products: :environment do
    UserMailer.daily_products(User.first, Product.all).deliver_now
  end
end

config/initializers/SendGrip.rb:

ActionMailer::Base.smtp_settings = {
  address: 'smtp.sendgrid.net',
  port: 467,
  domain: ENV['HOSTNAME'],
  user_name: ENV['SMTP_USER_NAME'],
  password: ENV['SMTP_API_KEY'],
  authentication: 'plain',
  enable_starttls_auto: true
}

ActionMailer::Base.default_url_options[:host] = ENV["HOSTNAME"]

When I make a test it runs, however email is not arriving in the email accounts and SendGrid log doesn't show any error.

I don't know if theres any error in some configuration file, I have tried with many tutorials but the mistake persist.

Aucun commentaire:

Enregistrer un commentaire