Link to home
Start Free TrialLog in
Avatar of CFS_developer
CFS_developer

asked on

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 = "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 });
SOLUTION
Avatar of arnold
arnold
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CFS_developer
CFS_developer

ASKER

I found something that actually works.