Browse Source

Maintenance: Enable mail to file.

Tobias Schäfer 2 years ago
parent
commit
1e9517ff14
2 changed files with 36 additions and 4 deletions
  1. 8 4
      app/models/channel/driver/sendmail.rb
  2. 28 0
      spec/models/channel/driver/sendmail_spec.rb

+ 8 - 4
app/models/channel/driver/sendmail.rb

@@ -16,15 +16,19 @@ class Channel::Driver::Sendmail
     end
 
     mail = Channel::EmailBuild.build(attr, notification)
-    mail.delivery_method delivery_method
+    delivery_method(mail)
     mail.deliver
   end
 
   private
 
-  def delivery_method
-    return :test if Rails.env.test?
+  def delivery_method(mail)
+    if ENV['ZAMMAD_MAIL_TO_FILE'].present?
+      return mail.delivery_method :file, { location: Rails.root.join('tmp/mails'), extension: '.eml' }
+    end
+
+    return mail.delivery_method :test if Rails.env.test?
 
-    :sendmail
+    mail.delivery_method :sendmail
   end
 end

+ 28 - 0
spec/models/channel/driver/sendmail_spec.rb

@@ -0,0 +1,28 @@
+# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
+
+require 'rails_helper'
+
+RSpec.describe Channel::Driver::Sendmail do
+  context 'with env var ZAMMAD_MAIL_TO_FILE present' do
+
+    let(:address) { Faker::Internet.email }
+    let(:body)    { Faker::Lorem.sentence(word_count: 3) }
+    let(:file)    { Rails.root.join("tmp/mails/#{address}") }
+
+    around do |example|
+      ENV['ZAMMAD_MAIL_TO_FILE'] = '1'
+      FileUtils.rm_f(file)
+      example.run
+      FileUtils.rm_f(file)
+      ENV.delete('ZAMMAD_MAIL_TO_FILE')
+    end
+
+    it 'creates mail file', :aggregate_failures do
+      described_class.new.send({}, { to: address, from: address, body: body })
+      expect(file).to exist
+      content = File.read(file)
+      expect(content).to match(%r{#{body}})
+      expect(content).to match(%r{#{address}})
+    end
+  end
+end