<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>HJK Solutions: Tag email</title>
    <link>http://blog.hjksolutions.com/articles/tag/email</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>We Launch Startups</description>
    <item>
      <title>Re-Queing a mailbox full of bounced messages</title>
      <description>We've recently run in to a situation where we had a fairly large number of legitimate messages bounce due to a misconfigured MTA. Once we fixed the MTA, we needed to get all of those messages back out the door again. It was a job for CPAN.
&lt;pre&gt;
    &lt;code&gt;

use strict;
use warnings;        
use Mail::Box::Manager;
use Email::MIME;
use Email::Send;

foreach my $mbox (@ARGV) {
    my $mgr = Mail::Box::Manager-&gt;new;
    my $folder = $mgr-&gt;open(folder =&gt; $mbox);
    my $sender = Email::Send-&gt;new({mailer =&gt; 'SMTP'});
    $sender-&gt;mailer_args([Host =&gt; 'localhost']);

    foreach my $msg ($folder-&gt;messages) {
        PART: foreach my $part ($msg-&gt;parts('ALL')) {  
            my $body =  $part-&gt;decoded;
            if ($body =~ /This is the mail system at/) {
                next PART;
            } elsif ($part-&gt;decoded =~ /Action: failed/) {
                next PART;
            } else {
                my $email = Email::MIME-&gt;new(\$part-&gt;decoded);
                print "Sending to: " . $email-&gt;header("To") . "\n";
                $sender-&gt;send($email);
            }
        }
    }
}
&lt;/code&gt;
&lt;/pre&gt;
The code uses a couple of different modules:
&lt;ol&gt;
&lt;li&gt;&lt;a href="http://search.cpan.org/~markov/Mail-Box-2.071/lib/Mail/Box/Manager.pod"&gt;Mail::Box::Manager&lt;/a&gt;: handles opening a mailbox (in pretty much every common format)
and extracting messages.
&lt;li&gt;&lt;a href="http://search.cpan.org/~rjbs/Email-MIME-1.859/lib/Email/MIME.pm"&gt; Email::MIME&lt;/a&gt;: lets you construct a new email from a MIME encoded message. 
&lt;li&gt; &lt;a href ="http://search.cpan.org/~rjbs/Email-Send-2.185/lib/Email/Send.pm"&gt;Email::Send&lt;/a&gt;: takes our Email::MIME object and sends it out (in this case, via SMTP, but you can choose your mechanism.)
&lt;/ol&gt;
So should you ever find yourself with a couple thousand erroneously bounced messages, don't fret. ;)

</description>
      <pubDate>Mon, 11 Jun 2007 20:04:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:68694964-e1b1-45a7-85fd-e3735f12c54c</guid>
      <author>Adam Jacob</author>
      <link>http://blog.hjksolutions.com/articles/2007/06/11/re-queing-a-mailbox-full-of-bounced-messages</link>
      <category>solutions</category>
      <category>perl</category>
      <category>email</category>
      <trackback:ping>http://blog.hjksolutions.com/articles/trackback/2</trackback:ping>
    </item>
  </channel>
</rss>
