通过Rails应用程序发送的邮件是空的

时间:2014-11-13 19:19:09

标签: ruby-on-rails email

我正在尝试在我的Rails应用上设置联系表单,但是虽然发送了电子邮件并收到了电子邮件的内容为空。

这是电子邮件的正文

!!!
%html
  %body
    %p
      = @message.name
      Schreef het volgende:
    %p= @message.email
    %p= @message.message

所以目前我的电子邮件中只有Schreef het volgende

我的MessagesController

class MessagesController < ApplicationController

    def create
      @message = Message.new(message_params)
      if @message.valid?
        # TODO send message here
        Messages.new_messages_email(@messages).deliver
        redirect_to root_url, notice: "Message sent! Thank you for contacting us."
      else
        redirect_to root_url, notice: "Something went wrong, try again!"
      end
    end

    private
    def message_params
      params.require(:message).permit(
        :name, 
        :message,
        :email
      )
    end
end

我的消息模型

class Message
  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend ActiveModel::Naming

  attr_accessor :name, :email, :message

  validates_presence_of :name
  validates :email, :email_format => {:message => 'is not looking good'}
  validates_length_of :message, :maximum => 500

  def initialize(attributes = {})
    attributes.each do |name, value|
      send("#{name}=", value)
    end
  end

  def persisted?
    false
  end
end

我的消息邮件,

class Messages < ActionMailer::Base
  default from: "info@domein.nl"

    def new_messages_email(message)

    @message = Message.new
    mail(to: 'peter@no-illusions.nl',
        subject: 'Iemand wilt contact met U')
    end
end

表格

= form_for @message do |f|
  .field
    %br/
    = f.text_field :name, :placeholder => "Naam"
  .field
    %br/
    = f.text_field :email, :placeholder => "Emailadres"
  .field
    %br/
    = f.text_area :message, :rows => 5, :placeholder => "Uw bericht"
  .actions= f.submit "Verstuur bericht", :id => "submit"

我正在尝试设置它而不将电子邮件存储在数据库中。有谁知道为什么放在字段中的内容不会在电子邮件中发送?

1 个答案:

答案 0 :(得分:2)

你和前一个问题非常相似:-)。

首先,Messages.new_messages_email(@messages).deliver。不是@messages,而是@message。第二:

class Messages < ActionMailer::Base
  default from: "info@domein.nl"

    def new_messages_email(message)

      @message = message # See the difference?
      mail(to: 'peter@no-illusions.nl', subject: 'Iemand wilt contact met U')
    end
end