【问题标题】:Sendgrid not sending email?Sendgrid不发送电子邮件?
【发布时间】:2018-08-09 02:48:07
【问题描述】:

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

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: <5a974f2d39c92_c5b2abcd76769fc423e0@albert-VirtualBox.mail>
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 config vars。在我的注册控制器中,我只是添加了以下行:

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

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

【问题讨论】:

  • 不要编写指向您的代码的链接。在这里写代码。如果它很长,请在此处提取与您的问题相关的最小部分。
  • 张贴development.rb 设置邮件程序的文件。
  • 请发布您针对相关环境的相关邮件配置。这是您机器上的开发问题还是服务器上的生产问题?
  • 嗨!我编辑了我的帖子,这样更好吗?
  • 转到 Rails 控制台并设置 user = User.last 然后运行 ​​UserMailer.send_sign_up_email(user).deliver - 它是否正确弹出电子邮件?

标签: ruby-on-rails ruby backend sendgrid


【解决方案1】:

我建议从您的环境文件(即 /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 设置即可。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-30
    • 2018-11-08
    • 1970-01-01
    相关资源
    最近更新 更多