LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 12-09-2004, 03:01 PM   #1
hamish
Member
 
Registered: Aug 2003
Location: Edinburgh
Distribution: Server: Gentoo2004; Desktop: Ubuntu
Posts: 720

Rep: Reputation: 30
mailserver: send mail to external email address


Hello

I have a server in my flat running Gentoo. I would like to set up a mail server which will just send an emali out to my external email address (eg hamish@hotmail) with some system information, eg my disk quota.

Therefore, I would like a mail server which just send out mail, doesn't receive any mail and doesn't store any mail. Basically, I need a simple system and a fairly simple way to set it up.

I have read about sendmail, and i guess that application would do this, however, I have heard that it is a pain in the ass to configure.

I have also been reading a bit about Exim mail server which is supposedly one of the easiest to configure.

Do i need anything sophisticated to set this up, or will something simple suffice?

Thanks in advance.

Hamish

Last edited by hamish; 12-09-2004 at 03:05 PM.
 
Old 12-09-2004, 03:12 PM   #2
jlangelier
Member
 
Registered: Jun 2003
Location: Denver, CO
Distribution: Debian
Posts: 95

Rep: Reputation: 15
Sendmail would be overkill.

All you need is Perl (which is almost guaranteed to be already on your box) , and the Mail::Mailer library.

That, and about 8 lines of perl code, e.g.





Code:
use Mail::Mailer;

$mailer = Mail::Mailer->new();
$mailer->open ({From => $from_address,
                            To => $to_address,
                            Subject => $subject,
                         })

print $mailer $body;
$mailer->close;
.... would work, once you fixed any typoes, and set the variables.

Last edited by jlangelier; 12-09-2004 at 03:13 PM.
 
Old 12-09-2004, 03:41 PM   #3
hamish
Member
 
Registered: Aug 2003
Location: Edinburgh
Distribution: Server: Gentoo2004; Desktop: Ubuntu
Posts: 720

Original Poster
Rep: Reputation: 30
hey

that is great! I was worried i had to install a huge programme.

I do have perl installed. How can I install the mail::mailer lib?

Also, that script you gave, is that a perl script? So it is run by

Code:
perl email.pl
is that right? or do I need to install a programme called "mail". As suggested in the link below.

You dont' happen to have/know where I could get a how to on this subject? I have found one resource which I will also read now (http://www.hk8.org/old_web/linux/cgi/ch09_06.htm)

Thanks for your helpful reply.

hamish

Last edited by hamish; 12-09-2004 at 03:43 PM.
 
Old 12-09-2004, 03:51 PM   #4
jlangelier
Member
 
Registered: Jun 2003
Location: Denver, CO
Distribution: Debian
Posts: 95

Rep: Reputation: 15
I think you might have to install Net::SMTP library as well. But the good news is that those libraries don't need any configuring.

Also, that script I posted does not have a 'shebang' line on it, and you'll need to know how to get your message into the perl variable.

I'll tell you what. Because I am in a good mood, I'll work on it tonight, and outline the steps (I can't do this now because I'm on a Windows box now ).

Last edited by jlangelier; 12-09-2004 at 03:53 PM.
 
Old 12-09-2004, 04:08 PM   #5
hamish
Member
 
Registered: Aug 2003
Location: Edinburgh
Distribution: Server: Gentoo2004; Desktop: Ubuntu
Posts: 720

Original Poster
Rep: Reputation: 30
cheers man! I really appreciate that.

Sorry to hear you are stuck on a windows box atm!

kind regards,
Hamish
 
Old 12-09-2004, 04:24 PM   #6
jlangelier
Member
 
Registered: Jun 2003
Location: Denver, CO
Distribution: Debian
Posts: 95

Rep: Reputation: 15
Hey.

One question. This will easy if your machine has access to an outgoing mail server (for example, your ISP's mail server).

So do your machine have access to an outgoing mail server?

If you can use an email program (i.e. KMail or Pine, or whatever) to send email from your machine, that (obviously) means you have access to an outgoing mail server.

Otherwise, it'll be hard (i.e. if all you have is hotmail).
 
Old 12-09-2004, 09:30 PM   #7
jlangelier
Member
 
Registered: Jun 2003
Location: Denver, CO
Distribution: Debian
Posts: 95

Rep: Reputation: 15
OK, you inspired me to go and write my own scripts which run daily and mail stats to myself. Way cool.

Problem is, you must have access to an SMTP server. Chances are your ISP has an SMTP server. If not, let me know, and we'll share a sob and go to plan 'B' (which will likely be to find you an SMTP server )

I had planned on using the Mail::Mailer module (Perl speak for 'library'). But Mail::Mailer requires Net::SMTP, so I cut out the middleman and just used Net::SMTP.

Chances are good you already have Net::SMTP. You can verify that by doing a locate on SMTP.pm

Or, run the script. If you don't have Net::SMTP, Perl will tell you.

If you don't have Net::SMTP let me know and I'll tell you how to install it. But you probably have it (I had it in my default install, but then again, I run slackware ) You'd have to go to cpan.org, search for Net::SMTP, download the tar.gzip, unzip it, and read the README. But hopefully you've got it already.

Here's the script:





Code:
#!/usr/bin/perl

# simplemail 1.0
# Released to the public domain.  No copyright.  
# Hacked together in about a half hour by JLangelier (and it shows) 

use Net::SMTP;
use Getopt::Std;

sub Usage;


getopts('t:f:m:s:j:d');

if ($opt_d) {
	$debug = 1;
} else {
	$debug = 0;
}

if ($opt_t) {
	$toAddress = $opt_t;
} else {
	Usage;
}

if ($opt_f) {
	$fromAddress = $opt_f;
} else {
	Usage;
}

if ($opt_m) {
	$pathToMessage = $opt_m;
} else {
	Usage;
}

if ($opt_s) {
	$SMTPServer = $opt_s;
} else {
	Usage;
}

if ($opt_j) {
	$subject = $opt_j;
} else {
	Usage;
}

open (INPUT, "$pathToMessage") or die "Could not open $pathToMessage for reading\n";
while ($line = <INPUT>) {
	$message .= $line;
}
close INPUT;


if ($debug) {
	$smtp = Net::SMTP->new($SMTPServer, Debug => 1); 
} else {
	$smtp = Net::SMTP->new($SMTPServer); 
}

die "Couldn't connect to server" unless $smtp;


$smtp->mail( $fromAddress );  
$smtp->to( $toAddress ); 

$smtp->data(); 

$smtp->datasend("To:  $toAddress\n"); 
$smtp->datasend("From: $fromAddress\n");  
$smtp->datasend("Subject: $subject\n");  
$smtp->datasend("\n"); 

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

$smtp->dataend(); 

$smtp->quit();   

exit 0;

sub Usage {
	print "\nArguments (all required)\n";
	print "\t-t = to address\n";
	print "\t-f = from address\n";
	print "\t-m = path to message\n";
	print "\t-s = SMTP server\n";
	print "\t-j = Subject\n\n";
	print "Options\n";
	print "\t-d = Print SMTP debugging messages\n\n";
	print "Sample Invocation:\n";
	print "\tsimplemail -m message.txt -s some.stmphost.com -t foo@example.net -f bar@test.com -j 'Test Subject'\n";
	exit 1;
}
Here's how you run it. Save it as 'simplemailer' wherever you save your usr scripts (I put mine in /usr/local/scripts) . At the top of the script, the first line has a path to Perl. Make sure your Perl is there (do a 'which perl' to verify). Edit the line to point to your Perl. Make the script executable.

The script expects a path to a text file which it will send. This text file is where you would put your statisticts. Write a text file you wish to send. Execute the script to get the usage and a sample invocation.

I set up a daily cron job to run another script, which generates a text file showing disk free space, 'top' output, and a fortune for grins. Then that other script dials up (I don't have broadband) if a dialup connection is not active, and invokes the 'simplemail' script as outlined above. I assumed you knew how you were going to grab your stats and were planning to put it in a text file for sending, but if not, give a holler.

I have probably assumed some knowledge in the above that you or others may not have, so don't hesitate to ask. I hope it works. It works for me, but YMMV.

And this example, my friends, shows why Perl is called 'The Duct Tape of the Internet'. Learn it. Love it. Use it.

Oh yeah, if the email doesn't go out, use the -d switch to see the debug messages from the SMTP module.

Last edited by jlangelier; 12-09-2004 at 10:22 PM.
 
Old 12-10-2004, 03:06 AM   #8
hamish
Member
 
Registered: Aug 2003
Location: Edinburgh
Distribution: Server: Gentoo2004; Desktop: Ubuntu
Posts: 720

Original Poster
Rep: Reputation: 30
hey dude! that worked perfectly first time! Totally rocks!!

I'm not familiar with perl at all, but I think I should learn it ASAP.

A couple of question about the code, so that I understand what is happening here.

The line $smtp->mail , what does the -> do?

In the line $smtp->data(); is the "data()" a built-in smtp command, or is it specified above?

Similarly, the $smtp->datasend lines, are they built into perl and smtp already?

This is the line which says that the connection has been establish, and is good, isn't it? : $smtp = Net::SMTP->new($SMTPServer);

What does INPUT mean in this context?

Thanks so much for your help, you have inspired me to learn perl!!!

Thanks
Hamish
 
Old 12-10-2004, 09:17 AM   #9
jlangelier
Member
 
Registered: Jun 2003
Location: Denver, CO
Distribution: Debian
Posts: 95

Rep: Reputation: 15
Quote:
The line $smtp->mail , what does the -> do?
Most modules are object oriented. At that point in the code, $smtp is a Net::SMTP object. The -> calls the 'mail' method. If you don't know object oriented programming, that's not going to make alot of sense.

The good news is that you can write perl without knowing object oriented programming. Just follow the examples for the modules, and that can be the only time you need to do object oriented stuff.

Quote:
In the line $smtp->data(); is the "data()" a built-in smtp command, or is it specified above?
Again, that is a call to the 'data' method for the Net::SMTP object. Because it has no arguments, I'm assuming it initializes the object (I just copied that code from the module documentation which is why I'm not sure).

Quote:
What does INPUT mean in this context?
INPUT, in that context, is a file handle. I opened a file and gave it the handle 'INPUT' (I could have called it FOOBAR for that matter) and from then on I can refer to that file using the handle: INPUT.


Quote:
Thanks so much for your help, you have inspired me to learn perl!!!
The great thing about Perl is that you can begin writing it very quickly, just using Perl 'baby talk'. Think of Perl as kind of a high-powered shell scripting language. The really excellent part about it is that there are thousands of different modules, which can do pretty much anything you need with ease.

Last edited by jlangelier; 12-10-2004 at 10:55 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Send root mail to email address GUIPenguin Linux - General 1 03-01-2005 07:16 PM
how to send logs (etc.) to email address??? win32sux Slackware 7 08-26-2004 12:48 PM
Exim cannot receive email from external address davidas Linux - Software 1 04-23-2004 03:17 AM
Send mail listening address? bradyc Linux - Newbie 4 02-06-2004 08:49 AM
Cannnot send external email... philg Linux - General 2 08-23-2002 09:45 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 02:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration