Rails 5 mailer Net::SMTPFatalError 发送电子邮件时

Posted

技术标签:

【中文标题】Rails 5 mailer Net::SMTPFatalError 发送电子邮件时【英文标题】:Rails 5 mailer Net::SMTPFatalError when sending email 【发布时间】:2019-10-29 19:53:50 【问题描述】:

我建立了一个非常基本的联系表格,以便人们向我发送电子邮件。这对我来说只是学习的一部分,所以它不是一个生产站点。当用户输入他们的信息并单击“发送”按钮时,它似乎会经历几秒钟到一分钟后我得到以下回击。

Net::SMTPFatalError (554 5.2.0 STOREDRV.Submission.Exception:SendAsDeniedException
.MapiExceptionSendAsDenied; Failed to process message due to a permanent 
exception with message Cannot submit message. 16.55847:6D0F0000, 
17.43559:0000000004020000000000000100000000000000, 
20.52176:140FC3870D00F01FCB030000, 20.50032:140FC3877D17401080030400, 
0.35180:D0030000, 255.23226:0A007680, 255.27962:0A000000, 
255.27962:0E000000, 255.31418:1F000130, 16.55847:A6000000, 
17.43559:00000000E0020000000000000100000000000000, 
20.52176:140FC3870D00401080030400, 20.50032:140FC3877D1710103A000000, 
0.35180:03000136, 255.23226:21000000, 255.27962:0A000000, 
255.27962:32000000, 255.17082:DC040000, 0.27745:44000000, 
4.21921:DC040000, 255.27962:FA000000, 255.1494:49000000, 0.38698:00000000, 
0.37692:1A010480, 0.37948:00000600, 5.33852:00000000534D545000010480, 
4.56248:DC040000, 7.40748:010000000000010B31333A4D, 
7.57132:00000000000000003A343731, 1.63016:32000000, 4.39640:DC040000, 
8.45434:2653E8C6336A0D409107BFF092BEB44600000000, 
5.10786:0000000031352E32302E313938372E3031333A4D5748505231374D42313433383A3437
3161356633312D326662322D346661312D383035372D3464363739346338313461300040101A010480, 255.1750:9D000000, 255.31418:80000000, 0.22753:03000236, 
255.21817:DC040000, 4.60547:DC040000, 0.21966:03001736, 4.30158:DC040000 
[Hostname=MWHPR17MB1438.namprd17.prod.outlook.com]
):

app/controllers/messages_controller.rb:11:in `create'

我的代码似乎是正确的,我认为问题出在 smtp 设置中,有人可以确认吗?我正在通过 godaddy 使用 office365,我看到很多人都在努力让它工作。

message_mailer.rb

class MessageMailer < ApplicationMailer
default from:  ENV['SMTP_EMAIL']
# Subject can be set in your I18n file at config/locales/en.yml
# with the following lookup:
#
#   en.message_mailer.contact_me.subject
#
def contact_me(message)

    @body = message.message_body

    mail to: ENV['SMTP_EMAIL'], from: message.email_address
  end
end

messages_controller.rb

class MessagesController < ApplicationController

  def new
      @message = Message.new
  end

  def create
      @message = Message.new(message_params)

      if @message.valid?
          MessageMailer.contact_me(@message).deliver_now
          redirect_to new_message_url, notice: "Message received"
      else
          render :new
      end
  end

  private

      def message_params
          params.require(:message).permit(:name, :email_address, :message_body)
      end

end

message.rb

class Message
    include ActiveModel::Model

    attr_accessor :name, :email_address, :message_body

    validates :name, :email_address, :message_body, presence: true
end

app/views/messages/new.html.erb

<%= form_for @message, url: create_message_url do |f| %>
    <%= notice %>
    <%= @message.errors.full_messages.join(', ') %>

    <%= f.text_field :name, placeholder: 'Name' %>
    <%= f.email_field :email_address, placeholder: 'Email address' %>
    <%= f.text_area :message_body, placeholder: 'body' %>
    <%= f.submit 'Send' %>

config/environments/development.rb

  config.action_mailer.perform_caching = false
  config.action_mailer.default_url_options = 
      :host => "localhost:3000"
  
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.perform_deliveries = true
  config.action_mailer.raise_delivery_errors = true

  config.action_mailer.smtp_settings = 
      address:              'smtp.office365.com',
      port:                 587,
      user_name:            ENV['SMTP_EMAIL'],
      password:             ENV['SMTP_PASS'],
      authentication:       :login,
      enable_starttls_auto: true
  

【问题讨论】:

【参考方案1】:

我通过进行以下更改解决了这个问题,并在计时器到时接受这个答案!

/config/environment.rb

# Load the Rails application.
require_relative 'application'
ActionMailer::Base.delivery_method = :sendmail # Added this line
ActionMailer::Base.smtp_settings =            # Added this line
    :domain  => 'localhost'                    # Added this line
                                              # Added this line
# Initialize the Rails application.
Rails.application.initialize!

/config/environments/development.rb

 config.action_mailer.perform_caching = false
 config.action_mailer.default_url_options = 
      :host => "localhost"                   # Removed port
 
 config.action_mailer.delivery_method = :smtp
 config.action_mailer.perform_deliveries = true
 config.action_mailer.raise_delivery_errors = true

  config.action_mailer.smtp_settings = 
      :address => 'smtp.office365.com',
      :port => 587,
      :domain => 'localhost',                # Added domain and set to localhost
      :authentication => :login,
      :user_name => ENV['SMTP_EMAIL'],
      :password => ENV['SMTP_PASS'],
      :enable_starttls_auto => true
   

【讨论】:

以上是关于Rails 5 mailer Net::SMTPFatalError 发送电子邮件时的主要内容,如果未能解决你的问题,请参考以下文章

在 Rails 的 Mailer 视图中获取格式

在 Rails 3.1 中为 Mailer 和 View 提供自定义助手

Rails 4.1 Mailer预览和设计自定义电子邮件

Rails Mailer 引用可打印替换 = 由 =3D

用于 webmail 的 rails 4 action mailer 设置 (SMTP)

Rails Action Mailer Document