我是新的紅寶石軌道郵件。我在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指導我
你得到任何錯誤? –
沒有錯誤的軌道服務器和rails日誌 –
我可以推薦真棒[mailcatcher gem](https://github.com/sj26/mailcatcher)用於調試電子郵件。有了它,你可以看到郵件是否被髮送,即如果問題出現在你的應用程序或例如與SMTP服務器。 – lime