Perl and SMTP

From JmPm

Revision as of 12:00, 9 March 2008 by Ori229 (Talk | contribs)
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.miller@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;
Personal tools