public
Description: A mock SMTP server for use in Rails development.
Homepage: http://rubymatt.rubyforge.org/mailtrap/
Clone URL: git://github.com/mmower/mailtrap.git
Committing Mailtrap 0.2.1 to Git
mmower (author)
Wed Mar 26 14:15:21 -0700 2008
commit  81e1f294a3ec56b4aa1103d472d411a2b5fd4507
tree    ac18e6b786b04e2649b7182a0aa1fb1dfe4a64bd
parent  9b50b902fe4ffff7b40501b54ad53d5eccf072f8
...
8
9
10
11
 
12
13
 
14
15
16
...
21
22
23
 
 
 
 
 
 
 
 
 
 
 
 
24
25
26
27
28
29
30
31
 
 
32
33
34
 
 
35
36
37
...
8
9
10
 
11
12
 
13
14
15
16
...
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 
 
39
40
 
41
42
43
44
45
46
47
48
49
50
0
@@ -8,9 +8,9 @@ Mailtrap is a mock SMTP server for use in Rails development.
0
 
0
 Mailtrap waits on your choosen port for a client to connect and talks _just enough_ SMTP protocol for ActionMailer to successfully deliver its message.
0
 
0
-Mailtrap makes *no* attempt to actually deliver messages and, instead, writes them into sequentially numbered files on disk (hence the name Mail_trap_).
0
+Mailtrap makes *no* attempt to actually deliver messages and, instead, writes them into a file (hence the name Mail_trap_). Handy tip: use tail -f to see emails being received.
0
 
0
-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.
0
+You can configure the hostname (default: localhost) and port (default: 2525) for the server and also where the messages get written (default: /var/tmp/mailtrap.log).
0
   
0
 == FEATURES/PROBLEMS:
0
 
0
@@ -21,17 +21,30 @@ You can configure the hostname (default: localhost) and port (default: 2525) for
0
 
0
 == SYNOPSIS:
0
 
0
+To use the defaults host:localhost, port:2525, file:/var/log/mailtrap.log
0
+
0
+* mailtrap start
0
+
0
+Customise startup:
0
+
0
+* sudo mailtrap start --host my.host --port 25 --once --file=/var/log/messages.txt
0
+
0
+(sudo because you want to use restricted port 25)
0
+
0
+For more info:
0
+
0
 * mailtrap --help (to see Daemonization options)
0
 * mailtrap start --help (to see Mailtrap options)
0
 
0
-* mailtrap start --host localhost --port 8025 --once --msgdir=/var/tmp
0
-
0
 == REQUIREMENTS:
0
 
0
-* Rubygems
0
+* Hoe rubygem
0
+* Rubygems rubygem
0
 * Daemons rubygem
0
 * Trollop rubygem
0
 
0
+All these are automatically installed if you use gem install -y
0
+
0
 == INSTALL:
0
 
0
 * sudo gem install -y mailtrap
...
4
5
6
7
 
8
9
10
...
4
5
6
 
7
8
9
10
0
@@ -4,7 +4,7 @@ require 'rubygems'
0
 require 'hoe'
0
 require './lib/mailtrap.rb'
0
 
0
-Hoe.new('mailtrap', Mailtrap::VERSION) do |p|
0
+Hoe.new('mailtrap', Mailtrap::VERSION ) do |p|
0
   p.rubyforge_name = 'rubymatt'
0
   p.author = 'Matt Mower'
0
   p.email = 'self@mattmower.com'
0
bin/mailtrap 100644 →
...
11
12
13
14
 
15
16
17
...
24
25
26
27
 
28
29
...
11
12
13
 
14
15
16
17
...
24
25
26
 
27
28
29
0
@@ -11,7 +11,7 @@ opts = Trollop::options do
0
   opt :host, "The host SMTP clients will connect to", :default => 'localhost'
0
   opt :port, "The port SMTP clients will connect to", :default => 2525
0
   opt :once, "Whether to terminate after serving the first client", :default => false
0
- opt :msgdir, "Where messages get stored", :default => "/var/tmp"
0
+ opt :file, "File where messages get written", :default => "/var/tmp/mailtrap.log"
0
 end
0
 
0
 options = {
0
@@ -24,5 +24,5 @@ options = {
0
 }
0
 
0
 Daemons.run_proc( 'mailtrap', options ) do
0
- Mailtrap.new( opts[:host], opts[:port], opts[:once], opts[:msgdir] )
0
+ Mailtrap.new( opts[:host], opts[:port], opts[:once], opts[:file] )
0
 end
0
\ No newline at end of file
...
10
11
12
13
 
14
15
16
17
18
 
19
20
21
22
 
 
 
 
 
23
24
25
26
27
...
57
58
59
60
61
62
63
64
65
66
67
68
69
 
 
 
70
71
72
73
 
74
75
76
...
10
11
12
 
13
14
15
16
17
 
18
19
20
21
 
22
23
24
25
26
27
 
28
29
30
...
60
61
62
 
 
 
 
 
 
 
 
 
 
63
64
65
66
67
68
69
70
71
72
73
0
@@ -10,18 +10,21 @@ require 'trollop'
0
 # for them to deliver a message which it writes to disk.
0
 #
0
 class Mailtrap
0
- VERSION = '0.1.0'
0
+ VERSION = '0.2.1'
0
   
0
   # Create a new Mailtrap on the specified host:port. If once it true it
0
   # will listen for one message then exit. Specify the msgdir where messages
0
   # are written.
0
- def initialize( host, port, once, msgdir )
0
+ def initialize( host, port, once, msgfile )
0
     @host = host
0
     @port = port
0
     @once = once
0
- @msgdir = msgdir
0
+ @msgfile = msgfile
0
+
0
+ File.open( @msgfile, "a" ) do |file|
0
+ file.puts "\n* Mailtrap started at #{@host}:#{port}\n"
0
+ end
0
     
0
- puts "Mailtrap starting at #{@host}:#{port} and writing to #{@msgdir}"
0
     service = TCPServer.new( @host, @port )
0
     accept( service )
0
   end
0
@@ -57,20 +60,14 @@ class Mailtrap
0
     from.gsub!( /MAIL FROM:\s*/, "" )
0
     to_list = to_list.map { |to| to.gsub( /RCPT TO:\s*/, "" ) }
0
     
0
- # Figure out what the file name should be
0
- n = 1
0
- Dir.chdir( @msgdir ) do
0
- files = Dir.glob( "smtp*.msg" )
0
- if files.length > 0
0
- n = 1 + Integer( files.last.gsub( /smtp(\d+)\.msg/, '\1' ) )
0
- end
0
- end
0
-
0
- File.open( File.join( @msgdir, "smtp%04d.msg" % n ), "w" ) do |file|
0
+ # Append to the end of the messages file
0
+ File.open( @msgfile, "a" ) do |file|
0
+ file.puts "* Message begins"
0
       file.puts "From: #{from}"
0
       file.puts "To: #{to_list.join(", ")}"
0
       file.puts "Body:"
0
       file.puts message
0
+ file.puts "\n* Message ends"
0
     end
0
 
0
   end

Comments

    No one has commented yet.