[Perl] XML mailer

Stato
Discussione chiusa ad ulteriori risposte.

imported_BlackLight

Utente Silver
16 Agosto 2007
211
8
1
98
Piccolo esercizio che ho fatto stamattina giusto per fare qualcosina di semplice su XML+Perl...
Si tratta di uno script che consente l'invio di un email prendendo le informazioni direttamente da un file XML. Un help su come strutturare tale file si può avere richiamando lo script con l'opzione --help.

Codice:
#!/usr/bin/perl


use IO::Socket;

$xml=shift or die "Usage: $0 <xml_file>   |    $0 --help\n";

if ($xml eq '--help')  {
        print <<EOF;
XML file must have a structure like this:

<?xml version="1.0" encoding="iso-8859-1"?>

<server>Here_the_SMTP_server_to_use</server>
<from>Sender</from>
<to>Recipient</to>
<fake_from>Here_the_eventual_fake_sender</fake_from>
<subject>Here_the_subject</subject>
<mail>Here the content of your mail</mail>
EOF

        die;
}

$content='';

open(IN,"< $xml") or die "Unable to open $xml: $!\n";

while (<IN>)  {
        $content .= $_;
}

close(IN);

if ($content =~ /<server>(.*)<\/server>/is)  { $server=$1; }
else  { die "You must specify a valid SMTP server\n"; }

if ($content =~ /<from>(.*)<\/from>/is)  { $from=$1; }
else  { die "No sender specified\n"; }

if ($content =~ /<to>(.*)<\/to>/is)  { $to=$1; }
else  { die "No recipient specified\n"; }

if ($content =~ /<mail>(.*)<\/mail>/is)  { $mail=$1; }
else  { die "No content specified\n"; }

if ($content =~ /<subject>(.*)<\/subject>/is)  { $sub=$1; }
else  {
        print "WARNING: You are sending a mail without subject\n";
        print "Would you like to continue anyway? (y/n) ";
        $ans=<STDIN>;
        chomp($ans);

        die if ($ans eq 'n');
}

if ($content =~ /<fake_from>(.*)<\/fake_from>/is)  { $fake=$1; }
else  { $fake=$from; }

$packet =
        "HELO $server\r\n".
        "MAIL FROM: $from\r\n".
        "RCPT TO: $to\r\n".
        "DATA\r\n".
        "From: $fake\r\n".
        "To: $to\r\n".
        "Subject: $sub\r\n".
        "$mail\r\n".
        ".\r\n";

$sock = new IO::Socket::INET(
        PeerAddr => "$server",
        PeerPort => 25,
        Proto => 'tcp',
);

die "Unable to create socket: $!\n" unless $sock;

print $sock $packet;
print "Server reply:\n\n";

while (<$sock>)  { print $_; }
close($sock);
 
Stato
Discussione chiusa ad ulteriori risposte.