using mailx with perl

They have: 32 posts

Joined: Jan 2001

Hi people,

I need to send mail using mailx with perl. I must use mailx because the server where I'm working on doesn't have the sendmail prog.
Does anybody knows how to use mailx with perl?

Thank you very much.

Adolfo

They have: 17 posts

Joined: Jan 2002

Hi

MAILX is a commandline tool, There is a nice (mailx.pm) written in PERL, it was just updated in the middle of "Jan", it is part of the MAIL:BOX, MAIL:TRANSPORT package...

It comes with a few simple sample scripts that show you how to use the many features........

Latest release Mail-Box-2.007.tar.gz
Released 14th January 2002
Author Information
Name Mark Overmeer
Email [email protected]
CPAN Directory MARKOV
Homepage http://mark.overmeer.net

http://www.cpan.org/authors/id/M/MA/MARKOV/Mail-Box-2.007.tar.gz

F!

They have: 32 posts

Joined: Jan 2001

Thanx !!

They have: 601 posts

Joined: Nov 2001

Ugh. mailx and mailto are ugly! Why don't you try a more portable solution through using sockets?

Here's a starting point for you. Sockets is a perl module that comes bundled with the default installation so you shouldn't require any downloads and this should work on all systems with perl installed.

use Socket;

$smtp_server = 'smtp.domain.com';
$host_name   = 'XXX';

$from    = '[email protected]';
$to      = '[email protected]';
$subject = "hi";
$body    = "hello";

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

sub send_mail {

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

   $body =~ s/\n\.[\r|\n]/\n. $1/g;  
 
   my $proto = getprotobyname('tcp');
   socket(SERVER, AF_INET, SOCK_STREAM, $proto);
   my $iaddr = gethostbyname($smtp_server);
   my $port = getservbyname('smtp', 'tcp');
   my $sin = sockaddr_in($port, $iaddr);
  
   my $sreply;

   connect(SERVER, $sin);
   recv SERVER, $sreply, 512, 0;  
  
   send SERVER, "HELO $host_name\r\n", 0;
   recv SERVER, $sreply, 512, 0;
  
   send SERVER, "MAIL From:<$from>\r\n", 0;
   recv SERVER, $sreply, 512, 0;
  
   send SERVER, "RCPT To:<$to>\r\n", 0;  
   recv SERVER, $sreply, 512, 0;  
  
   send SERVER, "DATA\r\n", 0;  
   recv SERVER, $sreply, 512, 0;
  
   send SERVER, "Subject: $subject\r\n", 0;
   send SERVER, "\r\n$body\r\n", 0;
   send SERVER, ".\r\n", 0;
   recv SERVER, $sreply, 512, 0;
  
   send SERVER, "QUIT\r\n", 0;
   recv SERVER, $sreply, 512, 0;
   close SERVER;  
}
'

Or if you really want to use mailx (please don't) then the following block of code should do what you're looking for. No need for the overheads of a perl module. Note that this is very insecure, and you should never pass any user input to it.

open(MAILPIPE, '|/usr/bin/mailx -s "Subject" user@host') or die "Can't open pipe $!";
print MAILPIPE "body text\n";
close MAILPIPE;
'

- wil

They have: 32 posts

Joined: Jan 2001

wow! that was great!! thank you very much Wil

They have: 1 posts

Joined: Aug 2009

I use
cmd = `echo \"My message with $VAR\" | mailx -s \"Hey u got a mail\" my_user\@domain.com`

Cya!

STR

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.