Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added extended (human friendly) info of the email #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

[![Build Status](https://travis-ci.org/joshmcarthur/mail-logger.png?branch=master)](https://travis-ci.org/joshmcarthur/mail-logger)



You know how you're looking through your logs to see how your mailing is doing, and after all that request rubbish, you just can't see that **Sent mail** stuff? Annoying, right?

MailLogger to the rescue! It lodges a callback with `Mail`, capturing all of the mail being sent, and logging it to it's own file, so you can simply look back through that file to see what's up. Easy peasy!
Expand All @@ -12,10 +10,11 @@ I could be a pain and require Rails here, but I don't. If you are using Rails, t

Default paths getting you down? Not to worry, this gem is configurable!

``` ruby
```ruby
Mail::Logger.configure do |config|
config.log_path = "Whatever you want"
config.log_file_name = "all my emails.log"
config.include_body = true # false by default
end
```

Expand All @@ -37,8 +36,6 @@ Or install it yourself as:


> Please note: There's another project around called [**mail_logger**](https://rubygems.org/gems/mail_logger), which records details of mail to the database via ActiveRecord, instead of a log file (which is what this project does). While I certainly didn't intend to infringe, the other project hasn't been updated in a few years, so I'm sticking with this name.



## Contributing

Expand Down
21 changes: 20 additions & 1 deletion lib/mail/logger/callback.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
class Mail::Logger::Callback
PROPERTIES = %i(from to cc bcc subject message_id mime_version content_type content_transfer_encoding)

def self.delivered_email(email)
Mail::Logger.logger.info email.inspect
text = PROPERTIES.map do |prop|
"#{key_camelize(prop)}: #{value_textize(email.send(prop))}"
end.join("\n") << "\n"

if Mail::Logger.configuration.include_body?
bodies = email.body.parts.empty? && [ email.body ] || email.body.parts
text << "Body:\n" << bodies.map(&:to_s).join("\n")
end

Mail::Logger.logger.info(text)
end

def self.key_camelize key
key.to_s.split(/_+/).map { |t| t[0].upcase + t[1..-1] }.join('-')
end

def self.value_textize value
value.is_a?(Array) && value.join(", ") || value
end
end

Expand Down
13 changes: 12 additions & 1 deletion lib/mail/logger/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ class Mail::Logger::Configuration
def initialize
self.log_path = rails_log_path || File.expand_path("./log")
self.log_file_name = rails_log_file_name || "mail.log"
@include_body = rails_include_body || false
end

def include_body?
@include_body
end

def rails_include_body
return nil unless defined? Rails

false
end

def rails_log_path
Expand All @@ -17,4 +28,4 @@ def rails_log_file_name

"mail_#{Rails.env}.log"
end
end
end
5 changes: 3 additions & 2 deletions test/mail/logger/callback_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
end

it "Logs the email" do
Mail::Logger.logger.expects(:info).with(subject.inspect).returns(true)
sample = "From: \nTo: \nCc: \nBcc: \nSubject: \nMessage-Id: \nMime-Version: \nContent-Type: \nContent-Transfer-Encoding: \n"
Mail::Logger.logger.expects(:info).with(sample).returns(true)
Mail::Logger::Callback.delivered_email(subject)
end
end
Expand Down Expand Up @@ -41,4 +42,4 @@ def deliver_email
def mail_object
Mail.new
end
end
end
12 changes: 10 additions & 2 deletions test/mail/logger/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ def self.env
end

def self.root
Pathname.new(".")
end
Pathname.new(".")
end
end
end

Expand All @@ -29,6 +29,10 @@ def self.root
it "Uses the Rails environment to name the file" do
subject.log_file_name.must_equal "mail_development.log"
end

it "Uses the setup value for include body as include body property" do
subject.include_body?.must_equal false
end
end

describe "without Rails" do
Expand All @@ -43,5 +47,9 @@ def self.root
it "Uses a default string to name the file" do
subject.log_file_name.must_equal "mail.log"
end

it "Uses the setup value for include body as include body property" do
subject.include_body?.must_equal false
end
end
end