Creating mail.pl
Now that we know the requirements, let’s create the code. To keep
the code as cross-platform as possible, we start by carefully invoking
the Perl interpreter. The two hyphens (--)
end
further options from taking effect, in case some system parses multiple
lines or extra characters and tries to pass them to Perl.
#!/usr/bin/perl --
Next, we need to include (“use” in Perl terminology) some Perl packages. The Socket package allows us to open TCP network sockets, which we will need in order to speak SMTP to our mail server. The other package, strict, forces more careful programming style in Perl. Variables, for instance, need to be explicitly declared. As a matter of course, one should probably use strict whenever doing network programming. It can save a lot of aggravation.
# Required packages use strict; use Socket; # Required for network communications
Note
While the ability to “slang” in Perl is wonderful for fast scripts, it can make Perl code hard to read and/or maintain. For code that will be around year after year, it is better to use strict. Additionally, strict is a good idea when performing any kind of network programming, because one is affecting action at a distance. Mistakes can be harder to overcome, especially if a bug has the potential to crash a server at your Internet service provider or other sensitive site.
Some constants need to be defined. We’ll put them in one spot so we can find them. Some of these are probably overkill (such as $padchar), but defining ...
Get Programming Internet Email now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.