#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Perl mail script using use IO::Socket;


    Hello,

    I use a perl script to send an email.
    The mail has a subject and body.
    Now we've to use a MS Exchange mail server and since then we do not receive any mail.
    Before that we used a unix mail server and everything worked great.

    I found out that if we use the MS mail server and send a mail with empty body the mail arrives at the user.
    If I put just one letter in the body no mail will arrive.

    I also made a tcpdump and noticed that the script fails somewhere because in the tcpdump I do no see a message ID.


    I use the following perl script:
    #!/usr/bin/perl
    #
    # Meelskript
    #
    #
    # 25/10/04
    #
    # default configfile meelskript.cfg

    use IO::Socket;

    if (!$ARGV[0]) {
    die "usage: ./meelskript.pl <configfile> <filetosend>\n";
    }

    if(!$ARGV[1]) {
    die "usage: ./meelskript.pl <configfile> <filetosend>\n";
    }


    $configfile = $ARGV[0];
    $file = $ARGV[1];
    $date = `date +%d-%m-%Y`;
    chomp($date);

    if(!-e "$configfile") {
    die "Can't access configfile: $configfile!\n";
    }

    # All OK, go for it!

    open FILE, "<$configfile";
    @config = <FILE>;
    close FILE;
    print "using configfile: $configfile\n";

    foreach(@config) {
    if($_ =~ /^mailserver/) {
    $current = $_;
    chomp($current);
    ($temp, $mailserver)=split(/\:\t*/,$current);
    $mailserver =~ s/[\t|\s]//g;
    if(!$mailserver) {
    die "ENOMAILSERVER!\n";
    }
    }
    elsif($_ =~/^mailport/) {
    $current = $_;
    chomp($current);
    ($temp, $mailport)=split(/\:\t*\D/,$current);
    if(!$mailport) {
    die "ENOMAILPORT!\n";
    }
    }
    elsif($_ =~ /^mailaddresses/) {
    $current = $_;
    chomp($current);
    ($temp, $mailaddresses)=split(/\:\t*/,$current);
    if(!$mailaddresses) {
    die "ENOMAILADDRESSES!\n";
    }
    }

    elsif($_ =~ /^subject/) {
    $current = $_;
    chomp($current);
    ($temp, $subject)=split(/\:\t*/,$current);
    }

    }


    if(!-e $file) {
    die "file $file not found!\n";
    }

    open FILE, "<$file";
    @file = <FILE>;
    close FILE;


    # we haev teh virabiles now!@$ si teh l33t!!!!!1111oneoneone

    @mail = split(/\,/,$mailaddresses);

    foreach(@mail) {

    $mailuser = $_;
    $mailuser =~ s/\\//;

    # I need new socks!

    my $sock = new IO::Socket::INET (
    PeerAddr => $mailserver,
    PeerPort => $mailport,
    Proto => 'tcp',
    Timeout => '5'
    );

    # Do I have new socks now???!?!?!

    if(!$sock) { die "Can't reach mailserver!\n"; } # Can't run without new socks!



    print $sock "ehlo ik\r\n";
    sleep(1);
    sysread($sock, $buff, 1000);
    print "$buff";
    print $sock "mail from: big\@fake.nl\r\n";
    undef($buff);
    sleep(1);
    sysread($sock, $buff, 1000);
    print "$buff";
    print $sock "rcpt to: $mailuser\r\n";
    undef($buff);
    sleep(1);
    sysread($sock, $buff, 1000);
    print "$buff";
    print $sock "data\r\n";
    print $sock "Subject: $subject $date\r\n";

    foreach(@file) {
    print $sock $_;
    }

    print $sock ".\r\n";
    sleep(1);
    close($sock);
    }

    the mail is send from the cron:
    /root/update.sh>/root/output1 ; /root/meelskript.pl /root/meelskript1.cfg /root/output1 2>&1

    I tried a lot of things but I can find it
    Any help would be very great

    Dirk
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    929
    When you post code, place it between [code]...[/code] tags to keep it legible.

    Your script prints out the responses from the server, have you looked at those to determine what is happening?

    Is there any particular reason why you're using IO::Socket instead of a Net::SMTP or even better Mail::Transport::SMTP? Either of those would protect you from errors in the implementation of the protocol.

    I don't see anything in your code to ensure the '.' that terminates the mail body will be preceded by a \r\n.
    sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0
    The reason why we use IO::Socket is because of the following:

    1) Someone else wrote the perl script ( i can not reach him)
    2) I'm not that handy to program myself
    3) The server is a sort appliance so not all the perl libraries are there, I think Net::SMTP; is on the system.
    4)I do not know how the rewrite the script to use Net::SMTP

    Thanks,
    Dirk

IMN logo majestic logo threadwatch logo seochat tools logo