Sending email via Perl on Windows

Posted on 2014-08-14
Last Modified: 2015-08-17
Sending email via Perl on Windows

I am using ActiveState Perl (v5.16.1) on Windows 7 and would like to send email via Perl.  Unfortunately, Windows machines do not have a sendmail program so I guess the principle is authenticate to a SMTP server and send email through them.  Using that methodology, I wrote the following PowerShell commands to send email and would like to translate those commands to Perl.  There seem to be quite a few modules for sending email, but I haven't gotten any to work.  

Mail::Sendmail doesn't work without sendmail;
MIME::Lite documentation on CPAN recommends using Email::Sender instead;  
I found a program using Email::Sender but it fails with
failed AUTH: 5.7.0 Must issue a STARTTLS command first. jf10sm21958990igb.2 - gsmtp.

#powershell commands to send email - this works
$EmailFrom = ""
$EmailTo = ""
$Subject = "Test email via powershell"
$Body = "This is the body of my email message `nand this is another line `nand yet another"
$SMTPServer = ""
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("", "myEmailPassword");
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

#!perl -w
#send email from perl - this fails
use v5.14;

#this fails with
#failed AUTH: 5.7.0 Must issue a STARTTLS command first. jf10sm21958990igb.2 - gsmtp

use Email::Sender::Simple qw(sendmail);
use Email::Sender::Transport::SMTP ();
use Email::Simple ();
use Email::Simple::Creator ();

my $transport = Email::Sender::Transport::SMTP->new({
  host => '',
  port => 25,
  sasl_username => '',
  sasl_password => 'did-it-work88'

my $email = Email::Simple->create(
  header => [
    To      => '',
    From    => '',
    Subject => 'Hi!',
  body => "This is my message\n",

sendmail($email, { transport => $transport });
Question by:CFS_developer
    LVL 76

    Assisted Solution

    You could add the IIS SMTP feature at which point you can create an email/file with a To From, subject (as the message header, separated by a single empty line)  and the message body in c:\inetpub\mailroot\pickup

    Once the file is no longer seen also one IIS smtp a will process and attempt to transmit it.

    There is a sendmail-nt that you also could get/install. A cygwin environment might be another option.

    Not sure whether blat is stil available as a configurable mail client that can be used to handle pthe email exchange.

    Accepted Solution

    I gave up looking for a simple direct Perl solution.  I did find a PowerShell approach that I called from Perl:

    #function:  send an email message with the given subject and body
    sub send_email ($$)
       my ($subject, $body) = @_;

       #open pipe to powershell to send email
       #I haven't found a perl module to send email on Windows
       open my $shellCmd, "| powershell - "
         or die "Unable to open file pipe to powershell:  $! \n";

       #convert string to powershell new lines
       $body =~ s|\n|`n|g;

       #specify email font
       $body = "<font size=4><pre>$body";

    #send html email via powershell
    print $shellCmd <<END;
    \$emailSmtpServer = ""
    \$emailSmtpServerPort = "123456"
    \$emailSmtpUser = "no-reply\"
    \$emailSmtpPass = "secretPassWord"
    \$emailMessage = New-Object System.Net.Mail.MailMessage
    \$emailMessage.From = "Alerts\"
    \$emailMessage.To.Add( "Alerts\" )
    \$emailMessage.Subject = "$subject"
    \$emailMessage.IsBodyHtml = \$true
    \$emailMessage.Body = "$body"
    \$SMTPClient = New-Object System.Net.Mail.SmtpClient( \$emailSmtpServer, \$emailSmtpServerPort )
    \$SMTPClient.EnableSsl = \$true
    \$SMTPClient.Credentials = New-Object System.Net.NetworkCredential( \$emailSmtpUser, \$emailSmtpPass );
    \$SMTPClient.Send( \$emailMessage )

      #close email pipe
      close ($shellCmd)
         or die $! ? "Syserr closing powershell pipe:  $! \n"
                   : "Wait status $? from powershell pipe \n";

    Good enough!

    Author Closing Comment

    I found something that actually works.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
    A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now