简体   繁体   中英

Rails Contact form, Action_mailer not sending e-mail

I am not really sure what I am doing wrong.

I see that quite many people have similar problems: Rails contact form not working , Rails 3 Contact Form, undefined method? , Contact Us form in Rails 3 and etc.

Even tough I also see that quite many consider this very simple - to build a contact form.
Been trough popular actionmailer guides: http://guides.rubyonrails.org/action_mailer_basics.html , http://railscasts.com/episodes/206-action-mailer-in-rails-3

I am not really a developer, so I find this quite confusing.

Anyway, I need to build a simple contact form, to just send the message as an email for a email account of mine. I don't want to store the messages in my db.

Here's my code:

/app/models/message.rb

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

    attr_accessor :name, :email, :content


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

   def persisted?
    false
   end
  end

app\\controllers\\messages_controller.rb

 class MessagesController < ApplicationController
  def new
   @message = Message.new
  end

  def create
   @message = Message.new(params[:message])
   MessageMailer.send(@message).deliver
   flash[:notice] = "Message sent! Thank you for contacting us."
   redirect_to root_url
  end
end

/app/mailer/message_mailer.rb

  class MessageMailer < ActionMailer::Base
    default :to => "emils.veveris@thrillengine.com"

    def send(message)
      @message = message
      mail( :subject => " Test ", :from => @message.email ) do |format|
      format.text
    end
  end
end

app/views/messages/new.html.erb

<h1> "Contact Us"  </h1>

  <%= form_for @message do |f| %>

  <p>
   <%= f.label :name %><br />
   <%= f.text_field :name %>
  </p>

  <p>
   <%= f.label :email %><br />
   <%= f.text_field :email %>
  </p>

  <p>
  <%= f.label :content, "Message" %><br />
  <%= f.text_area :content %>
  </p>
  <p><%= f.submit "Send Message" %></p>
 <% end %>

app/views/message_mailer/sent.text.erb

Message sent by <%= @message.name %>

<%= @message.content %>

and development.rb

N1::Application.configure do

    # Don't care if the mailer can't send
    config.action_mailer.perform_deliveries = true
    config.action_mailer.raise_delivery_errors = true

end

I don't see any errors in log file and I don't receive any error messages. The mail is just not delivered.

Can You please tell me what I am doing wrong?

Thanks!

This is a little old but maybe some of the concepts can help you...it's pretty much exactly what you're trying to do.

http://railscasts.com/episodes/193-tableless-model

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM