【问题标题】:Rails 4 - Postmark integrationRails 4 - 邮戳集成
【发布时间】:2016-12-04 18:37:55
【问题描述】:

我正试图弄清楚如何从我的 Rails 4 应用程序发送交易电子邮件。

我找到了邮戳 gem 的教程,但我正在努力缩小教程中假设的内容(在哪里执行建议的步骤!)与我所知道的内容之间的差距。

我已经在我的 gemfile 中安装了 ruby​​ 和 rails gem:

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'] }

我想尝试制作和使用邮戳中的电子邮件模板。

邮戳 gem 文档中的说明说我需要:

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客户端的实例。

谁能告诉我接下来的步骤(或更详细的教程)?

【问题讨论】:

    标签: ruby-on-rails ruby actionmailer postmark


    【解决方案1】:

    安装 gems 后,您需要创建一个 Mailer。我假设您已经以正确的方式配置了 API 密钥等,所以我将专注于实际发送模板/静态电子邮件。

    让我们使用以下内容创建 app/mailers/postmark_mailer.rb 文件。

    class PostmarkMailer < ActionMailer::Base
      default :from => "your@senderapprovedemail.com>"
      def invite(current_user)
        @user = current_user
        mail(
          :subject => 'Subject',
          :to      => @user.email,
          :return => '74f3829ad07c5cffb@inbound.postmarkapp.com',
          :track_opens => 'true'
        )
      end
    end
    

    然后我们可以在文件 app/views/postmark_mailer/invite.html.erb 中模板化这个邮件。让我们使用以下标记来帮助您开始。

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

    您可以像使用任何其他 .html.erb 模板一样使用标签、HTML 等来编写它。

    要实际发送此电子邮件,您需要在控制器中按以下方式放置一个操作。

    PostmarkMailer.invite(current_user)
    

    或者,如果您希望在访问主页时发送此电子邮件,它很可能如下所示:

    app/controllers/home_controller.rb 包含内容

    class HomeController < ApplicationController
    
      # GET /
      def index
        PostmarkMailer.invite(current_user)
      end
    end
    

    及对应路线

    config/routes.rb 与内容

    root :to => 'home#index'
    

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

    【讨论】:

      猜你喜欢
      • 2013-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-17
      • 2013-08-08
      • 1970-01-01
      • 2013-10-04
      • 1970-01-01
      相关资源
      最近更新 更多