2016-07-30 46 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' 

我希望这能回答你的问题。