GitLab CE 拒绝成功发送邮件

GitLab CE refuses to successfully send emails

我花了几个小时研究各种 Stack Overflow 线程和 GitLab 文档,试图简单地让 GitLab 中的 SMTP 电子邮件发送工作。不幸的是,我没有成功。

我从 9.5.4 AWS 市场 AMI 启动了一个 GitLab CE 实例,然后将其升级到最新版本。帮助页面报告此版本:GitLab Community Edition 10.2.4 ba9acca。我是 运行 这个来自私有 EC2 实例的 GitLab 实例,通过连接到 VPN 进行访问。 GitLab 实例使用 SNAT 实例访问互联网。我的电子邮件域是 mydomain.com,内部 GitLab 域是 gitlab.corp.mydomain.com.

令人困惑的是,显然有两个地方可以指定 SMTP 设置:/etc/gitlab/gitlab.rb/opt/gitlab/embedded/service/gitlab-rails/config/initializers/smtp_settings.rb。某些设置存在于这两个地方。我看到 "Omnibus" 使用 gitlab.rb 设置,而 "built from source" 使用 smtp_settings.rb;我不确定我应该使用哪些设置,所以我在两个地方都指定了设置。

相关gitlab.rb摘录:

### Email Settings
gitlab_rails['gitlab_email_enabled'] = true
gitlab_rails['gitlab_email_from'] = 'gitlab@mydomain.com'
gitlab_rails['gitlab_email_display_name'] = 'GitLab'
gitlab_rails['gitlab_email_reply_to'] = 'noreply@mydomain.com'
gitlab_rails['gitlab_email_subject_suffix'] = ''

### GitLab email server settings
###! Docs: https://docs.gitlab.com/omnibus/settings/smtp.html
###! **Use smtp instead of sendmail/postfix.**

gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.office365.com"
gitlab_rails['smtp_port'] = 587
gitlab_rails['smtp_user_name'] = "me@mydomain.com"
gitlab_rails['smtp_password'] = "<redacted>"
gitlab_rails['smtp_domain'] = "mydomain.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = false

###! **Can be: 'none', 'peer', 'client_once', 'fail_if_no_peer_cert'**
###! Docs: http://api.rubyonrails.org/classes/ActionMailer/Base.html
gitlab_rails['smtp_openssl_verify_mode'] = 'none'

smtp_settings.rb:

# To enable smtp email delivery for your GitLab instance do the following:
# 1. Rename this file to smtp_settings.rb
# 2. Edit settings inside this file
# 3. Restart GitLab instance
#
# For full list of options and their values see http://api.rubyonrails.org/classes/ActionMailer/Base.html
#
# If you change this file in a Merge Request, please also create a Merge Request on https://gitlab.com/gitlab-org/omnibus-gitlab/merge_requests

if Rails.env.production?
  Rails.application.config.action_mailer.delivery_method = :smtp

  ActionMailer::Base.delivery_method = :smtp
  ActionMailer::Base.smtp_settings = {
    address: "smtp.office365.com",
    port: 587,
    user_name: "me@mydomain.com",
    password: "<redacted>",
    domain: "mydomain.com",
    authentication: :login,
    enable_starttls_auto: true,
    openssl_verify_mode: 'none', # See ActionMailer documentation for other possible options
  }
end

使用 GitLab 实例中的 telnet,我验证了我能够在端口 587 上建立到 smtp.office365.com 的出站连接。

我找到了一个解释如何测试电子邮件发送的页面 here。执行测试后,我注意到 FromReply-To headers 不正确,这导致 Office 365 正确拒绝我的电子邮件,因为它来自无效发件人:

Loading production environment (Rails 4.2.8)
irb(main):001:0> Notify.test_email('me@mydomain.com', 'Subject', 'Body').deliver_now

Notify#test_email: processed outbound mail in 120.6ms

Sent mail to me@mydomain.com (14573.5ms)
Date: Tue, 12 Dec 2017 04:42:39 +0000
From: GitLab <gitlab@gitlab.corp.mydomain.com>
Reply-To: GitLab <noreply@gitlab.corp.mydomain.com>
To: me@mydomain.com
Message-ID: <redacted>
Subject: Subject
Mime-Version: 1.0
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit
Auto-Submitted: auto-generated
X-Auto-Response-Suppress: All

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><p>Body</p></body></html>

Net::SMTPFatalError: 550 5.7.60 SMTP; Client does not have permissions to send as this sender [CY1PR13MB0380.namprd13.prod.outlook.com]

        from /opt/gitlab/embedded/lib/ruby/2.3.0/net/smtp.rb:974:in `check_response'
        from /opt/gitlab/embedded/lib/ruby/2.3.0/net/smtp.rb:919:in `data'
        from /opt/gitlab/embedded/lib/ruby/2.3.0/net/smtp.rb:664:in `block in send_message'
        from /opt/gitlab/embedded/lib/ruby/2.3.0/net/smtp.rb:859:in `rcptto_list'
        from /opt/gitlab/embedded/lib/ruby/2.3.0/net/smtp.rb:664:in `send_message'
        from /opt/gitlab/embedded/lib/ruby/gems/2.3.0/gems/mail-2.6.6/lib/mail/network/delivery_methods/smtp.rb:112:in `block in deliver!'
        from /opt/gitlab/embedded/lib/ruby/2.3.0/net/smtp.rb:522:in `start'
        from /opt/gitlab/embedded/lib/ruby/gems/2.3.0/gems/mail-2.6.6/lib/mail/network/delivery_methods/smtp.rb:111:in `deliver!'
        from /opt/gitlab/embedded/lib/ruby/gems/2.3.0/gems/mail-2.6.6/lib/mail/message.rb:2149:in `do_delivery'
        from /opt/gitlab/embedded/lib/ruby/gems/2.3.0/gems/mail-2.6.6/lib/mail/message.rb:237:in `block in deliver'
        from /opt/gitlab/embedded/lib/ruby/gems/2.3.0/gems/actionmailer-4.2.8/lib/action_mailer/base.rb:543:in `block in deliver_mail'
        from /opt/gitlab/embedded/lib/ruby/gems/2.3.0/gems/activesupport-4.2.8/lib/active_support/notifications.rb:164:in `block in instrument'
        from /opt/gitlab/embedded/lib/ruby/gems/2.3.0/gems/activesupport-4.2.8/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
        from /opt/gitlab/embedded/lib/ruby/gems/2.3.0/gems/activesupport-4.2.8/lib/active_support/notifications.rb:164:in `instrument'
        from /opt/gitlab/embedded/lib/ruby/gems/2.3.0/gems/actionmailer-4.2.8/lib/action_mailer/base.rb:541:in `deliver_mail'
        from /opt/gitlab/embedded/lib/ruby/gems/2.3.0/gems/mail-2.6.6/lib/mail/message.rb:237:in `deliver'
        from /opt/gitlab/embedded/lib/ruby/gems/2.3.0/gems/actionmailer-4.2.8/lib/action_mailer/message_delivery.rb:85:in `deliver_now'
        from (irb):1
        from /opt/gitlab/embedded/lib/ruby/gems/2.3.0/gems/railties-4.2.8/lib/rails/commands/console.rb:110:in `start'
        from /opt/gitlab/embedded/lib/ruby/gems/2.3.0/gems/railties-4.2.8/lib/rails/commands/console.rb:9:in `start'
        from /opt/gitlab/embedded/lib/ruby/gems/2.3.0/gems/railties-4.2.8/lib/rails/commands/commands_tasks.rb:68:in `console'
        from /opt/gitlab/embedded/lib/ruby/gems/2.3.0/gems/railties-4.2.8/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
        from /opt/gitlab/embedded/lib/ruby/gems/2.3.0/gems/railties-4.2.8/lib/rails/commands.rb:17:in `<top (required)>'
        from bin/rails:9:in `require'
        from bin/rails:9:in `<main>'

我不知道那些 FromReply-To 地址来自哪里。它们没有出现在我的 gitlab.rb 配置中。我确定这就是电子邮件发送失败的原因。

问题:

正如您在评论中所说,错误是您对设置进行了评论。此外,您应该注意到,对于 Gitlab 的 Omnibus 安装,您应该使用的唯一设置是 gitlab.rb

此文件用于生成其他设置文件,如 smtp_settings。 如果您 运行 命令 sudo gitlab-ctl reconfigure 将执行文件的重新生成,这也会在您升级实例时触发。