在轨道上使用ruby发送邮件

时间:2011-12-28 06:40:03

标签: ruby-on-rails actionmailer ruby-on-rails-2

在我的一个rails应用程序中,我必须发送一封电子邮件。我刚配置了我的应用程序,如下所示。

我正在使用Rails v2.0。

environment.rb中:

RAILS_GEM_VERSION = '2.0.2' unless defined? RAILS_GEM_VERSION

# Bootstrap the Rails environment, frameworks, and default configuration
require File.join(File.dirname(__FILE__), 'boot')

Rails::Initializer.run do |config|
  config.action_controller.session = {
    :session_key => '_mailtest_session',
    :secret      => 'key_here'
  }
   ActionMailer::Base.delivery_method = :smtp
   ActionMailer::Base.smtp_settings = {
    :address => "smtp.gmail.com",
    :port => 25,
    :domain => "gmail.com",
    :authentication => :login,
    :user_name => "mailid@mailid.com",
    :password => "password"
  }

  ActionMailer::Base.default_content_type = "text/html"
end

模型/ notifier.rb:

class Notifier < ActionMailer::Base

     def contact(recipient, subject, message, sent_at = Time.now)
      @subject = subject
      @recipients = recipient
      @from = 'from@mail.com'
      @sent_on = sent_at
      @body["title"] = 'Signup Info'
      @body["email"] = 'mail@mail.com'
      @body["message"] = message
      @headers = {}
      return true
   end
end

控制器/管理员

class AdministratorController < ApplicationController
  def index
    recipient = "tomailid@mail.com"
    subject = "Subject"
    message = "message"
    Notifier.deliver_contact(recipient, subject, message)
  end
end

当我尝试调用索引方法时出现此错误:

Couldn't find template file for notifier/contact in ["D:/Rails/rails_apps/mailtest/app/views"]

请帮忙......

1 个答案:

答案 0 :(得分:1)

notifier下创建一个名为app/views的目录,并在其中放置contact.html.erb。 Rails的一个约定是它希望视图目录以邮件程序/控制器类命名。