diff --git a/lib/courrier/email.rb b/lib/courrier/email.rb index 5fe751e..e468248 100644 --- a/lib/courrier/email.rb +++ b/lib/courrier/email.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require "erb" + require "courrier/email/address" require "courrier/jobs/email_delivery_job" if defined?(Rails) require "courrier/email/layouts" @@ -133,7 +135,25 @@ def delivery_disabled? ENV["COURRIER_EMAIL_DISABLED"] == "true" || ENV["COURRIER_EMAIL_ENABLED"] == "false" end - def method_missing(name, *) = @context_options[name] + def method_missing(name, *) + if name == :text || name == :html + render_template(name.to_s) + else + @context_options[name] + end + end + + def render_template(format) + template_path = template_file_path(format) + + File.exist?(template_path) ? ERB.new(File.read(template_path)).result(binding) : nil + end + + def template_file_path(format) + class_path = self.class.name.gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase + + File.join(Courrier.configuration&.email_path, "#{class_path}.#{format}.erb") + end def respond_to_missing?(name, include_private = false) = true end diff --git a/test/courrier/email_test.rb b/test/courrier/email_test.rb index d83e91c..a52889d 100644 --- a/test/courrier/email_test.rb +++ b/test/courrier/email_test.rb @@ -98,4 +98,50 @@ def test_url_generation_with_missing_host assert_equal "Click here: ", email.text end + + def test_template_rendering_with_files + email_path = "tmp/test_emails" + + FileUtils.mkdir_p(email_path) + File.write("#{email_path}/test_email_with_templates.text.erb", "Hello <%= name %>!") + File.write("#{email_path}/test_email_with_templates.html.erb", "

Hello <%= name %>!

") + + Courrier.configure do |config| + config.email_path = email_path + end + + email = TestEmailWithTemplates.new( + to: "recipient@railsdesigner.com", + from: "devs@railsdesigner.com", + name: "World" + ) + + assert_equal "Hello World!", email.text + assert_equal "

Hello World!

", email.html + + FileUtils.rm_rf(email_path) +end + +def test_method_takes_precedence_over_template + email_path = "tmp/test_emails" + + FileUtils.mkdir_p(email_path) + File.write("#{email_path}/test_email_with_mixed_content.text.erb", "Template text") + File.write("#{email_path}/test_email_with_mixed_content.html.erb", "

Template HTML

") + + Courrier.configure do |config| + config.email_path = email_path + end + + email = TestEmailWithMixedContent.new( + to: "recipient@railsdesigner.com", + from: "devs@railsdesigner.com" + ) + + assert_equal "Method text", email.text + assert_equal "

Template HTML

", email.html + + FileUtils.rm_rf(email_path) +end + end diff --git a/test/fixtures/test_email_with_mixed_content.rb b/test/fixtures/test_email_with_mixed_content.rb new file mode 100644 index 0000000..10347e5 --- /dev/null +++ b/test/fixtures/test_email_with_mixed_content.rb @@ -0,0 +1,8 @@ +require "courrier/email" + +class TestEmailWithMixedContent < Courrier::Email + def subject = "Mixed Content Test" + + def text = "Method text" + # html is defined in template +end diff --git a/test/fixtures/test_email_with_templates.rb b/test/fixtures/test_email_with_templates.rb new file mode 100644 index 0000000..4aeab9b --- /dev/null +++ b/test/fixtures/test_email_with_templates.rb @@ -0,0 +1,5 @@ +require "courrier/email" + +class TestEmailWithTemplates < Courrier::Email + def subject = "Template Test" +end