using mailx with perl
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
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
Fataqui posted this at 21:57 — 1st February 2002.
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!
Adolfo posted this at 13:05 — 2nd February 2002.
They have: 32 posts
Joined: Jan 2001
Thanx !!
Wil posted this at 15:06 — 2nd February 2002.
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
Adolfo posted this at 12:33 — 4th February 2002.
They have: 32 posts
Joined: Jan 2001
wow! that was great!! thank you very much Wil
strevino posted this at 23:53 — 27th August 2009.
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.