Skip to content

Instantly share code, notes, and snippets.

@Lukom
Last active February 24, 2022 19:26
Show Gist options
  • Save Lukom/ac5116e308c4a32e46f6827744c88991 to your computer and use it in GitHub Desktop.
Save Lukom/ac5116e308c4a32e46f6827744c88991 to your computer and use it in GitHub Desktop.
# app/admin/email_previews.rb
ActiveAdmin.register_page 'Email Previews' do
content do
div '.'
end
sidebar 'Mail Previews' do
Dir['app/mailer_previews/*_preview.rb'].each do |preview_path|
preview_mailer = File.basename(preview_path, '.rb')
mailer = preview_mailer.chomp('_preview')
div { strong { mailer } }
preview_mailer_class = preview_mailer.camelize.constantize
mails = preview_mailer_class.public_instance_methods(false).map(&:to_s).sort
mails.each do |mail|
div { link_to mail, {action: :preview, mailer: mailer, mail: mail} }
end
br
end
end
page_action :preview do
mailer_preview_class = "#{params[:mailer]}_preview".camelize.constantize
@email = mailer_preview_class.new.send(params[:mail])
ActionMailer::InlinePreviewInterceptor.previewing_email(@email)
part = @email.find_first_mime_type('text/html') || @email
@email_content = part.decoded
end
end
-# app/views/admin/email_previews/preview.haml
%p
!= "<b>From:</b> #{h @email.header['from']}"
!= "<br><b>Reply-To:</b> #{h @email.header['reply-to']}" if @email.reply_to
!= "<br><b>To:</b> #{h @email.header['to']}"
!= "<br><b>CC:</b> #{h @email.header['cc']}" if @email.cc
!= "<br><b>Subject:</b> #{h @email.subject}"
- if @email.attachments.present?
%br
%b Attachments:
- @email.attachments.each do |a|
- filename = a.respond_to?(:original_filename) ? a.original_filename : a.filename
= link_to filename, "data:application/octet-stream;charset=utf-8;base64,#{Base64.encode64(a.body.to_s)}", download: filename
%iframe(srcdoc="#{@email_content}" style="width: 100%;height: 500px;border: 2px solid lightgrey;")
# app/mailer_previews/user_mailer_preview.rb
class UserMailerPreview
def some_email
UserMailer.some_email(User.find(123).id)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment