Perl and SMTP

From JmPm

Jump to: navigation, search

Sending with the default program on the machine

sendmail ('recipient@his-domain.com','subject','The text...','sender@my-domain.com');
sub sendmail {
        my ($rec,$sub,$text,$from)=@_;
        my $mailprog="/usr/lib/sendmail";
        open MAIL, "|$mailprog -t -n -oi";
        print MAIL "To: $rec\n";
        print MAIL "From: $from\n";
        print MAIL "Reply-To: $from\n";
        print MAIL "Subject: $sub\n\n";
        print MAIL "$text\n";
        close MAIL;
        return;
}

with Net::SMTP

use strict;
use Net::SMTP;
    $| = 1; # unbuffered output

    my $smtp = Net::SMTP->new(
                           Host => 'il-ex01.Corp.Exlibrisgroup.com',  #  'mailhost'
                           #Host => 'mail.netvision.net.il',  #  'mailhost'
                           Hello => 'kuku.co.il',  #  SMTP requires that you identify yourself...
                           Timeout => 30, # Maximum time, in seconds, to wait for a response from the SMTP server
                           Debug   => 1,  # use "1" to debug
                          );
    if(!defined($smtp) || !($smtp)) {
            print "SMTP ERROR: Unable to open smtp session.\n";
            exit;
    }

    $smtp->mail('someone2@example.com');

   $smtp->recipient('ori.killer@exlibris.co.il');
    print "__code: ",$smtp->code ,"\n";
    print "__message: ",$smtp->message ,"\n";
    #print "__ res:",$smtp->response(),"\n";
    print "__Status: ",$smtp->status ,"\n";

    $smtp->data();
    $smtp->datasend("From: me\@example.com\n");
    $smtp->datasend("To: to\@domain.com\n");
    $smtp->datasend("Subject: This is a test\n");

if (0) {
    $smtp->datasend("MIME-Version: 1.0\n");
    $smtp->datasend("Content-Disposition: attachment; filename=\"/tmp/testfile.txt\"\n");
    $smtp->datasend("Content-Type: application/text; name= attachment.htm\n");
    $smtp->datasend();
}
    $smtp->datasend("line1\nline2\n\n");


    print "__domain: ",$smtp->domain,"\n";

    $smtp->quit;

WIth Net::SMTP::SSL

For some reason, it works for me only from Windows (not from UNIX).

use strict;
use warnings;
use Net::SMTP::SSL;
##############################
sub send_mail {
    my $to = $_[0];
    my $subject = $_[1];
    my $body = $_[2];

    my $from = 'killero@gmail.com';
    my $password = 'my_PW';
    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";
    print "Auth good\n";

    $smtp->mail($from . "\n");
    $smtp->to($to . "\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('ori.killer@exlibris.co.il', 
	'The server has just blew up', 
	"Here are the details:\nSome more details...");

my $input = <>;
Personal tools