sending email from perl using a pipe and mailx

sending email from perl using a pipe and mailx

am 18.04.2008 16:05:38 von bl8n8r

#!/usr/bin/perl

# the main user to send the email to
$recip = 'mainuser@domain.org';

# a comma separated list of users to CC the email too
$cclist = 'ccuser1@domain.org,ccuser2@domain.org';

# open a pipe to the mailx utility and feed it a subject along with
# recipients and the cclist
open (FI, "|/usr/bin/mailx -s 'TEST: New email message' $recip -c
$cclist");

# send the body of the email message and close.
# note: the email is not sent until the pipe is closed.
printf (FI "blah\nblah blah\n");
close (FI);

Re: sending email from perl using a pipe and mailx

am 18.04.2008 16:15:49 von 1usa

bl8n8r wrote in
news:453ccec8-946e-48ea-ae92-827b8b5d0461@
59g2000hsb.googlegroups.com
:

> #!/usr/bin/perl
>
> # the main user to send the email to
> $recip = 'mainuser@domain.org';
>
> # a comma separated list of users to CC the email too
> $cclist = 'ccuser1@domain.org,ccuser2@domain.org';
>
> # open a pipe to the mailx utility and feed it a subject along
> with # recipients and the cclist
> open (FI, "|/usr/bin/mailx -s 'TEST: New email message' $recip -c
> $cclist");
>
> # send the body of the email message and close.
> # note: the email is not sent until the pipe is closed.
> printf (FI "blah\nblah blah\n");
> close (FI);

Do you have a question?

Sinan

--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/

Re: sending email from perl using a pipe and mailx

am 18.04.2008 20:40:36 von szr

A. Sinan Unur wrote:
> bl8n8r wrote in
> news:453ccec8-946e-48ea-ae92-827b8b5d0461@
> 59g2000hsb.googlegroups.com
>>
>
>> #!/usr/bin/perl
>>
>> # the main user to send the email to
>> $recip = 'mainuser@domain.org';
>>
>> # a comma separated list of users to CC the email too
>> $cclist = 'ccuser1@domain.org,ccuser2@domain.org';
>>
>> # open a pipe to the mailx utility and feed it a subject along
>> with # recipients and the cclist
>> open (FI, "|/usr/bin/mailx -s 'TEST: New email message' $recip -c
>> $cclist");
>>
>> # send the body of the email message and close.
>> # note: the email is not sent until the pipe is closed.
>> printf (FI "blah\nblah blah\n");
>> close (FI);
>
> Do you have a question?

Why assume everything that is posted is always going to be a question?
Looks to me like someone was just sharing some code they had written. It
may not be the most complex of programs, but hey, everyone's gotta'
start somewhere.

--
szr

Re: sending email from perl using a pipe and mailx

am 18.04.2008 21:55:02 von Glenn Jackman

At 2008-04-18 10:05AM, "bl8n8r" wrote:
> #!/usr/bin/perl
>
> # the main user to send the email to
> $recip = 'mainuser@domain.org';
>
> # a comma separated list of users to CC the email too
> $cclist = 'ccuser1@domain.org,ccuser2@domain.org';
>
> # open a pipe to the mailx utility and feed it a subject along with
> # recipients and the cclist
> open (FI, "|/usr/bin/mailx -s 'TEST: New email message' $recip -c
> $cclist");
>
> # send the body of the email message and close.
> # note: the email is not sent until the pipe is closed.
> printf (FI "blah\nblah blah\n");
> close (FI);

If you're looking for feedback:

#!/usr/bin/perl

use strict;
use warnings;

my $to = 'mainuser@domain.org';
my $cc = 'ccuser1@domain.org,ccuser2@domain.org';
my $subj = 'TEST: New email message';

open my $pipe, '|-', '/usr/bin/mailx', '-s', $subj, '-c', $cc, $to
or die "can't open pipe to mailx: $!\n";

print $pipe $body;

close $pipe;

die "mailx exited with a non-zero status: $?\n" if $?;

--
Glenn Jackman
"If there is anything the nonconformist hates worse than a conformist,
it's another nonconformist who doesn't conform to the prevailing
standard of nonconformity." -- Bill Vaughan

Re: sending email from perl using a pipe and mailx

am 18.04.2008 22:49:34 von jurgenex

bl8n8r wrote:
>#!/usr/bin/perl

Missing
use strict;
use warnings;

># the main user to send the email to
>$recip = 'mainuser@domain.org';
>
># a comma separated list of users to CC the email too
>$cclist = 'ccuser1@domain.org,ccuser2@domain.org';
>
># open a pipe to the mailx utility and feed it a subject along with
># recipients and the cclist
>open (FI, "|/usr/bin/mailx -s 'TEST: New email message' $recip -c
>$cclist");

Missing error handling.
Would be better to use 3-argument form of open.

># send the body of the email message and close.
># note: the email is not sent until the pipe is closed.
>printf (FI "blah\nblah blah\n");

Why are you using printf() when you don't have a format specifier?

>close (FI);

jue