University of Turku

Simple email sender

Why

In some special cases we need a tool to send automatic email. Very often from computers, where no high end email server or client is needed. For example a Linux server might want to send a status report or a Windows PC wants to send some measurement data. These could be easily handled, if your tabletop sits in the same network with a fixed address.

The problem gets difficult, if your home server wants to give you information about the temperature in your house and you are in the other end of the world and your only contact to the network is a Internet Coffee. Here the email is the easiest method.


Possibilities

There is hundred of email clients. In the high end the names Eudora, Mozilla Mail, Pegasos, MS Outlook etc have become familiar. They have a common nominator: they are interactive. They need somebody sitting beside the keyboard and monitor.

In the middle end there is for example Exim Perl Email Project and still some more. ( I gave some attemt to poth, PEP and Exim and found them both miss a kind of KISS documentation, which makes wheels roll ).

In the low end there is not so many. For windows I have found a rather good one: BLAT. You find more information from their website.


Servers

Most of the clients need a separate server in your PC to receive, classify, archieve, delete, copy and send your email. Some clients have the whole bunch in their belly.

My main interest is in Linux. I know there is for example Sendmail, which is a kind of standard in email trafic in Unix. Then there is QMail, which tries to make sendmail obsolete and of course XMail for the same goal.

These are all nice systems for the work they are intended. Still they are too heavy for the project in mind, because we are seeking a tool just to send a simple message containing a line or two to one and only receiver. I threw some keywords for Google. For example 'Simple linux email' and got 23 100 000 hits. This led me to nothing. Except gave me one new keyword: SMTP which is an acronym for Simple Mail Transfer Protocol, which sounds just what we are looking for ! Then I went another way around. In my Linux server I have PERL installed and most of the housekeeping is made with programs written for it. So let's dive. Real man reads no manuals but makes his own errors.


How To

I started dumb:
Command "find | grep perl | grep SMTP" gave me a promising answer:
./usr/lib/perl5/5.8.4/Net/SMTP.pm

So, there seems to be a module in the PERL 5.8.4 installation for some kind of SMTP.

Next logical step was to go to CPAN (Comprehensive Perl Archive Network.) There the first search for 'net smtp' gave a direct hit: module named Net::SMTP. The seek was over.

There was even a working example which I made some unimportant modifications:

 #!/usr/local/bin/perl -w
   
 my $MailFrom = "TXname\@TXdomain.com";
 my $MailTo = "RXname\@RXdomain.com"; 
 my $MailSubject = "No news is good news";    
 my $MailMessage = "Short message";

 use Net::SMTP;
 $smtp = Net::SMTP->new('smtp.mydomain.com'); 
        
 $smtp->mail($MailFrom);
 $smtp->to($MailTo);

 $smtp->data();
 $smtp->datasend("To: $MailTo\n");
 $smtp->datasend("Subject: $MailSubject\n");
 $smtp->datasend("\n");

 $smtp->datasend("$MailMessage\n");

 $smtp->dataend();
$smtp->quit;
This identifies the file as a PERL script

Senders identification  
Receivers information
mail subject
mail body

The module we are going to use
The mail server we are going to use

send information about sender
-

-
-
-
End of the header

Send the body of the message

The data ends here
The connection ends here

The TXname@TXdomain.com is important. This should be an entitled username in that domain. Better still, it should be your own ! If you use somebody others identity, you break some laws. These TXname ans RXname are not the only information that goes with the email. The headers contain exact information about the path of the email.
So: be warned !

There is no need for the Linux PC to reside in the same domain as the TXusername@TXdomain.

The email servername "smtp.mydomain.com" has to be given. Check it from your hostmaster.

From now on everything is just normal work, work, work and work.
First you got to copy this script to a file in a directory where it has right to work and give it right to do it (chmod plaa plaa).

Intended use

With this system it is possible to send information of your house or chickenhouse.
If you add this to your cron, your home server can inform you every hour or so about the IP-address of your home-ADSL. This is a nice feature, if you have a dynamic IP. The information is in the header of the email !

PTMUSTA at UTU.FI