【问题标题】:Using ActionMailer in Rails to receive emails from forms在 Rails 中使用 ActionMailer 接收来自表单的电子邮件
【发布时间】:2017-12-30 00:13:17
【问题描述】:

我正在尝试使用 Rails 5 中的 ActionMailer 从联系表单接收电子邮件。但我不确定如何将表单上创建的资源的参数发送到接收方法上的电子邮件并将其发送到应用程序。

控制器/contacts_controller.rb:

class ContactsController < ApplicationController

  def index
    @contact = Contact.new
  end

  def create
    @contact = Contact.new(contact_params)
    if @contact.save
      DefaultMailer.receive(@contact).deliver
      redirect_to root_path
    else
      render :new
    end
  end

  private
  def contact_params
    params.require(:contact).permit(:name, :email, :subject, :body)
  end
end

mailers/default_mailer.rb:

class DefaultMailer < ApplicationMailer
  default from: "atendimento@lcasystems.com.br"

  def receive(email)
    mail.create(subject: email.subject, body: email.body)
  end
end

它给出一个 500 服务器错误,并在接收器方法中为 nil:NilClass 显示未定义的方法 `humanize'。

【问题讨论】:

    标签: ruby-on-rails actionmailer


    【解决方案1】:

    这是一个联系表单,因此您希望将电子邮件发送给您自己,因此您需要 to: 键值对。

    包含联系人的姓名和电子邮件也很有用,这样您就可以知道将任何回复发送到哪里。

    def receive(contact)
      mail(to: "atendimento@lcasystems.com.br", 
           subject: contact.subject, 
           body: contact.body.to_s + 
                "\n (Sender is #{contact.name} and their email is #{contact.email})")
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-09
      • 1970-01-01
      • 1970-01-01
      • 2016-11-03
      相关资源
      最近更新 更多