mailtrap 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (7) hide show
  1. data.tar.gz.sig +0 -0
  2. data/README.txt +15 -11
  3. data/Rakefile +1 -1
  4. data/bin/mailtrap +2 -2
  5. data/lib/mailtrap.rb +31 -26
  6. metadata +2 -2
  7. metadata.gz.sig +0 -0
data.tar.gz.sig CHANGED
Binary file
data/README.txt CHANGED
@@ -4,23 +4,27 @@ mailtrap
4
4
 
5
5
  == DESCRIPTION:
6
6
 
7
- Mailtrap is a mock SMTP server for use in Rails development. Mailtrap waits for
8
- a client to connect and talks just enough SMTP for ActionMailer to send its message
9
- which Mailtrap will write into a file for you to inspect. Mailtrap makes no
10
- attempt to actually deliver anything (hence the name Mail*trap*).
11
-
12
- You can configure the hostname (default: localhost) and port (default: 1025)
13
- for the server and also where the messages get written (default: /tmp). Messages
14
- will get written to files named smtp0001.msg, smtp0002.msg, and so on.
7
+ Mailtrap is a mock SMTP server for use in Rails development.
8
+
9
+ Mailtrap waits on your choosen port for a client to connect and talks _just enough_ SMTP protocol for ActionMailer to successfully deliver its message.
10
+
11
+ Mailtrap makes *no* attempt to actually deliver messages and, instead, writes them into sequentially numbered files on disk (hence the name Mail_trap_).
12
+
13
+ You can configure the hostname (default: localhost) and port (default: 2525) for the server and also where the messages get written (default: /var/tmp). Messages will get written to files named smtp0001.msg, smtp0002.msg, and so on.
15
14
 
16
15
  == FEATURES/PROBLEMS:
17
16
 
18
- * Test with ActionMailer's SMTP delivery method
19
- * Very, very, dumb ... might not work with an arbitrary Mail client
17
+ * Lightweight, no setup
18
+ * Runs as a daemon with start, stop, etc..
19
+ * Tested with ActionMailer's SMTP delivery method
20
+ * Very, very, dumb ... might not work with an arbitrary SMTP client
20
21
 
21
22
  == SYNOPSIS:
22
23
 
23
- mailtrap --host localhost --port 8025 --once
24
+ * mailtrap --help (to see Daemonization options)
25
+ * mailtrap start --help (to see Mailtrap options)
26
+
27
+ * mailtrap start --host localhost --port 8025 --once --msgdir=/var/tmp
24
28
 
25
29
  == REQUIREMENTS:
26
30
 
data/Rakefile CHANGED
@@ -4,7 +4,7 @@ require 'rubygems'
4
4
  require 'hoe'
5
5
  require './lib/mailtrap.rb'
6
6
 
7
- Hoe.new('mailtrap', Mailtrap::VERSION) do |p|
7
+ Hoe.new('mailtrap', Mailtrap::VERSION ) do |p|
8
8
  p.rubyforge_name = 'rubymatt'
9
9
  p.author = 'Matt Mower'
10
10
  p.email = 'self@mattmower.com'
data/bin/mailtrap CHANGED
@@ -11,7 +11,7 @@ opts = Trollop::options do
11
11
  opt :host, "The host SMTP clients will connect to", :default => 'localhost'
12
12
  opt :port, "The port SMTP clients will connect to", :default => 2525
13
13
  opt :once, "Whether to terminate after serving the first client", :default => false
14
- opt :msgdir, "Where messages get stored", :default => "/var/tmp"
14
+ opt :file, "File where messages get written", :default => "/var/tmp/mailtrap.log"
15
15
  end
16
16
 
17
17
  options = {
@@ -24,5 +24,5 @@ options = {
24
24
  }
25
25
 
26
26
  Daemons.run_proc( 'mailtrap', options ) do
27
- Mailtrap.new( opts[:host], opts[:port], opts[:once], opts[:msgdir] )
27
+ Mailtrap.new( opts[:host], opts[:port], opts[:once], opts[:file] )
28
28
  end
data/lib/mailtrap.rb CHANGED
@@ -4,20 +4,29 @@ require 'daemons'
4
4
  require 'socket'
5
5
  require 'trollop'
6
6
 
7
+ #
8
+ # Mailtrap creates a TCP server that listens on a specified port for SMTP
9
+ # clients. Accepts the connection and talks just enough of the SMTP protocol
10
+ # for them to deliver a message which it writes to disk.
11
+ #
7
12
  class Mailtrap
8
- VERSION = '0.1.0'
13
+ VERSION = '0.2.0'
9
14
 
10
- def initialize( host, port, once, msgdir )
15
+ # Create a new Mailtrap on the specified host:port. If once it true it
16
+ # will listen for one message then exit. Specify the msgdir where messages
17
+ # are written.
18
+ def initialize( host, port, once, msgfile )
11
19
  @host = host
12
20
  @port = port
13
21
  @once = once
14
- @msgdir = msgdir
22
+ @msgfile = msgfile
15
23
 
16
- puts "Mailtrap starting at #{@host}:#{port} and writing to #{@msgdir}"
24
+ puts "Mailtrap starting at #{@host}:#{port} and writing to #{@msgfile}"
17
25
  service = TCPServer.new( @host, @port )
18
26
  accept( service )
19
27
  end
20
28
 
29
+ # Service one or more SMTP client connections
21
30
  def accept( service )
22
31
  while session = service.accept
23
32
 
@@ -39,30 +48,28 @@ class Mailtrap
39
48
  end
40
49
  end
41
50
 
51
+ # Write a plain text dump of the incoming email to a text
52
+ # file. The file will be in the @msgdir folder and will
53
+ # be called smtp0001.msg, smtp0002.msg, and so on.
42
54
  def write( from, to_list, message )
43
55
 
56
+ # Strip SMTP commands from To: and From:
44
57
  from.gsub!( /MAIL FROM:\s*/, "" )
45
58
  to_list = to_list.map { |to| to.gsub( /RCPT TO:\s*/, "" ) }
46
59
 
47
- n = 1
48
- Dir.chdir( @msgdir ) do
49
- files = Dir.glob( "smtp*.msg" )
50
- if files.length > 0
51
- n = 1 + Integer( files.last.gsub( /smtp(\d+)\.msg/, '\1' ) )
52
- end
53
- end
54
-
55
- File.open( File.join( @msgdir, "smtp%04d.msg" % n ), "w" ) do |file|
56
- file.puts "FROM: #{from}"
57
- to_list.each do |to|
58
- file.puts "TO: #{to}"
59
- end
60
- file.puts "----"
60
+ # Append to the end of the messages file
61
+ File.open( @msgfile, "a" ) do |file|
62
+ file.puts "****************************************"
63
+ file.puts "From: #{from}"
64
+ file.puts "To: #{to_list.join(", ")}"
65
+ file.puts "Body:"
61
66
  file.puts message
62
67
  end
63
68
 
64
69
  end
65
70
 
71
+ # Talk pidgeon-SMTP to the client to get them to hand over the message
72
+ # and go away.
66
73
  def serve( connection )
67
74
  connection.puts( "220 #{@host} MailTrap ready ESTMP" )
68
75
  helo = connection.get_line # whoever they are
@@ -74,12 +81,14 @@ class Mailtrap
74
81
  connection.puts "250 HELP"
75
82
  end
76
83
 
84
+ # Accept MAIL FROM:
77
85
  from = connection.get_line
78
86
  connection.puts( "250 OK" )
79
87
  puts "From: #{from}"
80
88
 
81
89
  to_list = []
82
90
 
91
+ # Accept RCPT TO: until we see DATA
83
92
  loop do
84
93
  to = connection.get_line
85
94
  break if to.nil?
@@ -94,26 +103,22 @@ class Mailtrap
94
103
  end
95
104
  end
96
105
 
97
- puts "We know who we're sending to."
98
-
106
+ # Capture the message body terminated by <CR>.<CR>
99
107
  lines = []
100
-
101
108
  loop do
102
109
  line = connection.get_line
103
110
  break if line.nil? || line == "."
104
111
  lines << line
105
112
  puts "+ #{line}"
106
113
  end
107
-
108
- puts "Message read"
114
+
115
+ # We expect the client will go away now
109
116
  connection.puts( "250 OK" )
110
-
111
117
  connection.gets # Quit
112
118
  connection.puts "221 Seeya"
113
-
114
119
  connection.close
115
120
  puts "And we're done with that bozo!"
116
-
121
+
117
122
  write( from, to_list, lines.join( "\n" ) )
118
123
 
119
124
  end
metadata CHANGED
@@ -3,7 +3,7 @@ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: mailtrap
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.0
6
+ version: 0.2.0
7
7
  date: 2007-10-04 00:00:00 +01:00
8
8
  summary: Mailtrap is a mock SMTP server for use in Rails development
9
9
  require_paths:
@@ -11,7 +11,7 @@ require_paths:
11
11
  email: self@mattmower.com
12
12
  homepage: " by Matt Mower <self@mattmower.com>"
13
13
  rubyforge_project: rubymatt
14
- description: "Mailtrap is a mock SMTP server for use in Rails development. Mailtrap waits for a client to connect and talks just enough SMTP for ActionMailer to send its message which Mailtrap will write into a file for you to inspect. Mailtrap makes no attempt to actually deliver anything (hence the name Mail*trap*). You can configure the hostname (default: localhost) and port (default: 1025) for the server and also where the messages get written (default: /tmp). Messages will get written to files named smtp0001.msg, smtp0002.msg, and so on. == FEATURES/PROBLEMS: * Test with ActionMailer's SMTP delivery method * Very, very, dumb ... might not work with an arbitrary Mail client == SYNOPSIS:"
14
+ description: "Mailtrap is a mock SMTP server for use in Rails development. Mailtrap waits on your choosen port for a client to connect and talks _just enough_ SMTP protocol for ActionMailer to successfully deliver its message. Mailtrap makes *no* attempt to actually deliver messages and, instead, writes them into sequentially numbered files on disk (hence the name \tMail_trap_). You can configure the hostname (default: localhost) and port (default: 2525) for the server and also where the messages get written (default: /var/tmp). Messages will get written to files named smtp0001.msg, smtp0002.msg, and so on. == FEATURES/PROBLEMS:"
15
15
  autorequire:
16
16
  default_executable:
17
17
  bindir: bin
metadata.gz.sig CHANGED
Binary file