Click here to get back home

sending email from perl using a pipe and mailx

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
sending email from perl using a pipe and mailx bl8n8r 04-18-2008
Posted by bl8n8r on April 18, 2008, 10:05 am
Please log in for more thread options
#!/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);

Posted by A. Sinan Unur on April 18, 2008, 10:15 am
Please log in for more thread options
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

--
(remove .invalid and reverse each component for email address)

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

Posted by szr on April 18, 2008, 2:40 pm
Please log in for more thread options
A. Sinan Unur wrote:
> 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



Posted by Glenn Jackman on April 18, 2008, 3:55 pm
Please log in for more thread options
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

Posted by Jürgen Exner on April 18, 2008, 4:49 pm
Please log in for more thread options
>#!/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

Similar ThreadsPosted
Sending email using perl November 15, 2004, 2:24 pm
Sending RTF email with Perl? June 4, 2007, 5:37 pm
[perl-python] sending email January 29, 2005, 4:36 pm
sending email thru perl script May 2, 2006, 9:24 pm
Sending Outlook Email w/voting buttons through a Perl script... November 19, 2004, 11:31 am
sending ctrl+C to pipe process October 15, 2006, 8:54 am
Sending Email April 20, 2005, 4:24 pm
Switches for configuring email sending June 10, 2005, 1:29 am
sending email with Net::SMTP::SSL -- help needed September 21, 2005, 12:09 am
Net::SMTP - sending email header ? October 3, 2005, 1:51 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap