?
Solved

Sending email via Perl on Windows

Posted on 2014-08-14
3
Medium Priority
?
331 Views
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 = "myEmailAddress@somewhere.com"
$EmailTo = "ToEmailAddress@somewhere.com"
$Subject = "Test email via powershell"
$Body = "This is the body of my email message `nand this is another line `nand yet another"
$SMTPServer = "smtp.gmail.com"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("myEmailAddress@gmail.com", "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 => 'smtp.gmail.com',
  port => 25,
  sasl_username => 'TestingEmail@gmail.com',
  sasl_password => 'did-it-work88'
});

my $email = Email::Simple->create(
  header => [
    To      => 'david.theMan@mySite.com',
    From    => 'david.theMan@mySite.com',
    Subject => 'Hi!',
  ],
  body => "This is my message\n",
);

sendmail($email, { transport => $transport });
0
Comment
Question by:CFS_developer
  • 2
3 Comments
 
LVL 81

Assisted Solution

by:arnold
arnold earned 1500 total points
ID: 40262431
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.
0
 

Accepted Solution

by:
CFS_developer earned 0 total points
ID: 40381029
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 = "smtp.somePlace.com"
\$emailSmtpServerPort = "123456"
\$emailSmtpUser = "no-reply\@somePlaceElse.com"
\$emailSmtpPass = "secretPassWord"
\$emailMessage = New-Object System.Net.Mail.MailMessage
\$emailMessage.From = "Alerts\@SomePlaceElese.com"
\$emailMessage.To.Add( "Alerts\@SomePlaceElese.com" )
\$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 )
END


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



Good enough!
0
 

Author Closing Comment

by:CFS_developer
ID: 40932653
I found something that actually works.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
Six Sigma Control Plans
Suggested Courses

850 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