Sending email via Perl on Windows

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 });
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
CFS_developerAuthor Commented:
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!

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CFS_developerAuthor Commented:
I found something that actually works.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.