# 通过自定义插件替换 email/notification.html - 有什么建议？

**URL:** https://meta.discourse.org/t/replacing-email-notification-html-via-custom-plugin-tips/87529
**Category:** Development
**Created:** [2018 年5 月 15 日 12:02 UTC](https://meta.discourse.org/t/replacing-email-notification-html-via-custom-plugin-tips/87529 "2018-05-15T12:02:47Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Maisy](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/maisy/32/119902_2.png) [@Maisy](https://meta.discourse.org/u/Maisy)
#### Post date: [2018 年5 月 15 日 12:02 UTC](https://meta.discourse.org/t/replacing-email-notification-html-via-custom-plugin-tips/87529/1 "2018-05-15T12:02:47Z")

</div>

I’ve reached a bit of an impasse and I’m looking for a spot of help.

As part of our switchover to Discourse, we want to change the design of the default email templates and obviously, as only the copy can be changed via the control panel, we figured we’d need to substitute the email templates by building a plugin that allows our email templates to persist between upgrades.

I’ve sussed out how to override the digests, thanks to this thread here:

> [@Example Plugin: Custom Activity Summary Template](https://meta.discourse.org/t/example-plugin-custom-activity-summary-template/69016?source_topic_id=61653):
>
> Hey everyone, I have created an example plugin to override default Activity Summary (digest) template: [https://github.com/muhlisbc/discourse-custom-activity-summary-template](https://github.com/muhlisbc/discourse-custom-activity-summary-template) Screenshot: [[image] ](https://raw.githubusercontent.com/muhlisbc/discourse-custom-activity-summary-template/master/custom-digest1.jpg)[[image] ](https://raw.githubusercontent.com/muhlisbc/discourse-custom-activity-summary-template/master/custom-digest2.jpg) The main purpose of this plugin is to show how to override default .erb templates on Discourse. I hope this helpful for someone.

…however, this doesn’t help when it comes to the notification.html.erb template, which seems to be impervious to `::ActionMailer::Base.prepend_view_path`.

Poking into the business end of [user\_notifications.rb](https://github.com/discourse/discourse/blob/master/app/mailers/user_notifications.rb), I noticed this:

```rb
html = UserNotificationRenderer.new(Rails.configuration.paths["app/views"]).render(
          template: 'email/notification',
          format: :html,
          locals: { context_posts: context_posts,
                    reached_limit: reached_limit,
                    post: post,
                    in_reply_to_post: in_reply_to_post,
                    classes: Rtl.new(user).css_class
          }
)

```

I assume this is what is responsible for setting the location of the template file to be used, but see no obvious way to manipulate this without overriding the method in a plugin and admittedly, my experience with Ruby is somewhat limited, so any help you could provide would be very much appreciated.

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [2018 年5 月 16 日 00:01 UTC](https://meta.discourse.org/t/replacing-email-notification-html-via-custom-plugin-tips/87529/2 "2018-05-16T00:01:03Z")

</div>

I believe we have plans to make email templates more customizable in the near future, don’t we @awesomerobot?

---

<div class="post-metadata">

### Author: ![Maisy](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/maisy/32/119902_2.png) [@Maisy](https://meta.discourse.org/u/Maisy)
#### Post date: [2018 年5 月 16 日 15:18 UTC](https://meta.discourse.org/t/replacing-email-notification-html-via-custom-plugin-tips/87529/3 "2018-05-16T15:18:18Z")

</div>

Well, since yesterdaty, I’ve managed to make _something_ happen. I don’t think it’s particularly clean, but here it is.

In my **plugin.rb** file:

```rb
# This attempts to override the Notification Emails by sliding in 
# our custom template directory first.
require_dependency 'user_notifications'
module ::UserNotificationsOverride
    def send_notification_email(opts)
        Rails.configuration.paths["app/views"].unshift(File.expand_path("../templates", __FILE__ ))
        super(opts)
    end
end

class ::UserNotifications
    prepend ::UserNotificationsOverride
end

```

…and then adjacent to the plugin file is a **template/** directory that contains customised .erb files using the same structure as below **/app/views**. So my plugin now looks like:

```plaintext
/my-email-plugin
├── plugin.rb
└── templates
    └── email
        ├── _post.html.erb
        ├── invite.html.erb
        ├── notification.html.erb
        └── template.html.erb

```

What I wanted to do was shift my custom templates folder ahead of the default, in an attempt to replicate what the `::ActionMailer::Base.prepend_view_path` line was doing, but without breaking anything else or by carbon-copying the entire `send_notification_email` method for the sake of changing one line.

If you can think of any ways to improve this, then please do share. 🙂

---

<div class="post-metadata">

### Author: ![tomve](https://avatars.discourse-cdn.com/v4/letter/t/9f8e36/32.png) [@tomve](https://meta.discourse.org/u/tomve)
#### Post date: [2019 年7 月 29 日 16:24 UTC](https://meta.discourse.org/t/replacing-email-notification-html-via-custom-plugin-tips/87529/4 "2019-07-29T16:24:28Z")

</div>

这个方法对您有效吗？现在还能用吗？

我尝试使用您在 `plugin.rb` 中的代码和模板结构来创建一个插件，但在 `containers/app.yml` 中添加插件后重新构建应用时，出现了以下错误：

> LoadError: No such file to load – user\_notifications.rb

---

<div class="post-metadata">

### Author: ![tomve](https://avatars.discourse-cdn.com/v4/letter/t/9f8e36/32.png) [@tomve](https://meta.discourse.org/u/tomve)
#### Post date: [2019 年7 月 30 日 08:36 UTC](https://meta.discourse.org/t/replacing-email-notification-html-via-custom-plugin-tips/87529/5 "2019-07-30T08:36:53Z")

</div>

我已经找出问题所在。`plugin.rb` 的代码需要放在 `after_initialize` 内部：

```
after_initialize do
    require_dependency 'user_notifications'
    module ::UserNotificationsOverride
        def send_notification_email(opts)
            Rails.configuration.paths["app/views"].unshift(File.expand_path("../templates", __FILE__ ))
            super(opts)
        end
    end

    class ::UserNotifications
        prepend ::UserNotificationsOverride
    end
end

```

现在对我来说运行正常。感谢分享这个方法！

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [2019 年7 月 31 日 05:28 UTC](https://meta.discourse.org/t/replacing-email-notification-html-via-custom-plugin-tips/87529/6 "2019-07-31T05:28:13Z")

</div>

注意，@neil 刚刚合并了以下内容：

> <https://github.com/discourse/discourse/commit/9656a21fdb0da85a16bff74120a521046867fe42>
>
> This feature adds the ability to customize the HTML part of all emails using a c…ustom HTML template and optionally some CSS to style it. The CSS will be parsed and converted into inline styles because CSS is poorly supported by email clients. When writing the custom HTML and CSS, be aware of what email clients support. Keep customizations very simple.
> 
> Customizations can be added and edited in Admin \> Customize \> Email Style.
> 
> Since the summary email is already heavily styled, there is a setting to disable custom styles for summary emails called "apply custom styles to digest" found in Admin \> Settings \> Email.
> 
> As part of this work, RTL locales are now rendered correctly for all emails.

这个新的核心功能是否解决了您创建此插件的根本需求？

---

<div class="post-metadata">

### Author: ![neil](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/neil/32/102150_2.png) [@neil](https://meta.discourse.org/u/neil)
#### Post date: [2019 年7 月 31 日 14:00 UTC](https://meta.discourse.org/t/replacing-email-notification-html-via-custom-plugin-tips/87529/7 "2019-07-31T14:00:09Z")

</div>

notification.html.erb 和 `_*post.html.erb` 模板的内容仍然无法自定义，因此我们需要在邮件定制工作的第二部分中找出解决这些问题的方法。
