无法在rails上发送电子邮件Ruby

时间:2013-07-30 08:41:22

标签: ruby-on-rails ruby smtp actionmailer

我是新的ruby on rails mailer。我在Emailer模型中创建了mailer。 我的代码如下所示。

我的动作邮件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
<{1}}中的

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", } 中,但我没有收到任何。我的代码有什么问题。 PLZ指导我

2 个答案:

答案 0 :(得分: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 => "saghir.alam@xxxxxx.com",
  :password => "xxxxxxx",
}

答案 1 :(得分:0)

您需要像

一样调用'deliver'
Emailer.contact(recipient, subject, message).deliver