diff --git a/app/controllers/comfy/admin/web_settings_controller.rb b/app/controllers/comfy/admin/web_settings_controller.rb index 49f018b17..65cb560c9 100644 --- a/app/controllers/comfy/admin/web_settings_controller.rb +++ b/app/controllers/comfy/admin/web_settings_controller.rb @@ -46,7 +46,8 @@ def subdomain_params :email_name, :email_signature, :enable_2fa, - :email_notification_strategy + :email_notification_strategy, + :track_email_opens, ) end end \ No newline at end of file diff --git a/app/controllers/mailbox/tracking_controller.rb b/app/controllers/mailbox/tracking_controller.rb new file mode 100644 index 000000000..a34d9c24e --- /dev/null +++ b/app/controllers/mailbox/tracking_controller.rb @@ -0,0 +1,17 @@ +class Mailbox::TrackingController < ApplicationController + def open + emails = params[:emails] + message_id = params[:message_id] + email_uuid = params[:email_uuid] + message = Message.find_by(id: message_id, email_message_id: email_uuid) + ip = request.ip + user_agent = request.user_agent + + if message + message.update(opened: true) + # mark as opened + end + + render json: { status: 200, code: 'OK' } + end +end \ No newline at end of file diff --git a/app/mailers/e_mailer.rb b/app/mailers/e_mailer.rb index f978dabac..d4c6923a7 100755 --- a/app/mailers/e_mailer.rb +++ b/app/mailers/e_mailer.rb @@ -3,6 +3,7 @@ def ship @message = params[:message] @message_thread = params[:message_thread] @subdomain = Subdomain.current + @recipients = @message_thread.recipients @from = if @subdomain.email_name.present? "#{@subdomain.email_name} <#{@subdomain.name}@#{ENV["APP_HOST"]}>" else @@ -30,7 +31,7 @@ def ship mail_settings = { # This will make the mail addresses visible to all (no Blank Carbon Copy) - to: @message_thread.recipients, + to: @recipients, subject: @message_thread.subject, from: @from, message_id: email_message_id(@message) diff --git a/app/views/comfy/admin/web_settings/_form.haml b/app/views/comfy/admin/web_settings/_form.haml index 17362827f..d4b330cf9 100644 --- a/app/views/comfy/admin/web_settings/_form.haml +++ b/app/views/comfy/admin/web_settings/_form.haml @@ -82,10 +82,15 @@ = f.check_box :forum_enabled %label Forum + .form-group + = f.check_box :track_email_opens + %label + Email Tracking .form-group#tracking-checkbox = f.check_box :tracking_enabled %label - Tracking + Web Visit Tracking + .form-group.consent-text = f.label :cookies_consent_ui diff --git a/app/views/e_mailer/ship.html.erb b/app/views/e_mailer/ship.html.erb index f4bf7b249..e4f0c01ed 100644 --- a/app/views/e_mailer/ship.html.erb +++ b/app/views/e_mailer/ship.html.erb @@ -3,6 +3,10 @@ + <% if @subdomain.track_email_opens %> + <% tracking_link = "#{root_url(subdomain: @subdomain.subdomain_name)}email_tracking/open/?emails=#{@recipients.join(',')}&message_id=#{@message.id}&email_uuid=#{@message.email_message_id}" %> + + <% end %> <%= @message.content %> <% if @subdomain.email_signature.present? %>
diff --git a/app/views/mailbox/message_threads/show.html.haml b/app/views/mailbox/message_threads/show.html.haml index 672e74206..e3a174e0f 100644 --- a/app/views/mailbox/message_threads/show.html.haml +++ b/app/views/mailbox/message_threads/show.html.haml @@ -12,6 +12,9 @@ .card-body .card-subtitle.mb-2.text-muted = message.from + - if !message.from && Subdomain.current.track_email_opens + .card-subtitle.mb-2.text-muted + = message.opened ? 'opened' : 'not opened yet' .card-text.bg-light.px-2.py-3 = message.content - if message.attachments.any? diff --git a/config/routes.rb b/config/routes.rb index 820cc3185..77916151d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -51,6 +51,9 @@ def self.matches?(request) end end + # email tracking + get 'email_tracking/open', to: 'mailbox/tracking#open' + # calendar / meetings functionality resources :calendars, controller: 'comfy/admin/calendars' resources :meetings diff --git a/db/migrate/20241006155850_add_tracking_for_emails.rb b/db/migrate/20241006155850_add_tracking_for_emails.rb new file mode 100755 index 000000000..819130933 --- /dev/null +++ b/db/migrate/20241006155850_add_tracking_for_emails.rb @@ -0,0 +1,6 @@ +class AddTrackingForEmails < ActiveRecord::Migration[6.1] + def change + add_column :messages, :opened, :boolean, default: false + add_column :subdomains, :track_email_opens, :boolean, default: false + end +end diff --git a/db/schema.rb b/db/schema.rb index 4c2e5e965..1a8f9a4a5 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2023_09_13_160600) do +ActiveRecord::Schema.define(version: 2024_10_06_155850) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -462,6 +462,7 @@ t.string "email_message_id" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false + t.boolean "opened", default: false t.index ["email_message_id"], name: "index_messages_on_email_message_id" t.index ["message_thread_id"], name: "index_messages_on_message_thread_id" end @@ -527,6 +528,7 @@ t.text "cookies_consent_ui", default: "\n " t.boolean "enable_2fa", default: false t.string "email_notification_strategy", default: "user_email" + t.boolean "track_email_opens", default: false t.index ["deleted_at"], name: "index_subdomains_on_deleted_at" t.index ["name"], name: "index_subdomains_on_name" end