sending email from perl using a pipe and mailx

#!/usr/bin/perl

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

# a comma separated list of users to CC the email too
$cclist = 'ccuser1 [at] domain.org,ccuser2 [at] 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);
bl8n8r [ Fr, 18 April 2008 16:05 ] [ ID #1945493 ]

Re: sending email from perl using a pipe and mailx

bl8n8r <bl8n8r [at] gmail.com> wrote in
news:453ccec8-946e-48ea-ae92-827b8b5d0461 [at]
59g2000hsb.googlegroups.com
:

> #!/usr/bin/perl
>
> # the main user to send the email to
> $recip = 'mainuser [at] domain.org';
>
> # a comma separated list of users to CC the email too
> $cclist = 'ccuser1 [at] domain.org,ccuser2 [at] 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 [at] 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/
1usa [ Fr, 18 April 2008 16:15 ] [ ID #1945494 ]

Re: sending email from perl using a pipe and mailx

A. Sinan Unur wrote:
> bl8n8r <bl8n8r [at] gmail.com> wrote in
> news:453ccec8-946e-48ea-ae92-827b8b5d0461 [at]
> 59g2000hsb.googlegroups.com
>>
>
>> #!/usr/bin/perl
>>
>> # the main user to send the email to
>> $recip = 'mainuser [at] domain.org';
>>
>> # a comma separated list of users to CC the email too
>> $cclist = 'ccuser1 [at] domain.org,ccuser2 [at] 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
szr [ Fr, 18 April 2008 20:40 ] [ ID #1945504 ]

Re: sending email from perl using a pipe and mailx

At 2008-04-18 10:05AM, "bl8n8r" wrote:
> #!/usr/bin/perl
>
> # the main user to send the email to
> $recip = 'mainuser [at] domain.org';
>
> # a comma separated list of users to CC the email too
> $cclist = 'ccuser1 [at] domain.org,ccuser2 [at] 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 [at] domain.org';
my $cc = 'ccuser1 [at] domain.org,ccuser2 [at] 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
Glenn Jackman [ Fr, 18 April 2008 21:55 ] [ ID #1945508 ]

Re: sending email from perl using a pipe and mailx

bl8n8r <bl8n8r [at] gmail.com> wrote:
>#!/usr/bin/perl

Missing
use strict;
use warnings;

># the main user to send the email to
>$recip = 'mainuser [at] domain.org';
>
># a comma separated list of users to CC the email too
>$cclist = 'ccuser1 [at] domain.org,ccuser2 [at] 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
jurgenex [ Fr, 18 April 2008 22:49 ] [ ID #1945513 ]
Perl » comp.lang.perl.misc » sending email from perl using a pipe and mailx

Vorheriges Thema: FAQ 4.44 How do I test whether two arrays or hashes are equal?
Nächstes Thema: Looking for a utility that lists all the functions under a