简体   繁体   English

Sendgrid 不发送电子邮件?

[英]Sendgrid not sending email?

I am trying to implement sendgrid into my backend api rails system so that when a user signs up I can send them a welcome email.我正在尝试将 sendgrid 实施到我的后端 api rails 系统中,以便在用户注册时我可以向他们发送欢迎电子邮件。 After making a post request and handling user creation, I get this verification:发出帖子请求并处理用户创建后,我得到了以下验证:

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

My code looks exactly like in this linkhttps://sendgrid.com/docs/Integrate/Frameworks/rubyonrails.html .我的代码看起来与此链接https://sendgrid.com/docs/Integrate/Frameworks/rubyonrails.html 中的完全一样。

This looks all fine and well, but the email is just not sending (I put stars here for the email but I actually put in my email, and I used another one of emails as the default for sending).这看起来一切都很好,但电子邮件只是没有发送(我在这里为电子邮件添加了星星,但我实际上是在我的电子邮件中添加了电子邮件,并且我使用了另一封电子邮件作为默认发送)。 There is no email in my outbox or inbox.我的发件箱或收件箱中没有电子邮件。

However, now that I think about it, I never "logged in" with my email or entered the password, so the application shouldn't have access to send emails with my email.但是,现在回想起来,我从未使用我的电子邮件“登录”或输入密码,因此应用程序不应该有权使用我的电子邮件发送电子邮件。 I also never did anything with the api key that I made on my sendgrid account.我也从未对我在 sendgrid 帐户上创建的 api 密钥进行任何操作。 Furthermore, for the environment.rb file, I wasn't sure what to put in domain, so I put gmail.com.此外,对于environment.rb 文件,我不确定在域中放什么,所以我放了gmail.com。 These all seem kinda sketchy to me, I think the tutorial doesn't contain everything.这些对我来说似乎有点粗略,我认为教程并不包含所有内容。 Does anyone know how to configure this?有谁知道如何配置这个? I've been stuck on it for a while.我已经坚持了一段时间。

Edit: I tried doing it on production and it is not working.编辑:我尝试在生产中这样做,但它不起作用。 Here is more info: My production.rb looks like this:这是更多信息:我的 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
  }

I have a heroku sendgrid add on.我有一个 heroku sendgrid 插件。 I have set the heroku config vars.我已经设置了 heroku 配置变量。 In my registrations controller I merely added the line:在我的注册控制器中,我只是添加了以下行:

UserMailer.send_sign_up_email(@current_user).deliver

My mailer class looks like:我的邮件程序类看起来像:

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

However, when I sign up on my website, the user gets added to the database but the email is not sending.但是,当我在我的网站上注册时,用户被添加到数据库中,但电子邮件没有发送。 Does anyone know why, or how I can debug?有谁知道为什么,或者我如何调试?

I would suggest to remove all config for ActionMailer from your environment files (ie files under /config/environments ), except following ones我建议从您的环境文件(即/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

Then create an initializer /config/initializers/mail_config.rb and add following code to it:然后创建一个初始化程序/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

The Settings object is available as part of using config gem. Settings对象可作为使用config gem 的一部分使用。 If you do not want to use that gem for configuring env-specific values then you can just hard-code them in the initializer and try it.如果您不想使用该 gem 来配置特定于环境的值,那么您可以在初始化程序中对它们进行硬编码并尝试一下。

My /config/settings.yml looks like我的/config/settings.yml看起来像

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

and my /config/settings/development.yml looks like我的/config/settings/development.yml看起来像

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

Having a centralized mailer config helps in diagnosing mailer settings related issues in quick manner.拥有集中的邮件程序配置有助于快速诊断与邮件程序设置相关的问题。

First try it for Gmail account and if it works you can be sure that sending email works.首先尝试 Gmail 帐户,如果它有效,您可以确保发送电子邮件有效。 Just make sure in your Gmail account Less Secure Apps setting is enabled.只需确保在您的 Gmail 帐户中启用了“不太安全的应用程序”设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM