Monthly Archives: June 2013

Send simple mail with Perl on Ubuntu

Was looking around for the easiest way to send email with Perl on ubuntu.

First, we need to make sure that smtp server is running. To check if it is already running, use this command:

nmap localhost | grep smtp

If that returns a line like:

25/tcp open smtp

then smtp is running. Otherwise execute:

sudo apt-get install postfix

(following and accept the defaults (or set them).

Following perlmonks, here is how we send mail with Perl:

#!/usr/bin/perl -w
use Mail::Sendmail; 
    From => '', 
    To => '', 
    Subject => 'You got it!', 
    Message => "Now you know how to send mail!", 

Of course, if the module is not installed, we get the following error:

Can't locate Mail/ in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.10.1 /usr/local/share/perl/5.10.1 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.10 /usr/share/perl/5.10 /usr/local/lib/site_perl .) at line XXX.
BEGIN failed--compilation aborted at line XXX.

You will need to installĀ libmail-sendmail-perl:

sudo apt-get install libmail-sendmail-perl

If after that, you get another error like this complaining that MIME::Lite is missing:

Can't locate MIME/ in @INC (you may need to install the MIME::Lite module) (@INC contains: /etc/perl /usr/local/lib/perl/5.18.2 /usr/local/share/perl/5.18.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.18 /usr/share/perl/5.18 /usr/local/lib/site_perl .) at ../util/ line 4.

then install:

sudo apt-get install libmime-lite-perl