2014-07-03 91 views
1

我想從我的Rails應用程序的紅寶石發送郵件,我按照以下步驟:電子郵件是不是在RoR應用程序發送

首先,我用下面的命令來創建郵件:

rails generate mailer UserMailer 

我做下面user_mailer.rb頁:

class UserMailer < ActionMailer::Base 
    default from: "[email protected]" 

    def schedule 
    @mail_to = '[email protected]' #params[:Schedule][:Appointments] 
    mail :to => @mail_to, :subject => "Hi" 
    end 
end 

而且schedule是我的看法schedule.html.erb頁:

<%= form_for(:Schedule,:html => {:id => "Schedule"}) do |f| %> 
    <table class="table" width="100%"> 
     <th><font size="+1">Interview Schedule</font></th> 
    </table> 
    <table> 
     <tr> 
      <td> 
       <label style="font-size:small; font-weight:bold;">Send Appointments to:</label> 
      </td> 
      <td> 
       <%= f.text_field :mail_to_address, :style => "font-size:small;" %> 
      </td> 
     </tr> 
     <tr> 
      <td style="vertical-align:top;"> 
       <label style="font-size:small; font-weight:bold;">Message:</label> 
      </td> 
      <td> 
       <%= f.text_area :message, :style => "font-size:small; width:300px; height:100px;" %> 
      </td> 
     </tr> 
     <tr> 
      <td> 
      </td> 
      <td> 
       <%= f.submit "Send", { :style => "font-size:small" } %> 
      </td> 
     </tr> 
    </table> 
<% end %> 

在上面的代碼mail_to_address text_field我把我想要發送電子郵件,並把一些文字在message text_field並點擊Send按鈕的電子郵件地址,但郵件沒有發送。爲什麼?

而且,下面是environment.rb頁面代碼:

# Load the Rails application. 
require File.expand_path('../application', __FILE__) 

# Initialize the Rails application. 
VideoResume::Application.initialize! 
ActionMailer::Base.delivery_method = :smtp 
ActionMailer::Base.smtp_settings = { 
    :address => "smtp.gmail.com", 
    :port => 587, 
    :domain => "gmail.com", 
    :authentication => :login, 
    :charset => 'utf-8', 
    :user_name => "[email protected]", 
    :password => "password" 
} 

,並在我的應用程序/配置/環境/ development.rb我:

VideoResume::Application.configure do 
    # Settings specified here will take precedence over those in config/application.rb. 

    # In the development environment your application's code is reloaded on 
    # every request. This slows down response time but is perfect for development 
    # since you don't have to restart the web server when you make code changes. 
    config.cache_classes = false 

    # Do not eager load code on boot. 
    config.eager_load = false 

    # Show full error reports and disable caching. 
    config.consider_all_requests_local  = true 
    config.action_controller.perform_caching = false 

    # Don't care if the mailer can't send. 
    config.action_mailer.raise_delivery_errors = false 

    # Print deprecation notices to the Rails logger. 
    config.active_support.deprecation = :log 

    # Raise an error on page load if there are pending migrations. 
    config.active_record.migration_error = :page_load 

    # Debug mode disables concatenation and preprocessing of assets. 
    # This option may cause significant delays in view rendering with a large 
    # number of complex assets. 
    config.assets.debug = true 

    # Adds additional error checking when serving assets at runtime. 
    # Checks for improperly declared sprockets dependencies. 
    # Raises helpful error messages. 
    config.assets.raise_runtime_errors = true 

    config.action_mailer.perform_deliveries = true 
    config.action_mailer.raise_delivery_errors = true 
    # Raises error for missing translations 
    # config.action_view.raise_on_missing_translations = true 
end 

請建議我應該怎麼辦,等待回覆。 謝謝

+0

和....會發生什麼,當你嘗試發送電子郵件? – sevenseacat

+0

但它什麼都不做。 – user3800418

+3

您是否在結果操作中調用'UserMailer.schedule.deliver'方法? – mixan946

回答

3

從上面的評論我認爲問題是,你不叫你的郵件方法。由於@mixian945@Sachin指出你需要打電話給你的郵件。

讓我們假設你想從create_schedule方法在你的控制器發送郵件,那麼你可以通過

def create_schedule 
    # your method logic 
    UserMailer.schedule.deliver #this will call your schedule method in UserMailer class and hence send your mail 
end 

def schedule 
    mail to: "[email protected]", subject: "Hi" 
end 

而且如果要發送用戶ID或東西您提供發送方法那麼你可以打電話給你的日程安排方法一樣

def create_schedule 
    @user = User.find(params[:id]) #your logic to find your user 
    # your method logic 
    UserMailer.schedule.deliver(@user.email) 
end 

這將人低,你有你的方式像

def schedule (email) 
    mail :to => email, :subject => "Hi" 
end 

有關詳情請參閱Action Mailer Basics

+0

我想通過此電子郵件地址'abc @ gmail.com'向我發送電子郵件。可能嗎? – user3800418

+0

@ user3800418是啊!更新我的答案檢查上半年,你需要通過郵寄到:「[email protected]」,主題:「嗨」' – Mandeep

+0

我需要在你的日程安排方法中硬編碼它'我有一個問題,我的'日程表'頁面一個'經理人'樹和管理人員有一個控制器,並且在管理員控制器中我還在'user_mailer.rb'中添加了'調度程序'方法和時間表,那麼我該怎麼辦? – user3800418

0

交貨

底線是,當你創建ROR一個maileryou'll have to deliver it somehow

,你可以see here ,你基本上不得不打電話給郵件& .deliver方法得到的東西發送。這是平行於你的郵件本身的宣言:

#app/controllers/your_controller.rb 
Class YourController < ApplicationController 
    def action 
     @mailer = UserMailer.schedule 
     @mailer.deliver 
    end 
end 

-

設置

顯然,這需要有一系列的設置,以獲得它的工作正常。我們使用gmail的SMTP設置測試開發:

#config/environments/development.rb 
config.action_mailer.raise_delivery_errors = true 
config.action_mailer.perform_deliveries = true 
config.action_mailer.smtp_settings = { 
     :address    => "smtp.gmail.com", 
     :port     => 587, 
     :domain    => "testdomain.com", 
     :user_name   => "[email protected]", 
     :password    => Rails.application.secrets.gmail, 
     :authentication  => :plain, 
     :enable_starttls_auto => true 
} 
config.action_mailer.default_url_options = { 
     :host => "localhost" 
} 

這將允許您通過gmail發送郵件的服務器 - 上面是工作代碼

相關問題