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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sending mail using perl from windows?

Status
Not open for further replies.

StickyBit

Technical User
Jan 4, 2002
264
CA
Hi folks,

I'm new to perl and I have a question about sending mail from a windows 2000 server using Perl.

I have a perl script that runs on a windows 2000 server. When a certain condition is met, I need the script to send mail to my mail server. Basically I think I need a command line mail client like mail or mailx, so I can create a file handle, and call the mail client whenever I want to send mail. Like below:

open(MAIL, "|mailx -s Perl_Program john\@johndoe.com")
print MAIL "$some_message";
close(MAIL);

If it were a Unix box I would have no problems.....

Thanks,

StickyBit.
 
Try using the module Net::SMTP:


sub send_mail {

my($to, $from, $subject, @body) = @_;

use Net::SMTP;

my $relay = "host.com";
my $smtp = Net::SMTP->new($relay);

die "Can't open mail connection: $!" if (! defined $smtp);

$smtp->mail($from);
$smtp->to($to);

$smtp->data();
$smtp->datasend("To: to\n");
$smtp->datasend("From: $from\n");
$smtp->datasend("Suject: $subject\n");
$smtp->datasend("\n");

foreach $body (@body) {
$smtp->datasend("$body\n");
}
$smtp->dataend(); #note it's 'end' not 'send'
$smtp->quit();
}


-Aaron
 
Ok, thanks for the info however, I don't know how to use Perl modules, I'm still reading the learning perl book and haven't covered that part yet.

Do I have to download the module from CPAN?

StickyBit.
 
I think you'll be able to download it using the PPM utility which comes with ActiveState Perl.

Go into your Perl\Bin directory and type:

ppm
install Net::SMTP
quit

Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
You don't need to. If you have perl version 5.0 then you have this module. You can check which version you're using by running the following script:

#!/usr/bin/perl -w

use CGI;
use CGI qw:)all);

print header;



print "You are using Perl Version: $]";


And for the mail part, here's an example:


#!/usr/bin/perl -w

use CGI;
use CGI qw:)all);

print header;



@body = "MAIL BODY";

&send_mail('someone@host.com','you@localhost','Subject',@body);


sub send_mail {

my($to, $from, $subject, @body) = @_;

use Net::SMTP;

my $relay = "host.com";
my $smtp = Net::SMTP->new($relay);

die "Can't open mail connection: $!" if (! defined $smtp);

$smtp->mail($from);
$smtp->to($to);

$smtp->data();
$smtp->datasend("To: $to\n");
$smtp->datasend("From: $from\n");
$smtp->datasend("Suject: $subject\n");
$smtp->datasend("\n");

foreach $body (@body) {
$smtp->datasend("$body\n");
}
$smtp->dataend(); #note it's 'end' not 'send'
$smtp->quit();
}


-Aaron
 
This is a really common question. I've created a FAQ ( faq219-1563 ) with your send_mail() sub and attributed it to you Aaron, hope that's OK. Let me know if not. Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Thanks Mike for adding that to the FAQ. I have no problem with it :). Besides, i'm too lazy anyway:)

There is no Knowledge that is not power.

-Aaron
 
<grin> yeah right - that's why you answer loads of questions on TT....

Thx Aaron Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
I use the &quot;Mail::Sender&quot; module without any trouble (Win2K, Activestate Perl 5.6.0, HTML embedded mail messages). I manage the website and email communications for the little league organization in my town....

See the activestate module docs for examples. It was easy.
Snippet:

Code:
use Mail::Sender;

ref ($sender = new Mail::Sender({from => 'sender@domain.org',
					   fake_from => 'Fake From Sender Label',
					   replyto => 'replyto@domain.org',
                                 smtp => 'smtp.yoursmtpserver.com',
					   headers => &quot;MIME-Version: 1.0\r\nContent-type: text/html\r\nContent-Transfer-Encoding: 7bit&quot;,
					   subject => 'Subject Line'}))
                                 or die &quot;$Mail::Sender::Error\n&quot;;

# List of email names
open (MH,&quot;maillist.txt&quot;) or die &quot;Cannot open file mail list file\n&quot;;

# Read each mail name. Pause 3 seconds between each
my $counter=0;
while (<MH>) {
    chop;
    my $mailname = $_;
    $sender->Open({to => $mailname});

    open (INPUT,&quot;YourHTMLorTextFile.???&quot;) or die &quot;Cannot open mail message file\n&quot;;

    while (<INPUT>) {
        $sender->Send($_) or die &quot;$Mail::Sender::Error\n&quot;
        } close(INPUT);

    print &quot;[$counter] Mail sent to: $mailname \n&quot;;
    $counter++;

    $sender->Close;

    print &quot;Sleeping for 3 seconds ...&quot;;
    sleep 3;
    print &quot;get next mail name\n&quot;;
}
close(MH);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top