首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails,Devise,Postmark Gem -为devise邮件程序使用邮戳模板

Rails,Devise,Postmark Gem -为devise邮件程序使用邮戳模板
EN

Stack Overflow用户
提问于 2016-05-05 06:59:52
回答 2查看 1K关注 0票数 3

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

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

我已经把所有的东西都准备好了,除了我想不出怎么给邮戳加上确认令牌。

在邮戳中,我有这个模板:

代码语言:javascript
复制
To get started, please confirm your account below:

action_url_Value

我不知道如何将下面这行代码放入模板中,而不是放入action url值中:

代码语言:javascript
复制
<%= link_to 'Confirm my account', confirmation_url(@resource, confirmation_token: @token) %></div>

我已经在我的初始化器/devise.rb中添加了以下内容:

代码语言:javascript
复制
config.mailer = 'PostmarkDeviseMailer'

在postmark_devise_mailer.rb中,我有:

代码语言:javascript
复制
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

接下来的步骤对我来说不太清楚。有没有人想出了如何使用邮戳模板作为设计交易电子邮件的邮件?

EN

回答 2

Stack Overflow用户

发布于 2016-05-06 09:15:15

Postmark help desk的回应是:不幸的是,Postmark模板不能很好地适应传统的Rails生态系统。有关详细信息,请参阅我的回复:

https://github.com/wildbit/postmark-rails/issues/53

总而言之,通过将Postmark API与Postmark模板一起使用,您可能会取代ActionMailer,但将它们混合在一起会带来巨大的挑战。不幸的是,我们从来没有尝试过Devise的"ActionMailer-free“方法,所以我不能在这里帮助你。除非您有充分的理由不这样做,否则我建议使用ActionMailer模板,并使用postmark-rails gem作为交付适配器。众所周知,这种方法与Devise配合使用效果很好。如果你有任何问题请告诉我。

票数 3
EN

Stack Overflow用户

发布于 2020-07-19 03:29:21

你需要做的第一件事是在app/mailers中创建一个自定义邮件程序在这个例子中它是user_mailer.rb如果你正在阅读这个答案,我假设你们两个都有一个邮戳帐户,如果没有在这里抓取一个:Postmark Registration和我假设你已经选择了一个模板和布局。

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

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

代码语言:javascript
复制
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

用解锁和密码邮件程序冲洗和重复,你所需要知道的就是你将要发送令牌到的urls。

完成这一切的

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

代码语言:javascript
复制
class User < ApplicationRecord
  devise :database_authenticatable, :confirmable,
         :recoverable, :rememberable, :validatable,
         :timeoutable, :trackable

  def devise_mailer
    UserMailer # Must match your mailer class
  end
end

完成此操作后,重新启动rails服务器,并开始尝试!

我希望这能帮助您将邮戳模板挂接到Devise mailers

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37039500

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档