This article will demonstrate how to in Perl send email in Windows. The Perl programming language is best suited for Linux environments, but on occasion there is a use case or an outdated program running on Windows that you have inherited to maintain. If this describes your scenario and you are attempting to add email functionality to it, this article is for you.
The instructions in this article assume you have a working Perl installation on Windows.
Install email modules in Perl on Windows
There are a few different options for downloading and installing Perl modules in Windows including downloading the module directly and running a series of make commands to install the module as well as downloading from cpan. Both of these options can fall short and unexpectedly fail for hard to discern reasons. We have found the easiest method to be using the ppm, otherwise known as the Perl Package Manager.
You will need the following two modules at minimum in order to send email from Perl on Windows.
To install these two Perl email modules run the following commands.
ppm install Email-MIME ppm install Email-Sender
Verify the modules were installed.
perl -e 'use Email::MIME'
If the module is successfully installed you will receive no output. Else, you will see the following error.
Can't locate Email/MIME.pm in @INC (you may need to install the Email::MIME module) (@INC contains: C:/Perl64/site/lib C:/Perl64/lib .) at -e line 1. BEGIN failed--compilation aborted at -e line 1.
If that’s the case, try installing the module again and make sure you are installing against the correct Perl installation.
The same command can be used for the Email::Sender module.
perl -e 'use Email::Sender'
If the module is not installed you will see the following error.
Can't locate Email/Sender.pm in @INC (you may need to install the Email::MIME module) (@INC contains: C:/Perl64/site/lib C:/Perl64/lib .) at -e line 1. BEGIN failed--compilation aborted at -e line 1.
Alternatively you can use the ppm GUI. In PowerShell, simply run ppm and the Perl package manager GUI will load.
After you have the Perl email modules installed on Windows, you can proceed with writing code to send mail from Perl.
Perl send email on Windows
We will start off this section with a potential error. If you are wanting to send through localhost you must have a mail server configured. Our coding examples will demonstrate how to leverage an smtp server. If you try to send through localhost you may see the following error.
unable to establish SMTP connection to (localhost) port 25
If that’s the case, proceed to the following code examples to send through an smtp server.
If the smtp connection is unauthenticated you can simply remove the credentials.
This article has demonstrated how to use Perl to send email in Windows. Let us know in the comments if you have any questions or would like to see more Perl examples on Windows. If you found this article useful, read more of our content.