user081608
user081608

Reputation: 1123

Mailx not sending email in Perl

I am trying to send an email in perl to myself and I can not get mailx to work correctly. Here is the whole perl file I have:

#!/usr/bin/perl

sub emailSender{
   $RECIPIENT = "test\@test.com";
   $FROM = "test\@test.com";
   $SUBJECT = "test subject";
   $BODY = @_[0];

   open (MAIL, "|mailx -s \"$SUBJECT\" $RECIPIENT");
   print MAIL $BODY;
   close MAIL;
}

emailSender("This is a test");

I am not getting any errors or warnings when I run the script. It runs correctly but does not send an email. Am I missing something here? I can't find anything in the manual.

Upvotes: 0

Views: 1519

Answers (1)

Gilles Quénot
Gilles Quénot

Reputation: 185530

Tested OK (note mail -v):

#!/usr/bin/perl

use strict; use warnings;

sub emailSender{ 
   my $RECIPIENT = '[email protected]';
   my $FROM = '[email protected]';
   my $SUBJECT = "test subject";
   my $BODY = shift;

   open (MAIL, "|mail -v -s \"$SUBJECT\" $RECIPIENT");
   print MAIL $BODY;
   close MAIL;
}

emailSender("This is a test");

But for the coding style, I would keep UPPER CASE VARIABLES only for system or Perl internals

Upvotes: 2

Related Questions