2013-07-06 40 views
0

我想做一個簡單的聯繫我們的形式,所以我需要實現郵件。ActionMailer和ActiveModel

我創建了一個控制器:

class ContactusController < ApplicationController 
    def index 
     @contact_us = Contactus.new 
    end 

    def new 
     redirect_to(action: 'index') 
    end 

    def create 
     @contact_us = Contactus.new(params[:contactus]) 
     if @contact_us.deliver 
     flash[:notice] = "Thank-you, We will contact you shortly." 
     else 
     flash[:error] = "Oops!!! Something went wrong. Your mail was not sent." 
     end 
     render :index 
    end 
end 

因爲我不想救我加載ActiveModel使用數據:

class Contactus 
    extend ActiveModel::Naming 
    include ActiveModel::Conversion 
    include ActiveModel::Validations 
    include ActionView::Helpers::TextHelper 
    attr_accessor :name, :email, :message 

    validate :name, 
     :presence => true 

    validates :email, 
     :format => { :with => /\b[A-Z0-9._%a-z\-][email protected](?:[A-Z0-9a-z\-]+\.)+[A-Za-z]{2,4}\z/ } 

    validates :message, 
     :length => { :minimum => 10, :maximum => 1000 } 

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

    def deliver 
     return false unless valid? 
    mail(
     :to => "[email protected]", 
     :from => %("#{name}" <#{email}>), 
     :reply_to => email, 
     :subject => "Website inquiry", 
     :body => message, 
     :html_body => simple_format(message) 
     ) 
    true 
end 

def persisted? 
    false 
end 
end 

,一切工作正常。路由是好的,驗證工作,但我得到的唯一錯誤是:undefined method mail for #<Contactus:0x007f9da67173e8>

我試着與名稱聯繫我們,並在該用戶模型代碼郵件,但我得到了 以下錯誤:private method new used

我如何在ActiveModel中使用ActionMailer函數?

回答

2

是有點更詳細的關於如何建立一個郵件,運行此命令生成您的郵件:

rails generate mailer ContactMailer 

下面添加一個文件名爲app/mailers/contact_mailer

class ContactMailer < ActionMailer::Base 
    default from: #{from_email} 

    def contact_mail(contact) 
    @contact = contact 
    mail(to: #{email}, subject: #{Whatever your subject would be}) 
    end 
end 

ActionMailer使用視圖來呈現其消息的佈局。我檢查了documentation,但我找不到您在此處使用的html_body選項上的任何內容。也許嘗試刪除它並使用body: simple_format(message)或者只是創建模板app/views/mailers/contact_mailer/contact_mail.html.erb並將消息放入自己。你可以看到我創建了一個實例變量,所以如果我要創建這樣一個模板,我可以訪問該對象的所有可用屬性,所以如果你願意,可以進一步自定義。

在不同的音符...

我有點擔心,你在這裏轉發new行動的index行動。你有沒有很好的理由來改變RESTful方法? 10次​​中的9次,當我遇到private method called奇怪的問題或其他一些神祕錯誤時,我一直試圖欺騙系統。如果您重新分配new操作以創建新的Contactus對象,那麼這是否解決了您的問題?

更新SMTP設置

我的僱主對這些設置標準可能不是最好的了,但這裏是我到目前爲止已經完成。在我environments/production.rb

config.action_mailer.raise_delivery_errors = false 
config.action_mailer.perform_deliveries = true 
config.action_mailer.delivery_method = :smtp 
config.action_mailer.smtp_settings = { 
    :hash => 'of', 
    :mail => 'client', 
    :smtp => 'settings' 
} 

在我environments/development.rb我複製相同的塊,但

config.action_mailer.raise_delivery_errors = true 

在我environments/test.rb代替在第一行我添加以上皆非。相反,我添加此行

config.action_mailer.delivery_method = :test 

但這是因爲我喜歡測試電子郵件在我的RSpec測試中發送。

我不確定你的配置在你的environment.rb中看起來是什麼樣的,但你可能想要確保你通過這樣的ActionMailer進行配置。當然,在進行這些更改後重新啓動您的應用程序,看看您是否仍然存在身份驗證問題。

對於特定的身份驗證問題

我個人的SMTP設置通過Gmail進行身份驗證,所以我在這些文件中設置包括對:

:address => 'smtp.gmail.com', 
:port => #{port_number}, 
:domain => #{our_email_domain}, 
:authentication => 'plain', 
:enable_starttls_auto => true 

嘗試找尋你的portdomain。谷歌搜索應該足夠,如果你沒有這樣做的業務與自己的域名。如果您的密碼沒有被正確解釋,authenticationsmarttls設置應該有所幫助。

+0

我會嘗試一定,關於新的行動問題,我發現我創建一個郵件類的對象,但事實上它沒有construtor所以有問題:) –

+0

這將做到這一點;) –

+0

現在我有點前進,但現在變得非常奇怪errro Net :: SMTPAuthenticationError即使我進入正確的東西在ActionMailer :: Base.smtp_settings enviornment.rb任何線索關於那 –

1

您必須在app/mailers之後的create a separate class繼承自ActionMailer::Base。在該課程中,您可以撥打mail方法。您的ContactUs課程中不能直接撥打mail

一旦你設置你的郵件類可以在ContactUs這樣使用它:

ContactMailer.contact_mail(self).deliver 
+0

我使用的ActionMailer :: Base.mail( :到=> 「[email protected]」, :從=>%( 「#{名稱}」 <#{email}>) :REPLY_TO =>電子郵件, :我的電子郵件地址爲:「網站查詢」, :body =>消息, :html_body => simple_format(消息) ),但它的工作,但沒有得到郵件,雖然我收到成功消息,爲什麼? –

+0

你能更準確地做什麼步驟明智..對不起新手在這裏:( –