0

我是 ruby​​ on rails mailer 的新手。我Emailermailer模型中创建。我的代码如下所示。

我的行动邮件Emailer.rb是:

class Emailer < ActionMailer::Base
  default from: 'saghir.alam@xxxxxxx.com'

def contact(recipient, subject, message, sent_at = Time.now)
      @subject = subject
      @recipients = recipient
      @from = 'saghir.alam@xxxxxxx.com'
      @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

index.rhtml 文件view/emailer看起来像这样。

<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 => "saghir.alam@xxxxxx.com",
   :password => "xxxxxxx",
}

我的问题是当我单击发送按钮时,我可以看到“已发送消息”,如图所示emailer_controller,但我没有收到任何消息。我的代码有什么问题。请指导我

4

2 回答 2

2

改变这个

 Emailer.contact(recipient, subject, message)

to Emailer.contact(recipient, subject, message).deliver

Emailer.contact(recipient, subject, message)将返回一个邮件对象。您必须调用deliver它才能实际发送它。

编辑 尝试将设置更改为

ActionMailer::Base.smtp_settings = {
  :address => "smtp.xxxx.com",
  :port => 25,
  :domain => "xxxx.com",
  :authentication => :login,
  :user_name => "saghir.alam@xxxxxx.com",
  :password => "xxxxxxx",
}
于 2013-07-30T08:46:04.697 回答
0

您需要调用“交付”,例如

Emailer.contact(recipient, subject, message).deliver
于 2013-07-30T08:46:25.680 回答