Action mailer-通过与Ruby on Rails的联系表接收电子邮件

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

我正在使用Ruby 2.6.3Rails 5.2。我想在我的网站上创建一个联系表,其中包含用于用户电子邮件地址的框和用于消息内容的框。当用户填写这些框并单击“提交”时,我希望它向我发送电子邮件。无需用户注册或进行任何操作。

我的网站是一个一页的网站,我没有任何模型,并且我只有1个控制器:pages_controller.rb

我使用了Action mailer上的文档,但没有找到。

这里是我遵循的步骤:

rails generate mailer ContactMailer

config / environments / development.rb

config.action_mailer.default charset: 'utf-8'
config.action_mailer.perform_deliveries = true
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.raise_delivery_errors = true    
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
    address: "smtp.mail.yahoo.com",
    port: 465,
    domain: "yahoo.com",
    authentication: "plain",
    enable_starttls_auto: true,
    user_name: "[email protected]",
    password: "********"
  }

app / mailers / contact_mailer.rb

class ContactMailer < ApplicationMailer
  default from: "[email protected]"
  def contact(message)
    @message = message
    mail(to: '[email protected]', subject: 'Test')
  end
end

app / views / contact_mailer / contact.html.erb

<p>Hello world</p>
<%= @message %>

app / controllers / pages_controller.rb

class PagesController < ApplicationController
  def home
  end
  def send_contact
     ContactMailer.contact(params[:message]).deliver
  end
end

app / views / pages / home.html.erb

<form action="/send_contact" method="post">
   <input name="email" type="email" class="form-control mb-1" placeholder="Your email">
   <textarea name="message" class="form-control" placeholder="Your message"></textarea>
   <button type="submit">Submit</button>
</form>

config / routes.rb

Rails.application.routes.draw do
  root to: 'pages#home'
  post "send_contact" => "pages#send_contact"
end

这应该会在单击“提交”时给我发送电子邮件。相反,我收到此错误:

EOFError in PagesController#send_contact end of file

我不明白我在这里做错了什么。

编辑:

  • [当我使用端口587时,我遇到另一个错误:Net::SMTPAuthenticationError in PagesController#send_contact 535 5.7.0 (#AUTH005) Too many bad auth attempts.

  • 当我使用具有正确用户名和密码的gmail地址时,出现此错误:535-5.7.8 Username and Password not accepted.。我还允许“安全性较低的应用程序”并停用2FA。同样的错误。

ruby-on-rails ruby
1个回答
0
投票
https://support.google.com/accounts/answer/185833?hl=en

[我认为更改发生在2018年左右,我记得我们的Rails应用程序运行良好,突然停止了发送消息。当时,Gmail并未广泛宣布他们进行了此更改(即使完全宣布),这使我们措手不及。

创建您的应用专用密码并使用这些凭据。看看是否开始为您发送电子邮件。您的代码看起来不错。

顺便说一下,我从未能够在Rails应用程序中成功使用Yahoo帐户。

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