Help sending e-mail via Perl script 
Author Message
 Help sending e-mail via Perl script

Hi,

        I'm brand new to Perl scripts and I have a need to create a
script which will send e-mail to a particular e-mail address.  This
script will have to run on different UNIX servers, for which I have no
guarantee as to what they use to send their mail or what their
directory structure will be, so I need a way to make it dynamic.  I will
also need to populate the From: and Subject: fields as well as the
message text.
        I've checked CPAN as I've seen that suggested in other messages
in this group, but came up with nothing.  Any help (especially a code-
sample or two) would be greatly appreciated, as would a CC to my
mailbox.
--
Thanks,
Dave Verschleiser
Q&D Software Development

=======================================================
For the best SHAREWARE on the Net, check out:

** The Shareware Central Interactive Catalog **
** http://www.*-*-*.com/ **

Shareware authors -- visit and find out how to obtain
a listing!
=======================================================



Thu, 24 Dec 1998 03:00:00 GMT  
 Help sending e-mail via Perl script

[courtesy copy e-mailed to author(s)]



?      I'm brand new to Perl scripts and I have a need to create a
?script which will send e-mail to a particular e-mail address.  This
?script will have to run on different UNIX servers, for which I have no
?guarantee as to what they use to send their mail or what their
?directory structure will be, so I need a way to make it dynamic.  I will
?also need to populate the From: and Subject: fields as well as the
?message text.
?      I've checked CPAN as I've seen that suggested in other messages
?in this group, but came up with nothing.  Any help (especially a code-
?sample or two) would be greatly appreciated, as would a CC to my
?mailbox.

This should work pretty much everywhere.

$SENDMAIL='/usr/lib/sendmail -oi -t';
open(MAIL, "| $SENDMAIL");
print MAIL <<EOM;


Subject: E-mail Script Thing (perl)

This is the body of the message.  I'm $0 and my pid is $$.

EOM
close(MAIL);

__END__

-----

N.D.U.S. UNIX Sys. Admin.    http://abattoir.cc.ndsu.nodak.edu/~nem
"Perl did the magic.  I just waved the wand." - Me



Thu, 24 Dec 1998 03:00:00 GMT  
 Help sending e-mail via Perl script

hi,
try this:

#------------------------------------------------------------
# sendmail
#
# in:  $from       email address of sender
#      $reply      email address for replying mails
#      $to         email address of reciever
#      $smtp       name of smtp server (name or IP)
#      $subject    subject line
#      $message    (multiline) message
#
# out:  1 (TRUE)   success
#      -1          $smtphost unknown
#      -2          socket() failed
#      -3          connect() failed
#      -4          service not available
#      -5          unspecified communication error
#      -6          local user $to unknown on host $smpt
#      -7          transmission of message failed
#      -8          argument $to empty
#
# example:
#

#            $smtpserver, $subject, $message );
#
# or
#
#   sendmail($from, $reply, $to, $smtp, $subject, $message );
#
# (sub changes $_)
# don't put a single line "\n.\n" into your message
# (it will be truncated there)
#
#------------------------------------------------------------

sub sendmail {


        if (!$to) { return -8; }

    my ($proto, $port, $smptaddr);

    my ($AF_INET)     =  2;
    my ($SOCK_STREAM) =  1;

    $proto = (getprotobyname('tcp'))[2];
        $port  = 25;

    $smptaddr = ($smtp =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/)
                ? pack('C4',$1,$2,$3,$4)
                : (gethostbyname($smtp))[4];

    if (!defined($smptaddr)) { return -1; }

    if (!socket(S, $AF_INET, $SOCK_STREAM, $proto))              { return -2; }
    if (!connect(S, pack('Sna4x8', $AF_INET, $port, $smptaddr))) { return -3; }

    my($oldfh) = select(S); $| = 1; select($oldfh);

    $_ = <S>; if (/^[45]/) { close S; return -4; }

    print S "helo localhost\r\n";
    $_ = <S>; if (/^[45]/) { close S; return -5; }

    print S "mail from: $from\r\n";
    $_ = <S>; if (/^[45]/) { close S; return -5; }

    print S "rcpt to: $to\r\n";
    $_ = <S>; if (/^[45]/) { close S; return -6; }

    print S "data\r\n";
    $_ = <S>; if (/^[45]/) { close S; return -5; }

    print S "Reply-to: $reply\nSubject: $subject\n$message\r\n.\r\n";
    $_ = <S>; if (/^[45]/) { close S; return -7; }

    print S "quit\r\n";
    $_ = <S>;

    close S;
    return 1;

Quote:
}

Cheers
Christian

--
Christian Mallwitz NetConsult Communications Germany
www: http://www.imn.htwk-leipzig.de/~mallwitz
pgp: 02 30 7E 1A 7A C1 5C 16 2B 4D 53 A9 1B 05 F7 DD



Fri, 25 Dec 1998 03:00:00 GMT  
 Help sending e-mail via Perl script

Quote:

> :    my ($AF_INET)     =  2;
> :    my ($SOCK_STREAM) =  1;

> Oh my.  You've hardcoded those.  That doesn't work except by chance.
> Please see the perlipc man page for examples of socket stuff.>

OK. I fixed it. One question about

        inet_aton()
        sockaddr_in() instead of pack('Sna4x8', ...)

Are this perl function from Socket.pm ? As I use HIP Perl for NT my Socket.pm
contains only constants as AF_INET and SOCK_STREAM.

Quote:
> There's also a CPAN module for sending mail.

>     http://www.perl.com/CPAN/modules/by-category/19_Mail_and_Usenet_News/...

A quite large although ...

Christian

#------------------------------------------------------------
# sendmail
#
# in:  $from       email address of sender
#      $reply      email address for replying mails
#      $to         email address of reciever
#      $smtp       name of smtp server (name or IP)
#      $subject    subject line
#      $message    (multiline) message
#
# out:  1 (TRUE)   success
#      -1          $smtphost unknown
#      -2          socket() failed
#      -3          connect() failed
#      -4          service not available
#      -5          unspecified communication error
#      -6          local user $to unknown on host $smpt
#      -7          transmission of message failed
#      -8          argument $to empty
#
# example:
#


#            $smtpserver, $subject, $message );
#
# or
#
#   sendmail($from, $reply, $to, $smtp, $subject, $message );
#
# (sub changes $_)
# don't put a single line "\n.\n" into your message
# (it will be truncated there)
#
#------------------------------------------------------------

use Socket;

sub sendmail {


    if (!$to) { return -8; }

    my($proto) = (getprotobyname('tcp'))[2];
    my($port)  = (getservbyname('smtp', 'tcp'))[2];

    my($smptaddr) = ($smtp =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/)
                    ? pack('C4',$1,$2,$3,$4)
                    : (gethostbyname($smtp))[4];

    if (!defined($smtpaddr)) { return -1; }

    if (!socket(S, AF_INET, SOCK_STREAM, $proto))               { return -2; }
    if (!connect(S, pack('Sna4x8', AF_INET, $port, $smtpaddr))) { return -3; }

    my($oldfh) = select(S); $| = 1; select($oldfh);

    $_ = <S>; if (/^[45]/) { close S; return -4; }

    print S "helo localhost\r\n";
    $_ = <S>; if (/^[45]/) { close S; return -5; }

    print S "mail from: $from\r\n";
    $_ = <S>; if (/^[45]/) { close S; return -5; }

    print S "rcpt to: $to\r\n";
    $_ = <S>; if (/^[45]/) { close S; return -6; }

    print S "data\r\n";
    $_ = <S>; if (/^[45]/) { close S; return -5; }

    print S "Reply-to: $reply\nSubject: $subject\n$message\r\n.\r\n";
    $_ = <S>; if (/^[45]/) { close S; return -7; }

    print S "quit\r\n";
    $_ = <S>;

    close S;
    return 1;

Quote:
}

--
Christian Mallwitz NetConsult Communications Germany
www: http://www.imn.htwk-leipzig.de/~mallwitz
pgp: 02 30 7E 1A 7A C1 5C 16 2B 4D 53 A9 1B 05 F7 DD


Fri, 25 Dec 1998 03:00:00 GMT  
 Help sending e-mail via Perl script


:> :    my ($AF_INET)     =  2;
:> :    my ($SOCK_STREAM) =  1;
:>
:> Oh my.  You've hardcoded those.  That doesn't work except by chance.
:> Please see the perlipc man page for examples of socket stuff.>
:
:OK. I fixed it. One question about
:
:       inet_aton()
:       sockaddr_in() instead of pack('Sna4x8', ...)
:
:Are this perl function from Socket.pm ? As I use HIP Perl for NT my Socket.pm
:contains only constants as AF_INET and SOCK_STREAM.

Yes, they're there.  But I fear that Hip is behind the times.

--tom
--

"I sincerely hope that the future does not mean we must continually restrict
 ourselves to the tools that were available 10 years ago."



Fri, 25 Dec 1998 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. cgi script to send to URL and via email

2. Sending files via e-mail - please help.

3. Sending files via e-mail - please help.

4. Sending email via perl

5. Sending email form perl via a HTML Page

6. Sending e-mail from within a perl script - help

7. Sending mail via Perl

8. newbe help please sending arguments to a cgi script via ssi

9. Sending E-Mail via PERL (CGI)

10. Sending mail via cgi/perl with a file attached to it

11. Sending mail via CGI/Perl ?

12. Sending files via mail in perl

 

 
Powered by phpBB® Forum Software