Monday, May 23, 2011

perl send email cgi

Lots of tutorials about sending email with perl depend on some uncommon libs.
There is a common way to do so:

use Net::SMTP::SSL;


sub send_mail {
    my $to      = $_[0];
    my $subject = $_[1];
    my $body    = $_[2];



    my $from     = 'steven@gmail.com';
    my $password = 'cryptopassword';

    my $smtp;

    if (
        not $smtp = Net::SMTP::SSL->new(
            'smtp.gmail.com',
            Port  => 465,
            Debug => 1
        )
      )
    {
        die "Could not connect to server\n";
    }

    $smtp->auth( $from, $password )
      || die "Authentication failed!\n";

    $smtp->mail( $from . "\n" );
    my @recepients = split( /,/, $to );
    foreach my $recp (@recepients) {
        $smtp->to( $recp . "\n" );
    }
    $smtp->data();
    $smtp->datasend( "From: " . $from . "\n" );
    $smtp->datasend( "To: " . $to . "\n" );
    $smtp->datasend( "Subject: " . $subject . "\n" );
    $smtp->datasend("\n");
    $smtp->datasend( $body . "\n" );
    $smtp->dataend();
    $smtp->quit;
}

&send_mail('steven's@gmail.com', 'Server just blew up', 'Some more detail');


No comments:

Post a Comment