Category Archives: Perl

Simple Email Notifications With Perl

I’m working on an application that needs to send email notifications using a Perl-based back end.  In my spare time, I’m developing a scheduling platform for yard service/lawn care applications.  In prototyping solutions on a Godaddy shared hosting server, my ability to install Perl packages in the hosted environment is very limited, so I am forced to work with what is available to me.  For sending email, the available Perl packages include Net::SMTP.  Luckily, using Net::SMTP to send email is easy and straight-forward, however I had some difficulty figuring out how to correctly send the Subject part of an email, primarily because most of the documentation on the web is not correct.  Net::SMTP is a relatively low level package, so all you need to understand for the correct usage of this package is how to send email by hand.  Here’s a good tutorial for using telnet on port 25 of a known SMTP Server to manually craft an email.

Here’s how to send an email with Perl, using Net::SMTP, which contains a proper Email Subject:

#!/usr/bin/perl
use strict;
use warnings;

use Net::SMTP;

my $smtp = Net::SMTP->new("localhost", Hello => 'mydomain.com', Timeout => 30, Debug => 1);
$smtp->mail("no-reply\@mydomain.com"); 
$smtp->to("service-request\@mydomain.com");
$smtp->data();
$smtp->datasend("Subject: Email Test");
$smtp->datasend("\r\n\r\n");
$smtp->datasend("Hallo Welt!");
$smtp->dataend;
$smtp->quit;