Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PERL script to send e-mail in Win32

Status
Not open for further replies.

Newposter

Technical User
May 9, 2002
735
0
0
US
I have what I hope is a simple question. I have a PERL script provided as freeware by my web hosting company to send an e-mail notification whenever a guestbook entry is made. They run Linux Redhat 7.2 and Apache, and Sendmail. I want to adapt this script to run in Win2K pro and Apache, with PERL 5.6 for Win32. Problem is I don't know the syntax to send mail via my e-mail server, which is Mail Enable Pro. The original linux script command line in question is:

open (OUTMAIL,"| "/usr/bin/sendmail -t") || die "Mail system error";

What do I use for the equivalent with Mail Enable Pro in Win32?
 
read faq219-1563

---------------------------------------
wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
Paul,

If you'd like to send some example code, as a comment on FAQ219-1563 , I'd be happy to add it to the FAQ and mark the contribution as yours. Mike
______________________________________________________________________
"Experience is the comb that Nature gives us after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
Mike,
Here is a code snippet, showing a sub-routine that composes and sends an e-mail containing a attachment. The sub-routine takes arguments for e-mail title, text etc. the required fields should be fairly obvious from the commenting at the top of the sub-routine.

The MIME::Lite module is available from CPAN:
Code:
use MIME::Lite;
#.....

sub Mail_File {
    my $fileName      = $_[0];   # To attach
    my $myMailAddress = $_[1];   # Your email address / address e-mail should appear from
    my $email_address = $_[2];   # Recipients mail address
    my $title         = $_[3];   # Email title
    my $body_message  = $_[4];   # Text in main part of e-mail
    my $fileType      = $_[5];   # Know whether attachment is 'BINARY' or 'TEXT'
    my $fileName      = $_[6];   # Name of file to attach (including path)
    my $outFileName   = $_[7];   # Name to give e-mail attachment
  
    # Create MIME::Lite mail object
    my $msg = MIME::Lite->new(
			   From     => $myMailAddress,
			   To       => $email_address,
			   Subject  => $title,
			   Type     => 'multipart/mixed',
			   );

    # Main Body of message
    $msg->attach(
		 Type     => 'TEXT',
                 Data     => $body_message
                 );

    # Attach file here
    $msg->attach(Type        => $fileType,
                 Path        => $fileName,
                 Filename    => $outFileName,
                 Disposition => 'attachment'
                 );
    # Send e-mail
    $msg->send();
} # end sub Mail_File

Hope this helps,

Paul :)
 
OK, I tried the format in the PERL FAQ FAQ219-1563 that MikeLacey suggested. I have slightly different variable names, but here is what I used:

sub guestbook_add {
my($masteremail, $useremail,@comments) = @_;

use Net::SMTP;

my $relay = "host.com";
my $smtp = Net::SMTP->new($relay)
|| die "Can't open mail connection: $!";

$smtp->mail($useremail);
$smtp->to($masteremail);

$smtp->data();
$smtp->datasend("To: $masteremail\n");
$smtp->datasend("From: $useremail\n");
$smtp->datasend("Subject: Guest Notice...\n");
$smtp->datasend("\n");

foreach $comments (@comments) {
$smtp->datasend("$comments\n");
}

$smtp->dataend();
$smtp->quit();

}

$Masteremail is hardcoded in the guestbook routine and $useremail is input from the guestbook visitor. The guestbook routine works correctly and posts the message, but no email notification is sent to the masteremail. Will have to see when I get home if there is any error logged in my email server; but no message apparently sent. Am I doing something wrong? I've used both my own host name and my ISP, whom I relay out through; no joy either way.
 
hmmm - is this the mail relay you normally use from that site? Mike
______________________________________________________________________
"Experience is the comb that Nature gives us after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
Yes. I have several domains running on my server, and for the various e-mail accounts I use the host.com for incoming (POP3) and mail.attbi.com (my ISP) for outgoing (SMTP). If I try to use host.com for outgoing, it rejects me from sending messages out under any identity but my attbi.com account. So I tried using the host.com (the domain of my server) and I tried mail.attbi.com; neither worked, and none of my 8 attempts yesterday showed up in the logs of the mail server.
 
I realise your probably not using MIME::Lite currently, but if you don't get any joy with your current tac, you could try this (also see previous post regarding mail object creation and CPAN documentation).

Code:
    $msg = MIME::Lite->new( 
                 From    =>'me@myhost.com',
                 Reply-To =>'reply@myhost.com',
                 To      =>'you@yourhost.com',
                 Cc      =>'some@other.com, some@more.com',
                 Subject =>'A message with 2 parts...',
                 Type    =>'multipart/mixed'
                 );

Regards,
Paul :)
 
I would prefer to stay with just PERL. I'm not familiar with MIME, I'd guess it's another scripting program to install? PERL should be capable of launching an email in Win32.
 
"Use the module, Net::SMTP available from CPAN"

Do I need to install a utility in addition to having incorporated this script into my cgi file? Is that the reason that the mail is not being executed? Do I use this PPM utility mentioned elsewhere on this site?
Sorry, but I'm a novice to programming and learn just enough to do the task at hand. I have to be talked through steps that seem obvious. Thanks.
 
Newposter,

Paul's suggestion is Perl - it just uses another module is all.

I think you should stick with the FAQ approach and make that work initially - it's a proven approach and will work.

Try a small script, plain Perl and not web stuff, to experiment with the process until you sort the bugs out. Mike
______________________________________________________________________
"Experience is the comb that Nature gives us after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top