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

Sending e-mail with Windows

Status
Not open for further replies.

Newposter

Technical User
May 9, 2002
735
US
"How do I send email without using UNIX sendmail?
faq219-1563

This neat sub was posted by AaronGeorge.

Use the module, Net::SMTP available from CPAN. It will work on any system with or without sendmail, UNIX or windows."

Can someone please clarify this for me? I have Apache and PERL installed, have working CGI scripts, and a working email server. If I incorporate the script from this FAQ in a .cgi file to launch an email, is there any other software that I have to download? What is the module from CPAN that this refers to? Do I download and install another piece of software?

I tried using the script and nothing happens - no errors, no email server log activity of any kind. It just doesn't execute; yet other script in that directory works fine.

What I want to do is launch an email in response to a guestbook entry, to the webmaster and the guest, acknowledging the entry. The guestbook part works. I have this routine running on a unix server at a hosting company, but want to transfer the domain to my Win2K Pro server. To do this I need to get the SMTP script working. Thanks.

Newposter
"Good judgment comes from experience. Experience comes from bad judgment."
 
this following script i know for a fact works to send email and i have used it on an exchange server.
this is not a cgi but it shows how the net::smtp mod works.
Code:
#!/usr/bin/perl
use Net::SMTP;

my $from    = "me@here.com";
my $to      = "someone@anywhere.com";
my $site    = "can be a site url"; # not needed can leave ""
my $smtp_host = "my.mail.server";

# loop can be used to send multiple emails (no spamming)
for ($i=0; $i < 1; $i++)
{
   my $smtp = Net::SMTP->new($smtp_host, Hello=> $site);

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

   $smtp->datasend(&quot;To: $to\n&quot;);
   $smtp->datasend(&quot;From: $from\n&quot;);
   $smtp->datasend(&quot;Subject: Hi, from Perl\n&quot;);
   $smtp->datasend(&quot;\n&quot;);
   $smtp->datasend(&quot;Hello.\n&quot;);
   $smtp->datasend(&quot;\n&quot;);

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

if you are having trouble with the module it may be several things:

1) the module isn't installed on the comp you are running the script under

2) your email server won't accept anonymous emails. a securly configured email server won't. i.e. you won't be able to put in any old mail server address and get this to work. this eliminates people from sending spoofed emails.

3) i dont know much about cgi scripts, but don't they have to have execute priviledges? could be that isn't set.

4) i dont know what is wrong

my guess is its one of those 4. :) --Derek

&quot;Fear not the storm for this is where we grow strong.&quot;
 
Found this at:

&quot;Good news! If you are using either Active State or Indigo Perl, you won't need to download and install the Net::SMTP module. It is already part of your installation and is located in perl/site/lib/net (SMTP.pm). Again, if you plan to use this module in its current location, make sure your path is set correctly.&quot;

So I should be able to run my script as is, since I am running Active State PERL 5.6.1.

I have the SMTP.pm file in my /PERL/site/bin/Net/ folder, so I don't need to download and install any additional software, right?
Newposter
&quot;Good judgment comes from experience. Experience comes from bad judgment.&quot;
 
Try creating a small perl script that does *nothing* but send a small email, probably easier to debug that.

Make sure you set the From address to something your relay host won't mind about.

Oh -- and I think an address that's set like this:

$ToAddress = &quot;michael.j.lacey@ntlworld.com&quot;;

will need a backslash before the @ to tell perl that ntlworld is not the name of an array, like this

$ToAddress = &quot;michael.j.lacey\@ntlworld.com&quot;; Mike
________________________________________________________________

&quot;Experience is the comb that Nature gives us, after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
No dice. I created this test file called testmail.pl:

#!D:/perl/bin/perl.exe

$to = &quot;me\@domain.com&quot;;
$from = &quot;me\@domain.com&quot;;
$subject= &quot;Test of PERL script to send email notice&quot;;
$body = &quot;This is the body of the test message&quot;;

sub send_mail {

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

use Net::SMTP;

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

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

$smtp->data();
$smtp->datasend(&quot;To: $to\n&quot;);
$smtp->datasend(&quot;From: $from\n&quot;);
$smtp->datasend(&quot;Subject: $subject\n&quot;);
$smtp->datasend(&quot;\n&quot;);

foreach $body (@body) {
$smtp->datasend(&quot;$body\n&quot;);
}

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

I then placed it in my cgi-bin folder, which has known good working scripts, opened a cmd window and executed the file:

d:/perl/bin/perl.exe d:/
The cursor blinked and nothing else happened - no error messages, no e-mail, nothing in the mail server logs at all. What am I doing wrong?
Newposter
&quot;Good judgment comes from experience. Experience comes from bad judgment.&quot;
 
Hi Newposter,

You need to add some code at the end of your script to call the send_mail routine.

my $to = &quot;mickey_mouse\@disney.com&quot;;
my $from = &quot;donald_duck\@disney.com&quot;;
my $subject = &quot;I'm just testing...&quot;;
my $body = &quot;Hiya Mickey, is this thing on?\n&quot;;

send_mail($to, $from, $subject, $body);


And then change this line in sub send_mail:

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

To

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

And then replace these lines:

foreach $body (@body) {
$smtp->datasend(&quot;$body\n&quot;);
}

With just:

$smtp->datasend(&quot;$body\n&quot;); Mike
________________________________________________________________

&quot;Experience is the comb that Nature gives us, after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
YES!!!!! It works! Thank you Mike! Newposter
&quot;Good judgment comes from experience. Experience comes from bad judgment.&quot;
 
I have a command line SMTP email utility that also does the job pretty fine, and I'm happy to email it to you. Only problem is when you port your code from Win32 to Un!x, it won't work anymore. That's where using the Net::SMTP module has the advantage -- code portability!!

My 2c worth...
 
Thanks, I had unix modules at two web sites that I pay a company to host. I also have 3 sites on my Windows server that lacked this feature. I'm going to be moving the 2 sites from unix to my WinPC to save hosting fees and have more direct control. Newposter
&quot;Good judgment comes from experience. Experience comes from bad judgment.&quot;
 
Glad you're sorted Newposter :) Mike
________________________________________________________________

&quot;Experience is the comb that Nature gives us, after we are bald.&quot;

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