Sendgrid没有发送电子邮件?

问题描述 投票:-1回答:1

我正在尝试将sendgrid实现到我的后端api rails系统中,以便当用户注册时我可以向他们发送欢迎电子邮件。在发布帖子请求并处理用户创建后,我收到了以下验证:

UserMailer#send_sign_up_email: processed outbound mail in 43.5ms
Sent mail to *******@gmail.com (185.8ms)
Date: Wed, 28 Feb 2018 16:54:05 -0800
From: *******@gmail.com
To: *******@gmail.com
Message-ID: <[email protected]>
Subject: Welcome to BottlesTonight albert jin!
Mime-Version: 1.0
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: quoted-printable

我的代码看起来与此链接https://sendgrid.com/docs/Integrate/Frameworks/rubyonrails.html完全一样。

这看起来很好,但电子邮件只是没有发送(我把星号放在这里用于电子邮件,但我实际上放了我的电子邮件,我用另一封电子邮件作为发送的默认值)。我的发件箱或收件箱中没有电子邮件。

但是,现在我考虑一下,我从来没有“登录”我的电子邮件或输入密码,因此应用程序不应该有权使用我的电子邮件发送电子邮件。我也从未对我在sendgrid帐户上创建的api密钥做任何事情。此外,对于environment.rb文件,我不知道在域中放什么,所以我把gmail.com。这些对我来说似乎都很粗略,我认为教程并不包含所有内容。有谁知道如何配置这个?我已经坚持了一段时间。

编辑:我尝试在生产中进行,但它无法正常工作。这里有更多信息:我的production.rb看起来像这样:

config.action_mailer.raise_delivery_errors = true
  config.action_mailer.delivery_method = :smtp

  config.action_mailer.default_url_options = { :host => ENV['DEFAULT_HOST'] }

  ActionMailer::Base.smtp_settings = {
    :address        => 'smtp.sendgrid.net',
    :port           => '587',
    :authentication => :plain,
    :user_name      => ENV['SENDGRID_USERNAME'],
    :password       => ENV['SENDGRID_PASSWORD'],
    :domain         => 'heroku.com',
    :enable_starttls_auto => true
  }

我有一个heroku sendgrid添加。我已经设置了heroku配置变量。在我的注册控制器中,我只是添加了一行:

UserMailer.send_sign_up_email(@current_user).deliver

我的邮件程序类看起来像:

def send_sign_up_email(user)
   @user = user
   mail(to: @user.email, subject: "Welcome! #{@user.first_name}")
   end

但是,当我在我的网站上注册时,用户会被添加到数据库中但电子邮件不会发送。有谁知道为什么,或者我如何调试?

ruby-on-rails ruby backend sendgrid
1个回答
0
投票

我建议从环境文件(即/ config / environments下的文件)中删除ActionMailer的所有配置,但以下文件除外

  # Don't care if the mailer can't send.
  config.action_mailer.perform_caching = false

  # Reference: http://stackoverflow.com/a/20770131/936494
  config.action_mailer.perform_deliveries = true
  config.action_mailer.raise_delivery_errors = true

然后创建一个初始化器/config/initializers/mail_config.rb并向其添加以下代码:

TEST_ENVS = %w(test)
FILESYSTEM_ENVS = TEST_ENVS + %w(development)

# ===========  DELIVERY METHOD SETTING
delivery_method = case Rails.env.to_sym
                    when :production, :staging, :experimental
                     :sendmail
                    when :test
                     :test
                    else
                     :smtp
                  end

ActionMailer::Base.delivery_method = delivery_method

# ===========  SMTP SETTINGS
ENVS_TO_USE_GMAIL_CONFIG = %w(development test)

gmail_config = {
  address:              'smtp.gmail.com',
  port:                 587,
  domain:               'gmail.com',
  user_name:            ENV['MAIL_USER_NAME'],
  password:             ENV['MAIL_PASSWORD'],
  authentication:       :plain,
  enable_starttls_auto: true
}

if :smtp == delivery_method
  use_gmail_config = ENVS_TO_USE_GMAIL_CONFIG.include?(Rails.env)

  smtp_settings = ( use_gmail_config ? gmail_config : {} )
  ActionMailer::Base.smtp_settings = smtp_settings
end

# =========== DEFAULT URL OPTIONS

default_url_options_settings = Settings.default_url_options
host = default_url_options_settings.host
port = default_url_options_settings.port
protocol = default_url_options_settings.protocol

default_url_options = {}
default_url_options[:host] = host if host.present?
default_url_options[:port] = port if port.present?
default_url_options[:protocol] = protocol if protocol.present?

ActionMailer::Base.default_url_options = default_url_options

Settings对象是使用config gem的一部分。如果您不想使用该gem来配置特定于env的值,那么您可以在初始化程序中对它们进行硬编码并尝试使用它。

我的/config/settings.yml看起来像

default_url_options:
  host: ''
  port: ''
  protocol: ''

和我的/config/settings/development.yml看起来像

default_url_options:
  host: 'localhost'
  port: '3000'

拥有集中式邮件程序配置有助于快速诊断邮件程序设置相关问题。

首先尝试使用Gmail帐户,如果有效,您可以确保发送电子邮件有效。只需确保在您的Gmail帐户中启用了Less Secure Apps设置即可。

© www.soinside.com 2019 - 2024. All rights reserved.