Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Email using sendmail

by alandev (Scribe)
on Sep 12, 2006 at 08:05 UTC ( [id://572472]=sourcecode: print w/replies, xml ) Need Help??
Category: E-Mail Programs
Author/Contact Info
Description: hi monks
i wrote small script to send mail from my unix box.
#!/usr/bin/perl
use Mail::Mailer;
$mailer=Mail::Mailer->new("sendmail");
print "\nRecipient Address:";
$too=<>;
chomp($too);
@to=split(";",$too);
print "\nSubject:";
$mail_subject=<>;
chomp($mail_subject);
print "\nContent:";
$mail_content=<STDIN>;
$mailer->open( { 
From    => $from,
To      => \@to,
Subject => $mail_subject
});
print $mailer $mail_content;
close($mailer);
Replies are listed 'Best First'.
Re: Email using sendmail
by McDarren (Abbot) on Sep 12, 2006 at 10:10 UTC
    Lets tidy that up a bit, shall we?

    • Enable strict, and warnings (always a good idea - and if you'd enabled them in the first place you would have realised that you do not assign a value to $from),
    • Allow the user to enter multi-line messages,
    • Apply a bit of consistency (ie. you use <> on one line, and <STDIN> on another),
    • Simplify a few of the statements,
    • Apply some sensible code indenting and a bit of whitespace, for readability.

    And this gives us:

    #!/usr/bin/perl use strict; use warnings; use Mail::Mailer; my $mailer = Mail::Mailer->new("sendmail"); print "\nRecipient Address:"; chomp (my @to = split /;/,<STDIN>); print "\nFrom:"; chomp (my $from = <STDIN>); print "\nSubject:"; chomp(my $subject = <STDIN>); print "\nContent (END to end):"; my $content; while (1) { my $line = <STDIN>; last if $line eq "END\n"; $content .= $line; } $mailer->open( { From => $from, To => \@to, Subject => $subject }); print $mailer $content; close($mailer);

    Of course, one other thing you should consider is some data validation, but that's left as an exercise for the reader :)

    Cheers,
    Darren :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://572472]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-16 17:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found