2013-07-30 59 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

不。它不是在里面发送的对象或草稿 –

相关问题