Solved

Confused on Sendmail and Perl

Posted on 2004-03-25
6
347 Views
Last Modified: 2010-03-04
Hello,

I am very new at this and trying to work with books for making PERL scripts. I am trying to get a script to write to a text file and send and e-mail. I can get the script to write to a text file.  However I cannot get the perl script to send an email.  I was using sendmail. Then I later found out That was for unix only and not windows. So now what? If Sendmail is for unix only then what do I do? Could someone look at my code and give me a hand. This is job related so it is important.

@gold=split(/&/,$buffer);
foreach $ab (@gold) {
($le,$ri)=split(/=/,$ab);
$ri=~tr/+/ /;
$ri=~s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
$Regis{$le}=$ri;
}
open(TECH,">>./TEXTFILES/CO5ACCTS.txt");
open(MAIL, "|/usr/lib/sendmail -oi -t ")|| die "Cannot start sendmail: $!\n";

$email='pdf4young@hotmail.com';


print MAIL "To: $email \n";
print MAIL "Subject: New Clockin\n";
print MAIL "Clockin=$days[$wday], $months[$mon] $mday, $year, $timey:$timei:$timee\n";

close MAIL or die $!;
print "Content-type: text/html\n\n";
@days = ("Sunday","Monday","Tuesday","Wednesday","Thursday",

"Friday","Saturday");
@months = ("January","February","March","April","May","June",

"July","August","September","October","November", "December");


@days = ("Sunday","Monday","Tuesday","Wednesday","Thursday",
        "Friday","Saturday");
@months = ("January","February","March","April","May","June",
          "July","August","September","October","November",
          "December");
($sec,$min,$hr,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime(time);
$year = $year + 1900;
$timey=$hr;
$timei=$min;
$timee=$sec;
print TECH
"
Clocked in at $days[$wday], $months[$mon] $mday, $year, $timey:$timei:$timee&


\n";
close(TECH);
print qq ~CLICK HERE(<A href="http://www.chemtroninc.com/CO5ACCTSLINKS.HTML">CO5ROUTES.HTML</A>)~;


0
Comment
Question by:kidfist
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 8

Expert Comment

by:davorg
ID: 10680167
You have a couple of options.

1/ Find a Windows program that emulates sendmail. I believe that many people use one called "blat".

2/ Use the Net::SMTP module to talk directly to your SMTP server.

Dave...
0
 
LVL 48

Expert Comment

by:Tintin
ID: 10683653
davorg.

It is a common misbelief that blat is equivalent to sendmail.  Not so, blat is a MUA (Mail User Agent) and sendmail is a MTA (Mail Transport Agent).

Apart from using Net::SMTP, you're probably better off using one of the higher level modules for mail such as Mail::Sendmail (which, despite the name, will talk SMTP)

Some general comments on your script.

1.  Use the standard CGI module for parsing CGI parameters.
2.  Always check the result of opening files.
3.  The POSIX strftime will simplify all your arrays and date code into one line.
0
 
LVL 8

Expert Comment

by:davorg
ID: 10684835
It don't know enough about Windows software to comment on this,  but I understand that you can use blat in place of sendmail in things like formmail scripts. I'm pretty sure that I've seen it done. Is that not the case?

Dave...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Accepted Solution

by:
sstouk earned 500 total points
ID: 10696417
First, get the sendmail.pm script from this location: http://www.stouk.com/download/Sendmail.pm
Just save it as a text file with the name:  'Sendmail.pm' to the location where your original script is.
then try my modification of your script that uses the above module:

############################################################
# Place Sendmail.pm to the location where the script is,
# or if it complains that the module not found,
# place it into the Windows system32 folder.
use Sendmail;

@gold=split(/&/,$buffer);
foreach $ab (@gold) {
($le,$ri)=split(/=/,$ab);
$ri=~tr/+/ /;
$ri=~s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
$Regis{$le}=$ri;
}
open(TECH,">>./TEXTFILES/CO5ACCTS.txt");

###########################################################################################
my($To)='pdf4young@hotmail.com';
my($Subject) = "New Clockin";
my($From) = 'pdf4young@hotmail.com';
  # Find your smtp server, you can use youe e-mail client,
  # like Outlook and check the configuration to get the name of the server.
my($SmtpServer) = 'smtp.domain.com';
  # Create Outgoing message:
my($Message) = "Clockin=$days[$wday], $months[$mon] $mday, $year, $timey:$timei:$timee\n";

SEND_MAIL($SmtpServer,$To,$From,$Subject,$Message);      
###########################################################################################
# Print Web Page:
print "Content-type: text/html\n\n";
@days = ("Sunday","Monday","Tuesday","Wednesday","Thursday", "Friday","Saturday");
@months = ("January","February","March","April","May","June","July","August","September","October","November","December");
@days = ("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
@months = ("January","February","March","April","May","June","July","August","September","October","November","December");
($sec,$min,$hr,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = $year + 1900; $timey=$hr; $timei=$min; $timee=$sec;
# Print to the Log file:

print TECH
"
Clocked in at $days[$wday], $months[$mon] $mday, $year, $timey:$timei:$timee&


\n";
close(TECH);
# Finish printing Web Page:
print qq ~CLICK HERE(<A href="http://www.chemtroninc.com/CO5ACCTSLINKS.HTML">CO5ROUTES.HTML</A>)~;
0
 
LVL 6

Expert Comment

by:sstouk
ID: 10696421
Do not forget to set your SMTP server.
0
 
LVL 6

Expert Comment

by:sstouk
ID: 10696447
Basically to send an e-mail from Windows using Perl is not that complicated.
There are multiple solutions developed already.
Starting from a simple Sendmail.pm module, which does not send attachments (although it is possible to encode attachment in the outgoing e-mail before using Sendmail and send it);
You can use a standard Net::SMTP module to send away simple messages and this options is standard with ActiveState installation of Perl - check your ActiveState Documentation for sending e-mail example;
However my best so far is MIME::Lite.
It is not difficult to install this module and it provides the easiest way of handling e-mails WITH attachments and multiple e-mail options.
You can build your own fully functional multy-user e-mail client with it, and this is exactly what i did a few months ago.
All my utilities I built for work come standard with e-mail alert notification options.

From the above example you can see that there are two critical lines, needed to send an e-mail message:
"use Sendmail;"
and
"SEND_MAIL($SmtpServer,$To,$From,$Subject,$Message);"

Hope this helps.


0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question