Take the 2008 Git User's Survey and help out! [ hide ]

public
Fork of mmower/mailtrap
Description: A mock SMTP server for use in Rails development.
Homepage: http://rubymatt.rubyforge.org/mailtrap/
Clone URL: git://github.com/ashleymoran/mailtrap.git
Search Repo:
Added Mailtrap::LogParser for turning Mailtrap log files into
an array of TMail::Mail objects - useful for inspecting the file
for testing purposes.
ashleymoran (author)
Sun Apr 06 11:24:34 -0700 2008
commit  fa975934c38f737f4ea2fd7219bacc9d8649b636
tree    d01b659063df9848ad7c26e923d3491f9b8bbb66
parent  248d8efd9e55242d2b5da06fe873c9c13f03b671
...
4
5
6
 
 
 
 
 
 
 
 
 
 
...
4
5
6
7
8
9
10
11
12
13
14
15
16
0
@@ -4,3 +4,13 @@ README.txt
0
 Rakefile
0
 bin/mailtrap
0
 lib/mailtrap.rb
0
+lib/mailtrap/log_parser.rb
0
+spec
0
+spec/mailtrap
0
+spec/mailtrap/log_parser_spec.rb
0
+spec/mailtrap/sample_logs
0
+spec/mailtrap/sample_logs/sample.log
0
+spec/mailtrap/sample_logs/sample_empty.log
0
+spec/spec.opts
0
+test
0
+test/test_mailtrap.rb
...
2
3
4
5
 
 
 
6
7
8
...
15
16
17
 
18
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
...
2
3
4
 
5
6
7
8
9
10
...
17
18
19
20
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
0
@@ -2,7 +2,9 @@
0
 
0
 require 'rubygems'
0
 require 'hoe'
0
-require './lib/mailtrap.rb'
0
+require 'spec'
0
+require 'spec/rake/spectask'
0
+require './lib/mailtrap'
0
 
0
 Hoe.new('mailtrap', Mailtrap::VERSION ) do |p|
0
   p.rubyforge_name = 'simplyruby'
0
@@ -15,6 +17,30 @@ Hoe.new('mailtrap', Mailtrap::VERSION ) do |p|
0
   p.remote_rdoc_dir = 'mailtrap'
0
   p.extra_deps << ['daemons','>= 1.0.8']
0
   p.extra_deps << ['trollop','>= 1.7']
0
+ p.extra_deps << ['tmail','>= 1.2.2']
0
 end
0
 
0
+namespace :spec do
0
+ desc "Run the specs under spec"
0
+ Spec::Rake::SpecTask.new('all') do |t|
0
+ t.spec_opts = ['--options', "spec/spec.opts"]
0
+ t.spec_files = FileList['spec/**/*_spec.rb']
0
+ end
0
+
0
+ desc "Run the specs under spec in specdoc format"
0
+ Spec::Rake::SpecTask.new('doc') do |t|
0
+ t.spec_opts = ['--format', "specdoc"]
0
+ t.spec_files = FileList['spec/**/*_spec.rb']
0
+ end
0
+
0
+ desc "Run the specs in HTML format"
0
+ Spec::Rake::SpecTask.new('html') do |t|
0
+ t.spec_opts = ['--format', "html"]
0
+ t.spec_files = FileList['spec/**/*_spec.rb']
0
+ end
0
+end
0
+
0
+desc "Run the default spec task"
0
+task :spec => :"spec:all"
0
+
0
 # vim: syntax=Ruby
...
59
60
61
 
 
 
 
62
63
64
...
80
81
82
83
84
85
86
 
 
 
 
 
87
88
89
...
109
110
111
 
 
 
 
 
 
112
113
114
...
118
119
120
121
 
122
123
124
...
139
140
141
142
 
143
144
145
...
59
60
61
62
63
64
65
66
67
68
...
84
85
86
 
 
 
 
87
88
89
90
91
92
93
94
...
114
115
116
117
118
119
120
121
122
123
124
125
...
129
130
131
 
132
133
134
135
...
150
151
152
 
153
154
155
156
0
@@ -59,6 +59,10 @@
0
                 lib/mailtrap.rb
0
                 </a>
0
         <br />
0
+ <a href="../files/lib/mailtrap/log_parser_rb.html">
0
+ lib/mailtrap/log_parser.rb
0
+ </a>
0
+ <br />
0
             </td>
0
         </tr>
0
 
0
@@ -80,10 +84,11 @@
0
 
0
     <div id="description">
0
       <p>
0
-<a href="Mailtrap.html">Mailtrap</a> creates a TCP server that listens on a
0
-specified port for SMTP clients. Accepts the connection and talks just
0
-enough of the SMTP protocol for them to deliver a message which it writes
0
-to disk.
0
+Class to read a <a href="Mailtrap.html">Mailtrap</a> log file and extract
0
+the emails, returning them as TMail objects. (Interim solution until we can
0
+get <a href="Mailtrap.html">Mailtrap</a> outputting structured log files.)
0
+&#8212; hacked together by Ashley Moran
0
+&lt;ashley.moran@patchspace.co.uk&gt; 06-Apr-2008
0
 </p>
0
 
0
     </div>
0
@@ -109,6 +114,12 @@ to disk.
0
 
0
     <div id="section">
0
 
0
+ <div id="class-list">
0
+ <h3 class="section-bar">Classes and Modules</h3>
0
+
0
+ Class <a href="Mailtrap/LogParser.html" class="link">Mailtrap::LogParser</a><br />
0
+
0
+ </div>
0
 
0
     <div id="constants-list">
0
       <h3 class="section-bar">Constants</h3>
0
@@ -118,7 +129,7 @@ to disk.
0
         <tr class="top-aligned-row context-row">
0
           <td class="context-item-name">VERSION</td>
0
           <td>=</td>
0
- <td class="context-item-value">'0.1.0'</td>
0
+ <td class="context-item-value">'0.2.1'</td>
0
         </tr>
0
         </table>
0
       </div>
0
@@ -139,7 +150,7 @@ to disk.
0
         <div class="method-heading">
0
           <a href="Mailtrap.src/M000001.html" target="Code" class="method-signature"
0
             onclick="popupCode('Mailtrap.src/M000001.html');return false;">
0
- <span class="method-name">new</span><span class="method-args">( host, port, once, msgdir )</span>
0
+ <span class="method-name">new</span><span class="method-args">( host, port, once, msgfile )</span>
0
           </a>
0
         </div>
0
       
...
10
11
12
13
14
 
 
15
16
17
18
 
 
 
 
 
19
20
21
22
23
...
10
11
12
 
 
13
14
15
16
17
 
18
19
20
21
22
23
 
24
25
26
0
@@ -10,14 +10,17 @@
0
   <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
0
 </head>
0
 <body class="standalone-code">
0
- <pre><span class="ruby-comment cmt"># File lib/mailtrap.rb, line 18</span>
0
- <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">initialize</span>( <span class="ruby-identifier">host</span>, <span class="ruby-identifier">port</span>, <span class="ruby-identifier">once</span>, <span class="ruby-identifier">msgdir</span> )
0
+ <pre><span class="ruby-comment cmt"># File lib/mailtrap.rb, line 19</span>
0
+ <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">initialize</span>( <span class="ruby-identifier">host</span>, <span class="ruby-identifier">port</span>, <span class="ruby-identifier">once</span>, <span class="ruby-identifier">msgfile</span> )
0
     <span class="ruby-ivar">@host</span> = <span class="ruby-identifier">host</span>
0
     <span class="ruby-ivar">@port</span> = <span class="ruby-identifier">port</span>
0
     <span class="ruby-ivar">@once</span> = <span class="ruby-identifier">once</span>
0
- <span class="ruby-ivar">@msgdir</span> = <span class="ruby-identifier">msgdir</span>
0
+ <span class="ruby-ivar">@msgfile</span> = <span class="ruby-identifier">msgfile</span>
0
+
0
+ <span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>( <span class="ruby-ivar">@msgfile</span>, <span class="ruby-value str">&quot;a&quot;</span> ) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">file</span><span class="ruby-operator">|</span>
0
+ <span class="ruby-identifier">file</span>.<span class="ruby-identifier">puts</span> <span class="ruby-node">&quot;\n* Mailtrap started at #{@host}:#{port}\n&quot;</span>
0
+ <span class="ruby-keyword kw">end</span>
0
     
0
- <span class="ruby-identifier">puts</span> <span class="ruby-node">&quot;Mailtrap starting at #{@host}:#{port} and writing to #{@msgdir}&quot;</span>
0
     <span class="ruby-identifier">service</span> = <span class="ruby-constant">TCPServer</span>.<span class="ruby-identifier">new</span>( <span class="ruby-ivar">@host</span>, <span class="ruby-ivar">@port</span> )
0
     <span class="ruby-identifier">accept</span>( <span class="ruby-identifier">service</span> )
0
   <span class="ruby-keyword kw">end</span></pre>
...
10
11
12
13
 
14
15
16
...
10
11
12
 
13
14
15
16
0
@@ -10,7 +10,7 @@
0
   <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
0
 </head>
0
 <body class="standalone-code">
0
- <pre><span class="ruby-comment cmt"># File lib/mailtrap.rb, line 30</span>
0
+ <pre><span class="ruby-comment cmt"># File lib/mailtrap.rb, line 34</span>
0
   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">accept</span>( <span class="ruby-identifier">service</span> )
0
     <span class="ruby-keyword kw">while</span> <span class="ruby-identifier">session</span> = <span class="ruby-identifier">service</span>.<span class="ruby-identifier">accept</span>
0
       
...
10
11
12
13
 
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 
 
 
30
31
32
33
 
34
35
36
...
10
11
12
 
13
14
15
16
17
18
19
 
 
 
 
 
 
 
 
 
 
20
21
22
23
24
25
26
27
28
29
30
0
@@ -10,27 +10,21 @@
0
   <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
0
 </head>
0
 <body class="standalone-code">
0
- <pre><span class="ruby-comment cmt"># File lib/mailtrap.rb, line 54</span>
0
+ <pre><span class="ruby-comment cmt"># File lib/mailtrap.rb, line 58</span>
0
   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">write</span>( <span class="ruby-identifier">from</span>, <span class="ruby-identifier">to_list</span>, <span class="ruby-identifier">message</span> )
0
     
0
     <span class="ruby-comment cmt"># Strip SMTP commands from To: and From:</span>
0
     <span class="ruby-identifier">from</span>.<span class="ruby-identifier">gsub!</span>( <span class="ruby-regexp re">/MAIL FROM:\s*/</span>, <span class="ruby-value str">&quot;&quot;</span> )
0
     <span class="ruby-identifier">to_list</span> = <span class="ruby-identifier">to_list</span>.<span class="ruby-identifier">map</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">to</span><span class="ruby-operator">|</span> <span class="ruby-identifier">to</span>.<span class="ruby-identifier">gsub</span>( <span class="ruby-regexp re">/RCPT TO:\s*/</span>, <span class="ruby-value str">&quot;&quot;</span> ) }
0
     
0
- <span class="ruby-comment cmt"># Figure out what the file name should be</span>
0
- <span class="ruby-identifier">n</span> = <span class="ruby-value">1</span>
0
- <span class="ruby-constant">Dir</span>.<span class="ruby-identifier">chdir</span>( <span class="ruby-ivar">@msgdir</span> ) <span class="ruby-keyword kw">do</span>
0
- <span class="ruby-identifier">files</span> = <span class="ruby-constant">Dir</span>.<span class="ruby-identifier">glob</span>( <span class="ruby-value str">&quot;smtp*.msg&quot;</span> )
0
- <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">files</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">0</span>
0
- <span class="ruby-identifier">n</span> = <span class="ruby-value">1</span> <span class="ruby-operator">+</span> <span class="ruby-constant">Integer</span>( <span class="ruby-identifier">files</span>.<span class="ruby-identifier">last</span>.<span class="ruby-identifier">gsub</span>( <span class="ruby-regexp re">/smtp(\d+)\.msg/</span>, <span class="ruby-value str">'\1'</span> ) )
0
- <span class="ruby-keyword kw">end</span>
0
- <span class="ruby-keyword kw">end</span>
0
-
0
- <span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>( <span class="ruby-constant">File</span>.<span class="ruby-identifier">join</span>( <span class="ruby-ivar">@msgdir</span>, <span class="ruby-value str">&quot;smtp%04d.msg&quot;</span> <span class="ruby-operator">%</span> <span class="ruby-identifier">n</span> ), <span class="ruby-value str">&quot;w&quot;</span> ) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">file</span><span class="ruby-operator">|</span>
0
+ <span class="ruby-comment cmt"># Append to the end of the messages file</span>
0
+ <span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>( <span class="ruby-ivar">@msgfile</span>, <span class="ruby-value str">&quot;a&quot;</span> ) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">file</span><span class="ruby-operator">|</span>
0
+ <span class="ruby-identifier">file</span>.<span class="ruby-identifier">puts</span> <span class="ruby-value str">&quot;* Message begins&quot;</span>
0
       <span class="ruby-identifier">file</span>.<span class="ruby-identifier">puts</span> <span class="ruby-node">&quot;From: #{from}&quot;</span>
0
       <span class="ruby-identifier">file</span>.<span class="ruby-identifier">puts</span> <span class="ruby-node">&quot;To: #{to_list.join(&quot;, &quot;)}&quot;</span>
0
       <span class="ruby-identifier">file</span>.<span class="ruby-identifier">puts</span> <span class="ruby-value str">&quot;Body:&quot;</span>
0
       <span class="ruby-identifier">file</span>.<span class="ruby-identifier">puts</span> <span class="ruby-identifier">message</span>
0
+ <span class="ruby-identifier">file</span>.<span class="ruby-identifier">puts</span> <span class="ruby-value str">&quot;\n* Message ends&quot;</span>
0
     <span class="ruby-keyword kw">end</span>
0
 
0
   <span class="ruby-keyword kw">end</span></pre>
...
10
11
12
13
 
14
15
16
...
10
11
12
 
13
14
15
16
0
@@ -10,7 +10,7 @@
0
   <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
0
 </head>
0
 <body class="standalone-code">
0
- <pre><span class="ruby-comment cmt"># File lib/mailtrap.rb, line 80</span>
0
+ <pre><span class="ruby-comment cmt"># File lib/mailtrap.rb, line 78</span>
0
   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">serve</span>( <span class="ruby-identifier">connection</span> )
0
     <span class="ruby-identifier">connection</span>.<span class="ruby-identifier">puts</span>( <span class="ruby-node">&quot;220 #{@host} MailTrap ready ESTMP&quot;</span> )
0
     <span class="ruby-identifier">helo</span> = <span class="ruby-identifier">connection</span>.<span class="ruby-identifier">get_line</span> <span class="ruby-comment cmt"># whoever they are</span>
...
1
 
...
 
1
0
@@ -1 +1 @@
0
-Thu, 04 Oct 2007 11:17:30 +0100
0
+Sun, 06 Apr 2008 19:24:06 +0100
...
56
57
58
59
 
60
61
62
...
56
57
58
 
59
60
61
62
0
@@ -56,7 +56,7 @@
0
     </tr>
0
     <tr class="top-aligned-row">
0
       <td><strong>Last Update:</strong></td>
0
- <td>Wed Oct 03 17:12:32 +0100 2007</td>
0
+ <td>Sun Apr 06 18:32:54 +0100 2008</td>
0
     </tr>
0
     </table>
0
   </div>
...
56
57
58
59
 
60
61
62
...
88
89
90
91
92
 
 
93
94
95
96
97
98
 
99
100
101
...
113
114
115
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
117
118
...
121
122
123
124
125
126
127
128
129
130
131
 
 
 
 
132
133
134
...
138
139
140
 
 
 
141
142
143
...
56
57
58
 
59
60
61
62
...
88
89
90
 
 
91
92
93
94
95
96
 
 
97
98
99
100
...
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
...
143
144
145
 
 
 
 
146
147
148
 
149
150
151
152
153
154
155
...
159
160
161
162
163
164
165
166
167
0
@@ -56,7 +56,7 @@
0
     </tr>
0
     <tr class="top-aligned-row">
0
       <td><strong>Last Update:</strong></td>
0
- <td>Thu Oct 04 10:55:14 +0100 2007</td>
0
+ <td>Sun Apr 06 18:32:54 +0100 2008</td>
0
     </tr>
0
     </table>
0
   </div>
0
@@ -88,14 +88,13 @@ ActionMailer to successfully deliver its message.
0
 </p>
0
 <p>
0
 <a href="../classes/Mailtrap.html">Mailtrap</a> makes <b>no</b> attempt to
0
-actually deliver messages and, instead, writes them into sequentially
0
-numbered files on disk (hence the name Mail_trap_).
0
+actually deliver messages and, instead, writes them into a file (hence the
0
+name Mail_trap_). Handy tip: use tail -f to see emails being received.
0
 </p>
0
 <p>
0
 You can configure the hostname (default: localhost) and port (default:
0
 2525) for the server and also where the messages get written (default:
0
-/var/tmp). Messages will get written to files named smtp0001.msg,
0
-smtp0002.msg, and so on.
0
+/var/tmp/mailtrap.log).
0
 </p>
0
 <h2>FEATURES/PROBLEMS:</h2>
0
 <ul>
0
@@ -113,6 +112,29 @@ smtp0002.msg, and so on.
0
 </li>
0
 </ul>
0
 <h2>SYNOPSIS:</h2>
0
+<p>
0
+To use the defaults host:localhost, port:2525, file:/var/log/mailtrap.log
0
+</p>
0
+<ul>
0
+<li>mailtrap start
0
+
0
+</li>
0
+</ul>
0
+<p>
0
+Customise startup:
0
+</p>
0
+<ul>
0
+<li>sudo mailtrap start &#8212;host my.host &#8212;port 25 &#8212;once
0
+&#8212;file=/var/log/messages.txt
0
+
0
+</li>
0
+</ul>
0
+<p>
0
+(sudo because you want to use restricted port 25)
0
+</p>
0
+<p>
0
+For more info:
0
+</p>
0
 <ul>
0
 <li>mailtrap &#8212;help (to see Daemonization options)
0
 
0
@@ -121,14 +143,13 @@ smtp0002.msg, and so on.
0
 href="../classes/Mailtrap.html">Mailtrap</a> options)
0
 
0
 </li>
0
-<li>mailtrap start &#8212;host localhost &#8212;port 8025 &#8212;once
0
-&#8212;msgdir=/var/tmp
0
-
0
-</li>
0
 </ul>
0
 <h2>REQUIREMENTS:</h2>
0
 <ul>
0
-<li>Rubygems
0
+<li>Hoe rubygem
0
+
0
+</li>
0
+<li>Rubygems rubygem
0
 
0
 </li>
0
 <li>Daemons rubygem
0
@@ -138,6 +159,9 @@ href="../classes/Mailtrap.html">Mailtrap</a> options)
0
 
0
 </li>
0
 </ul>
0
+<p>
0
+All these are automatically installed if you use gem install -y
0
+</p>
0
 <h2>INSTALL:</h2>
0
 <ul>
0
 <li>sudo gem install -y mailtrap
...
56
57
58
59
 
60
61
62
...
56
57
58
 
59
60
61
62
0
@@ -56,7 +56,7 @@
0
     </tr>
0
     <tr class="top-aligned-row">
0
       <td><strong>Last Update:</strong></td>
0
- <td>Wed Oct 03 20:58:33 +0100 2007</td>
0
+ <td>Sun Apr 06 18:32:54 +0100 2008</td>
0
     </tr>
0
     </table>
0
   </div>
...
56
57
58
59
 
60
61
62
...
56
57
58
 
59
60
61
62
0
@@ -56,7 +56,7 @@
0
     </tr>
0
     <tr class="top-aligned-row">
0
       <td><strong>Last Update:</strong></td>
0
- <td>Thu Oct 04 11:17:27 +0100 2007</td>
0
+ <td>Sun Apr 06 19:17:41 +0100 2008</td>
0
     </tr>
0
     </table>
0
   </div>
...
21
22
23
 
24
25
26
...
21
22
23
24
25
26
27
0
@@ -21,6 +21,7 @@
0
   <h1 class="section-bar">Classes</h1>
0
   <div id="index-entries">
0
     <a href="classes/Mailtrap.html">Mailtrap</a><br />
0
+ <a href="classes/Mailtrap/LogParser.html">Mailtrap::LogParser</a><br />
0
   </div>
0
 </div>
0
 </body>
...
24
25
26
 
27
28
29
...
24
25
26
27
28
29
30
0
@@ -24,6 +24,7 @@
0
     <a href="files/README_txt.html">README.txt</a><br />
0
     <a href="files/bin/mailtrap.html">bin/mailtrap</a><br />
0
     <a href="files/lib/mailtrap_rb.html">lib/mailtrap.rb</a><br />
0
+ <a href="files/lib/mailtrap/log_parser_rb.html">lib/mailtrap/log_parser.rb</a><br />
0
   </div>
0
 </div>
0
 </body>
...
22
23
24
 
25
26
27
...
22
23
24
25
26
27
28
0
@@ -22,6 +22,7 @@
0
   <div id="index-entries">
0
     <a href="classes/Mailtrap.html#M000002">accept (Mailtrap)</a><br />
0
     <a href="classes/Mailtrap.html#M000001">new (Mailtrap)</a><br />
0
+ <a href="classes/Mailtrap/LogParser.html#M000005">parse (Mailtrap::LogParser)</a><br />
0
     <a href="classes/Mailtrap.html#M000004">serve (Mailtrap)</a><br />
0
     <a href="classes/Mailtrap.html#M000003">write (Mailtrap)</a><br />
0
   </div>
...
5
6
7
8
 
9
10
11
12
13
 
14
15
16
...
5
6
7
 
8
9
10
11
12
 
13
14
15
16
0
@@ -5,12 +5,12 @@
0
 
0
 <!--
0
 
0
- rubymatt's mailtrap-0.1.0 Documentation
0
+ simplyruby's mailtrap-0.2.1 Documentation
0
 
0
   -->
0
 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
0
 <head>
0
- <title> rubymatt's mailtrap-0.1.0 Documentation</title>
0
+ <title> simplyruby's mailtrap-0.2.1 Documentation</title>
0
   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
0
 </head>
0
 <frameset rows="20%, 80%">
...
1
2
3
4
5
6
 
 
7
8
9
...
 
1
2
3
4
5
6
7
8
9
10
0
@@ -1,9 +1,10 @@
0
-
0
 require 'rubygems'
0
 require 'daemons'
0
 require 'socket'
0
 require 'trollop'
0
 
0
+$:.unshift File.expand_path(File.join(File.dirname(__FILE__)))
0
+
0
 #
0
 # Mailtrap creates a TCP server that listens on a specified port for SMTP
0
 # clients. Accepts the connection and talks just enough of the SMTP protocol

Comments

    No one has commented yet.