2016-07-30 48 views
1

我想弄清楚如何從我的Rails 4應用程序發送交易電子郵件。Rails 4 - 郵戳集成

我已經找到了postmark gem的教程,但是我正努力彌補教程中假定的內容(在哪裏建議的步驟!)和我所知道的之間的差距。

我已經安裝了紅寶石和我的Gemfile導軌寶石:

gem 'postmark-rails', '~> 0.13.0' 
gem 'postmark' 

我已經加入了郵戳配置我的config/application.rb中:

config.action_mailer.delivery_method = :postmark 
    config.action_mailer.postmark_settings = { :api_token => ENV['POSTMARKKEY'] } 

我想嘗試在郵戳中製作和使用電子郵件模板。

在郵戳寶石文檔的說明說,我需要:

Create an instance of Postmark::ApiClient to start sending emails. 

your_api_token = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' 
client = Postmark::ApiClient.new(your_api_token) 

我不知道如何做到這一步?我在哪裏寫第二行?我有我的API令牌存儲在我的配置。我不知道如何製作郵戳api客戶端的實例。

任何人都可以指向下一步(或更詳細的教程)嗎?

回答

3

安裝完寶石之後,您需要創建一個Mailer。我認爲你已經以正確的方式配置了API密鑰等,所以我將專注於實際發送模板/靜態電子郵件。

允許使用以下內容創建應用程序/郵件程序/ postmark_mailer.rb文件。

class PostmarkMailer < ActionMailer::Base 
    default :from => "[email protected]>" 
    def invite(current_user) 
    @user = current_user 
    mail(
     :subject => 'Subject', 
     :to  => @user.email, 
     :return => '[email protected]', 
     :track_opens => 'true' 
    ) 
    end 
end 

我們可以再模板此郵件的文件app /視圖/ postmark_mailer/invite.html.erb讓我們用下面的標記,讓你開始。

<p>Simple email</p> 
<p>Content goes here</p> 

你可以用任何其他.html.erb模板使用標記,HTML和類似方式來書寫它。

要實際發送此電子郵件,您需要按照以下方式在您的控制器中執行操作。

PostmarkMailer.invite(current_user) 

另外,如果你想這封電子郵件,在訪問網頁發送,這很可能會是這樣的:

應用程序/控制器/ home_controller.rb與內容

class HomeController < ApplicationController 

    # GET/
    def index 
    PostmarkMailer.invite(current_user) 
    end 
end 

和相應路線

config/routes.rb with content

root :to => 'home#index' 

我希望這能回答你的問題。