Feedback on: Creating a mailing list using Perl
Removing addresses:
By truncating, rewinding, then rewriting the file, you're opening
the opportunity for a loss of data: what can you do if your
print fails? (You don't even check its return value!) Far better
to create the new list as a temporary file, close both files, then
rename the temporary on top of the master file at the end. (This
also saves you from the hugely inefficient reading of the whole
file into memory as you can process a line at a time.)
Your check for the user's address is bogus too:
Take:
$email = 'p.a.bennett@btinternet.com'
$line = 'poadbennett@btinternet.com'
Now check:
$line =~ /^$email/o
Did you perhaps mean:
$line =~ /^\Q$email/o
instead? (The \Q quotes the metacharacters in the rest of the RE.)
As for whether the ^ is necessary or not in the pattern, it certainly
is, or 'a.bennett@btinternet.com' unsubscribing would take me too!
Calling sendmail directly
I took a sharp intake of breath when I saw you opening the
SENDMAIL filehandle. A number of things come to mind:
1. Why aren't you using a CPAN module (I'm sure there's
one to send mail somewhere)
2. Why are you passing the email address on the command line?
Imagine this address: ';rm -rf *'
Far better to call 'sendmail -t' and write the addresses on STDIN.
3. Why send a separate message for each user? Why not
group them together using the Cc: or Bcc: field? (Or for that
matter, why not use Majordomo?)
Also... a mistake on js049:
You refer to W3C's address as "http://www.w3.com" - it should
be "http://www.w3.org". Big difference.
Paul.
Worth:
Very worth reading
Length:
Just right
Technical:
Just right
Comments:
Jason,
I have been working through your excellent series of articles on Perl/CGI. They are the best on the web.
I think there may be an error in your article "creating a mailing list using Perl".
For removing names from the list, you suggest opening the file in >> mode.
I found that this did not allow me to read in the names into @emailfile
I had to open FILE in read mode, read in the names, close FILE.
Then open in >> mode. Truncate and do the regexp.
There is probably a more eleganyt way to do this, but it seems to work OK.
What do you think?
By the way -- willl you be doing any articles about using CGI with database files? I suppose this is the next step up from using simple text files with data in them.
Michael McLaughlin
Media Foundry
England
www.media-foundry.com
Worth:
Very worth reading
Length:
Just right
Technical:
Just right
Comments:
Hi,
The article was helpful, but I have a doubt. How can we get back the exit status of the sendmail program in PERL?
Sendmail returns status like EX_NOUSER, EX_NOHOST etc. I want to use these values after I send the mails.
Please help.
Thanx,
Jeena.
Worth:
Very worth reading
Length:
Just right
Technical:
Just right
Comments:
can you tell me where I could find some help on dynamically generating HTML mail? I have tried what appears to be the proper mime and content type tags - but they are consistently interpreted as the body of the email, not as control information. BTW: I know my email client is capable of displaying html email.
Worth:
Worth reading
Length:
Just right
Technical:
Not technical enough
Comments:
Good article, but I think that using the diamond operator is quite faster and more efficient, especially for larger files. (ie
while (<FILE>) {
..do something ...
}
instead of
@lines = <FILE>;
foreach (@lines) {
..do something ...
}
Also, flock() is 'blocking' by default, so there really is no need to die(). It will just stand there, waiting for the file to be released. Unless a proccess has crashed and kept the file open (in which case it will probably already have been corrupted), it should be available moments later.
Other than that, it's ok...