2013-07-30 55 views
0

我是新的紅寶石軌道郵件。我在mailer模型中創建了Emailer。 我的代碼如下所示。無法發送電子郵件紅寶石軌道上

我的行動郵件Emailer.rb是:

class Emailer < ActionMailer::Base 
    default from: '[email protected]' 

def contact(recipient, subject, message, sent_at = Time.now) 
     @subject = subject 
     @recipients = recipient 
     @from = '[email protected]' 
     @sent_on = sent_at 
     @body= message 
     @headers = {} 
    end 
end 

emailer_controller.rb

class EmailerController < ApplicationController 
    def sendmail 
     email = params["emailer"] 
    recipient = email["recipient"] 
    subject = email["subject"] 
    message = email["message"] 
     Emailer.contact(recipient, subject, message) 
     return if request.xhr? 
     render :text => 'Message sent successfully' 
    end 


     def index 
     render :file => 'app\views\emailer\index.rhtml' 
    end 
end 

view/emailer index.rhtml裏的文件是這樣。

<h1>Send Email</h1> 
<%= form_for :emailer,:url =>{ :action => :sendmail } do |f| %> 
    <p><label for="email_subject">Subject</label> 
     <%= f.text_field 'subject' %></p> 
    <p><label for="email_recipient">Recipient</label> 
     <%= f.text_field 'recipient' %></p> 
    <p><label for="email_message">Message</label> 
     <%= f.text_area 'message' %></p> 
    <%= f.submit "Send" %> 
<% end %> 

這裏是我的routes.rb

match "/email", to: 'emailer#index' 
    match "/emailer", to: 'emailer#sendmail' 

部分我config/enivronment/development.rb

ActionMailer::Base.delivery_method = :smtp 
ActionMailer::Base.server_settings = { 
    :address => "smtp.xxxx.com", 
    :port => 25, 
    :domain => "xxxx.com", 
    :authentication => :login, 
    :user_name => "[email protected]", 
    :password => "xxxxxxx", 
} 

我的問題是,當我點擊發送按鈕,我可以看到「消息派」,因爲它是所示emailer_controller,但我沒有收到任何。我的代碼有什麼問題。 PLZ指導我

+0

你得到任何錯誤? –

+0

沒有錯誤的軌道服務器和rails日誌 –

+0

我可以推薦真棒[mailcatcher gem](https://github.com/sj26/mailcatcher)用於調試電子郵件。有了它,你可以看到郵件是否被髮送,即如果問題出現在你的應用程序或例如與SMTP服務器。 – lime

回答

0

你需要調用 '提供' 像

Emailer.contact(recipient, subject, message).deliver 
+0

的意見,它沒有解決,同樣的問題。它說發送的電子郵件,但我沒有收到它 –

+0

你的action_mailer配置是什麼樣的? –

+0

你能告訴我你使用的Rails版本是什麼? 請參考Rails 3 http://guides.rubyonrails.org/action_mailer_basics.html#example-action-mailer-basigation-configuration –

2

更改此

Emailer.contact(recipient, subject, message) 

到 Emailer.contact(收件人,主題,郵件).deliver

Emailer.contact(recipient, subject, message)會返回一個郵件對象。您必須致電deliver才能發送。

編輯 嘗試更改設置來

ActionMailer::Base.smtp_settings = { 
    :address => "smtp.xxxx.com", 
    :port => 25, 
    :domain => "xxxx.com", 
    :authentication => :login, 
    :user_name => "[email protected]", 
    :password => "xxxxxxx", 
} 
+0

它沒有解決,同樣的問題。它說電子郵件發送,但我沒有收到它 –

+0

登錄到此帳戶'saghir.alam @ xxxxxx.com'並檢查郵件是否存在已發送郵件 – Santhosh

+0

不。它不是在裏面發送的對象或草稿 –

相關問題