<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>HJK Solutions: Tag email</title>
  <subtitle type="html">We Launch Startups</subtitle>
  <id>tag:blog.hjksolutions.com,2005:Typo</id>
  <generator uri="http://www.typosphere.org" version="4.0">Typo</generator>
  <link href="http://blog.hjksolutions.com/xml/atom/tag/feed.xml" rel="self" type="application/atom+xml"/>
  <link href="http://blog.hjksolutions.com/articles/tag/email" rel="alternate" type="text/html"/>
  <updated>2007-07-26T17:22:03-04:00</updated>
  <entry>
    <author>
      <name>Adam Jacob</name>
    </author>
    <id>urn:uuid:68694964-e1b1-45a7-85fd-e3735f12c54c</id>
    <published>2007-06-11T20:04:00-04:00</published>
    <updated>2007-07-26T17:22:03-04:00</updated>
    <title type="html">Re-Queing a mailbox full of bounced messages</title>
    <link href="http://blog.hjksolutions.com/articles/2007/06/11/re-queing-a-mailbox-full-of-bounced-messages" rel="alternate" type="text/html"/>
    <category term="solutions" scheme="http://blog.hjksolutions.com/articles/category/solutions" label="solutions"/>
    <category term="perl" scheme="http://blog.hjksolutions.com/articles/tag/perl"/>
    <category term="email" scheme="http://blog.hjksolutions.com/articles/tag/email"/>
    <summary type="html">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. ;)</summary>
    <content type="html">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. ;)

</content>
  </entry>
</feed>

