Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ Everything is optional; a fresh install works with zero config. Full list with c
| `allowed_embed_origins` | `[]` | Origins that may embed the widget on a page this app does not render |
| `mailer_from` | `nil` | **Required for any email at all** |
| `agent_emails` | `nil` | Array or callable |
| `mailer_layout` | `nil` | A host layout; the visitor's reply email gains an HTML part inside it. The team's stays plain text |
| `attach_files` | `true` | Needs Active Storage; inert without it |
| `storage_service` | app default | A `storage.yml` key for a dedicated bucket |
| `max_attachments`, `max_attachment_size` | `5`, `10.megabytes` | Enforced server-side |
Expand Down
14 changes: 13 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
# Changelog

## Unreleased
## 1.9.0 - 2026-12-05

- `config.mailer_layout` sends the visitor's reply email as HTML too, inside
a layout of the host's, so it can look like the rest of the host's customer
email. The plain-text part is unchanged, and the team's email stays plain
text: it is an internal nudge.
- Costs: off until a host sets it. Once set, every reply email renders a
second template inside the host's layout, which then has to work without a
request and with only the helpers Action Mailer gives it. Messages are
escaped and only the chat link is a link, so a URL a visitor or agent typed
stays text. Like the text part, the HTML copy is English apart from the link.

## 1.8.1 - 2026-12-03

- Add Slovenian text for the support widget, inbox, and emails. Translate the
recording controls, microphone messages, and widget typing indicator in 12
Expand Down
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ Everything is optional — a fresh install works with zero config. In
| `agent_display_name` | the full label | What **visitors** see — trim it or anonymise it |
| `mailer_from` | `nil` | Required for any email |
| `agent_emails` | `nil` | Who gets notified of new visitor messages |
| `mailer_layout` | `nil` | Your mail layout, to send the visitor's reply email as HTML too |
| `on_visitor_message` | no-op | Runs after a visitor writes — Slack, etc. |
| `on_agent_message` | no-op | Runs after an agent replies |
| `attach_files` | `true` | File attachments (needs Active Storage) |
Expand Down Expand Up @@ -248,8 +249,15 @@ unread stretch, not one per message. When an agent replies and the visitor is
away, the visitor gets one email (signed-in visitors automatically, guests once
they leave an address — the widget asks, gently).

Requires `mailer_from`; team notifications also need `agent_emails`. For
anything else, hook in:
Requires `mailer_from`; team notifications also need `agent_emails`. Both are
plain text. To brand the visitor's email like the rest of your customer email,
name your mail layout and it gains an HTML part rendered inside it:

```ruby
config.mailer_layout = "mailer"
```

For anything else, hook in:

```ruby
config.on_visitor_message = ->(message) { SlackNotifier.ping(message) }
Expand Down
14 changes: 10 additions & 4 deletions app/mailers/livechat/mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

module Livechat
# The two built-in notification emails. Plain text, no branding — they are
# nudges back into the conversation, not newsletters. Both require
# config.mailer_from; the callers in Livechat::Notifications enforce the
# rest of the conditions (recipients present, first unread message).
# nudges back into the conversation, not newsletters. A host that brands its
# customer email can wrap the visitor's in its own layout with
# config.mailer_layout. Both require config.mailer_from; the callers in
# Livechat::Notifications enforce the rest of the conditions (recipients
# present, first unread message).
class Mailer < ActionMailer::Base
MAX_MESSAGES = 5

Expand Down Expand Up @@ -37,7 +39,11 @@ def new_agent_reply(message)
I18n.t('livechat.mail.reply_subject', name: message.public_label, app: Livechat.app_name,
default: '%{name} replied to you on %{app}')
end
mail from: Livechat.config.mailer_from, to: @conversation.visitor_email, subject: subject
layout = Livechat.config.mailer_layout
mail(from: Livechat.config.mailer_from, to: @conversation.visitor_email, subject: subject) do |format|
format.text
format.html { render layout: layout } if layout
end
end

private
Expand Down
19 changes: 19 additions & 0 deletions app/views/livechat/mailer/new_agent_reply.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<% noun = @first_contact ? 'message' : 'reply' -%>
<p><%= @message.public_label %> sent <%= @message_count %> unread <%= @message_count == 1 ? noun : noun.pluralize %>:</p>

<% @messages.each do |message| %>
<blockquote style="margin: 0 0 16px; padding-left: 12px; border-left: 3px solid #e4e4e7;">
<%= simple_format(h(message.body.presence || '(no text)'), {}, sanitize: false) %>
<% if @attachment_names[message].any? %>
<p>Attachments: <%= @attachment_names[message].join(', ') %></p>
<% end %>
</blockquote>
<% end %>

<% if @more_count.positive? %>
<p>...and <%= @more_count %> more unread <%= @more_count == 1 ? noun : noun.pluralize %>.</p>
<% end %>

<% if @chat_url.present? %>
<p><%= link_to t('livechat.mail.continue', default: 'Continue the conversation'), @chat_url %></p>
<% end %>
4 changes: 4 additions & 0 deletions lib/generators/livechat/install/templates/initializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@
# config.agent_emails = ["support@example.com"]
# config.agent_emails = -> { User.where(admin: true).pluck(:email) }

# Send the visitor's reply email as HTML too, inside one of your layouts.
# Plain text only by default; the team's email stays plain text.
# config.mailer_layout = "mailer"

# Called with each saved Livechat::Message — notify Slack, push…
# config.on_visitor_message = ->(message) {}
# config.on_agent_message = ->(message) {}
Expand Down
6 changes: 6 additions & 0 deletions lib/livechat/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ class Configuration
# email to be sent.
attr_accessor :mailer_from

# A layout of the host's, e.g. "mailer", to send the visitor's reply email
# as HTML inside it, alongside the plain text. nil sends plain text only.
# The team's email stays plain text either way.
attr_accessor :mailer_layout

# Called with each saved Livechat::Message — wire up Slack, Noticed,
# push… Runs inline after save; keep it fast or hand off to a job.
attr_accessor :on_visitor_message, :on_agent_message
Expand Down Expand Up @@ -194,6 +199,7 @@ def initialize
@allowed_embed_origins = []
@agent_emails = nil
@mailer_from = nil
@mailer_layout = nil
@on_visitor_message = ->(_message) {}
@on_agent_message = ->(_message) {}
@rate_limit = { to: 30, within: 60 }
Expand Down
2 changes: 1 addition & 1 deletion lib/livechat/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Livechat
VERSION = '1.8.1'
VERSION = '1.9.0'
end
3 changes: 3 additions & 0 deletions test/dummy/app/views/layouts/mailer.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<html><body data-host-mail-layout>
<%= yield %>
</body></html>
29 changes: 29 additions & 0 deletions test/integration/notifications_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,33 @@ class NotificationsTest < ActionDispatch::IntegrationTest
assert_includes mail.body.to_s, 'hi!'
assert_includes mail.body.to_s, 'still there?'
end

test 'the reply email is plain text unless the host names a mail layout' do
mail = reply_mail('see https://elsewhere.example <b>now</b>')

assert_equal 'text/plain', mail.mime_type
end

test 'with a mail layout the reply email gains an HTML part inside it' do
Livechat.config.mailer_layout = 'mailer'
mail = reply_mail('see https://elsewhere.example <b>now</b>')
html = Nokogiri::HTML(mail.html_part.decoded)

assert_equal 'multipart/alternative', mail.mime_type
assert_includes mail.text_part.decoded, 'see https://elsewhere.example <b>now</b>'
assert html.at_css('body[data-host-mail-layout]')
assert_includes html.text, 'see https://elsewhere.example <b>now</b>'
assert_empty html.css('b')
assert_equal(['http://dummy.example.com/livechat/chat?locale=en'], html.css('a').map { |link| link['href'] })
end

private

def reply_mail(body)
Livechat.config.mailer_from = 'chat@example.com'
conversation = Livechat::Conversation.create!(visitor_token: SecureRandom.hex(8), locale: 'en',
visitor_email: 'guest@example.com',
page_url: 'http://dummy.example.com/pricing')
Livechat::Mailer.new_agent_reply(conversation.post_agent_message!(body: body, agent_id: 1, agent_label: 'Ada'))
end
end