There seem to be hundreds of questions in a similar vein. I think have read them all but none seem to answer this question. I have looked at FetchMail but don't think I can install it on my DreamHost shared server. I have looked at Mail::Box, but cannot figure out how to log in and collect the mail I want to forward. I am currently trying to do it as shown below ... obviously this has been simplified to make it easier to follow and may contain errors. But essentially I am taking the body of the read mail and trying to post that on as the body of the forwarded email.
The mail is read and forwarded correctly but when viewing the forwarded mail all the formatting is wrong and sometimes it is truncated.
use Net::IMAP::Simple;
use Email::Simple;
use IO::Socket::SSL;
use Email::Address;
# Connect
my $imap = Net::IMAP::Simple->new($Server, port => 993, use_ssl => 1,) || die "Unable to connect to IMAP: $Net::IMAP::Simple::errstr\n";
# Log in
if ( !$imap->login( $L, $P ) ) {print "Login failed for $L: " . $imap->errstr . "\n"; exit(64);}
# Look in the inbox
my $nm = $imap->select('Inbox');
## Iterate through all messages
for ( my $i = 1 ; $i <= $nm ; $i++ ) {
my $es = Email::Simple->new( join '', @{ $imap->get($i) } );
$Body = $es->body;
use Net::SMTP::SSL;
my $smtp;
if (not $smtp = Net::SMTP::SSL->new($Server, Port => 465, Debug => 0)) {warn "Could not connect to $Server!\n"; $method = 'Server Fail'; $error=$@;} else {
if (not $smtp->auth($L2, $P2)) {warn "Authentication Failed! Login: $L sending from $from TO $to\n"; $method = "Authentication Fail Login: $L2 $P2 sending from $from TO $to\n"; $error=$@;} else {
$smtp->mail($from . "\n");
$smtp->to($to . "\n");
$smtp->data();
$smtp->datasend("MIME-Version: 1.0\n");
$smtp->datasend("From: " . $from . "\n");
$smtp->datasend("To: " . $to . "\n");
$smtp->datasend("Subject: " . $subject . "\n");
$smtp->datasend("\n");
$smtp->datasend($Body . "\n");
$smtp->dataend();
# Check it was sent correctly
$result = $smtp->message();
if ($result !~ /OK/i) {$error = $result; if ($result =~ /quota exceeded/) {$method = 'Quota Blocking'; } else {$method = 'Bad Address';}}
$smtp->quit;
# Copy this message
$imap->create_mailbox( "Inbox.Processed" );
$imap->copy( $i, "Inbox.Processed" ) or die $imap->errstr;
$imap->delete( $i );