Rails、Devise、Postmark Gem - 使用邮戳模板设计邮件程序

Mel*_*Mel 4 ruby-on-rails devise postmark

我正在尝试弄清楚如何设置我的 Rails 4 应用程序,以便设计邮件程序使用邮戳模板通过邮戳发送。

我的 gem 文件中有 postmark-rails gem。

我一切正常,但我不知道如何将确认令牌提供给邮戳。

在邮戳我有这个模板:

To get started, please confirm your account below:

action_url_Value
Run Code Online (Sandbox Code Playgroud)

我不知道如何将以下行而不是操作 url 值放入模板中:

<%= link_to 'Confirm my account', confirmation_url(@resource, confirmation_token: @token) %></div>
Run Code Online (Sandbox Code Playgroud)

我已将以下内容添加到我的初始化程序/devise.rb 中:

config.mailer = 'PostmarkDeviseMailer'
Run Code Online (Sandbox Code Playgroud)

在 postmark_devise_mailer.rb 中,我有:

class PostmarkDeviseMailer < ActionMailer::Base
  include Devise::Mailers::Helpers

  def message
    mail(
      :subject => 'Hello from Postmark',
      :to  => 'sender@address.com',
      :from => 'sender@address.comm',
      :html_body => '<strong>Hello</strong> dear Postmark user.',
      :track_opens => 'true')
  end
end


  default from: "sender@address.com"

  def confirmation_instructions(record)
    devise_mail(record, :confirmation_instructions)
  end

  def reset_password_instructions(record)
    devise_mail(record, :reset_password_instructions)
  end

  def unlock_instructions(record)
    devise_mail(record, :unlock_instructions)
  end
Run Code Online (Sandbox Code Playgroud)

接下来的步骤对我来说不太清楚。有谁知道如何使用邮戳模板作为设计交易电子邮件的邮件程序?

Sha*_*son 6

您需要做的第一件事是在app/mailers本例中创建一个自定义邮件程序user_mailer.rb,如果您正在阅读此答案,我假设您都有一个邮戳帐户(如果没有在这里获取一个):邮戳注册,我假设您有一个模板并选择布局。

要记住的一项重要事项是确保为模板分配一个别名。这可以在模板标题上方以灰色小文本显示的小链接中找到。

其中的项目self.template_model是 100% 可定制的,并在布局/模板编辑器中显示如下:{{product_name}}

class UserMailer < Devise::Mailer
  include PostmarkRails::TemplatedMailerMixin

  helper :application
  include Rails.application.routes.url_helpers
  include Devise::Controllers::UrlHelpers

  default from: 'from_you@something.com'
  default reply_to: 'support@hammer-lane-industries.com' # Optional 

  def confirmation_instructions(record, token, opts={})
    @admin = record
    @token = token
    # this will pass the data via the postmark-api to your template / layout these fields are 100% customizable and can be cahnged in your template / layout
    self.template_model = { product_name: 'your product',
                            username: @user.username,
                            email: @user.email,
                            confirmation_url: user_confirmation_url(@resource, confirmation_token: @token, subdomain: 'add subdomain here if needed'),
                            login_url: login_root_url(subdomain: 'add subdomain here if needed'),
                            trial_length: '14-days',
                            sender_name: 'What ever you want',
                            action_url: user_confirmation_url(@resource, confirmation_token: @token, subdomain: 'add subdomain here if needed'),
                            parent_company_name: 'Your company name',
                            company_address: 'Your address'}
    mail to: @admin.email, postmark_template_alias: 'devise_user_confirmation'
  end
end
Run Code Online (Sandbox Code Playgroud)

使用解锁和密码邮件程序冲洗并重复,您需要知道的只是将令牌发送到的 URL。

要完成这一切:

您需要向您的设计资源模型添加一个小块,在本例中我使用了user.rb

class User < ApplicationRecord
  devise :database_authenticatable, :confirmable,
         :recoverable, :rememberable, :validatable,
         :timeoutable, :trackable

  def devise_mailer
    UserMailer # Must match your mailer class
  end
end
Run Code Online (Sandbox Code Playgroud)

完成后,重新启动 Rails 服务器,然后尝试一下!

我希望这可以帮助您将邮戳模板 (API) 连接到Devise 邮件程序中