How to send email with perl, or at least control Outlook Express


J

Jack

Hi for some reason I can send email and the target receives it with my
James mailserver via Outlook Express, but not via BLAT. I am using
user/pass authentication.. can anyone point me to a script that works
and is simple for
1- send an email directly (problem with below is email never shows up
at destination)
OR 2-controlling Outlook Express to simply send an email (Undefined
subroutine &threads::new called at outlook_api_send.pl line 83.)

Neither of the examples work below
# 1 -
use Net::SMTP;

$smtp = Net::SMTP->new("smtp.xyz.com", Timeout => 5,Port =>25);

$ret = $smtp->auth("barry","password");

if ( ! $ret ) {
printf("Authentication failed\n");
} else {
printf("Authentication succeeded\n");
}

if ( ! $smtp ) {
die "Connection to SMTP server failed\n";
}


# Start a new email, and set the From address
$smtp->mail("(e-mail address removed)");

# We're sending this to three people
@recipients = (
"(e-mail address removed)"
);

$smtp->recipient(@recipients);

# Build the mail's headers, including the From: and To: address
$headers = <<"EOH";
From: Barry <[email protected]>
To: Bill <[email protected]>
Subject: This is a test
Date: Sun, 9 Mar 2008 00:03:46 +1100
EOH

# Build the content of the mail:
$message = <<"EOM";
This is a test. Please ignore this message.
EOM

# Now send it out:
$smtp->data($headers . "\n" . $message);

# Close the connection to the SMTP server.
$smtp->quit();

############################
#2 Error: (Undefined subroutine &threads::new called at
outlook_api_send.pl line 83)
# This script sends email via Outlook and MS Exchange and works around
the MS service pack security update
# So now you don't need outlook running and you don't have to press a
button to complete sending the mail.
# The code is based on one found at the perlmonks forum. The one on
the forum had some bugs and did not work for me
# as stated with the security prompts.
#
# Created by: me
#
# Usage:
# perl domail.pl <options>
# <options>: (M) = Mandetory; (O) = Optional
#
# --email (M) target email address in full
# --to (M) target name in full
# --lead (M) Leads name in full
# --sr (M) SR number
# --cd (M) Expected Date for Completion.
use strict;
use Thread;

use Win32::OLE;
use Win32::OLE qw(in with);
use Win32::GUI;
use Win32::GuiTest;
use IO::File;
use Getopt::Long;
use File::Basename;
use POSIX qw(tmpnam);


my $progname = basename($0);

## These variable will be used to store the command line argufromnts
my $email; #= ''; # (M) target email address in full
my $to; #= ''; # (M) target name in full
my $lead; #= ''; # (M) Leads name in full
my $sr; #= ''; # (M) SR number
my $cd; #= ''; # (M) usernafrom to log into unix box

my $true = 1;
my $false = 0;


GetOptions ("email=s" => \$email,
"to=s" => \$to,
"lead=s" => \$lead,
"sr=s" => \$sr,
"cd=s" => \$cd);

sub usage
{
print("\nUsage:");
print("\n\tperl domail.pl <options>");
print("\n");
print("\n\t<options>: (M) = Mandetory; (O) = Optional");
print("\n");
print("\n\t--email (M) target email address in full");
print("\n\t--to (M) target name in full");
print("\n\t--lead (M) Leads name in full");
print("\n\t--sr (M) SR number");
print("\n\t--cd (M) Expected Completion Date.");
print("\n");
print("\n\texample:");
print("\n\tperl $progname -email \"receiver\@mycompany.com\" -to
\"Mr Receiver\" -lead \"Mr Lead\" -sr SR0000000 -cd \"Friday 13 May
2005\"");
print("\n");
print("\n");
}

sub chkOpts
{
my $status = 1;
unless (defined $email) {print ("\nRecepient email is not
defined"); $status = 0;}
unless (defined $to) {print ("\nRecipient name is not
defined"); $status = 0;}
unless (defined $lead) {print ("\nLeads name is not
defined"); $status = 0;}
unless (defined $sr) {print ("\nSR in question is not
defined"); $status = 0;}
unless (defined $cd) {print ("\nExpected Completion Date is
not defined"); $status = 0;}

if (!$status) {usage(); exit;}

}



my $thr1 = Thread->new(\&handleOutlookSecurity);

sub findSecurityWindow
# this method detects whether a security window popped up in Outlook.
If it is
# the case, it needs to be processed so that the script can be
executed.
# Else it'll pend.
{
my $messageClass = "#32770";
my $winName = "Microsoft Outlook";
return Win32::GUI::FindWindow($messageClass,$winName);
}

sub clearSecurityWindow
# this method brings the security window on top of all the others,
hence focusing it
# and sends it a series of keystrokes in order to validate it.
{
my $securityHandle = shift;

while ($securityHandle!=Win32::GUI::GetForegroundWindow())
{
Win32::GUI::SetForegroundWindow($securityHandle);
Win32::GUI::Enable($securityHandle);
}

# now send key sequence that will allow maximum time for code to
run
# The sequence is as follows : initially the no button is
focused...
# One tab brings us to cancel, another tab to the time tick box
# a spacebar hit will tick the box. Then a tab will select the drop-
down
# list in order for us to choose the time length. One more tab
brings us to Yes. A keypress on the End key will
# select maximum time. Then a return key will validate our choice
# 2 tabs - spacebar - 2 tab - one end - one return

Win32::GuiTest::SendKeys("{TAB}");
Win32::GuiTest::SendKeys("{TAB}");
Win32::GuiTest::SendKeys("{SPACEBAR}");
Win32::GuiTest::SendKeys("{TAB}");
Win32::GuiTest::SendKeys("{TAB}");
Win32::GuiTest::SendKeys("{END}");
Win32::GuiTest::SendKeys("{ENTER}");
}

sub clearSecurityWindow2
# this method does the same as clearSecurityWindow but handles the
second security window.
{
my $securityHandle = shift;

while ($securityHandle!=Win32::GUI::GetForegroundWindow())
{
Win32::GUI::SetForegroundWindow($securityHandle);
Win32::GUI::Enable($securityHandle);
}
Win32::GuiTest::SendKeys("{TAB}");
Win32::GuiTest::SendKeys("{TAB}");
Win32::GuiTest::SendKeys("{ENTER}");
}


sub handleOutlookSecurity
{
# We don't need to sleep but just to be safe
sleep 3;
my $securityHandle = 0;

while (not ($securityHandle)) # detects Outlook's popup window
that asks whether access should be granted to
{
$securityHandle = findSecurityWindow(); # wait for security
window to pop-up...
}

# window has been found - clear it
clearSecurityWindow($securityHandle);

# We need the "Yes" button to be active
sleep 10;

$securityHandle = 0;

while (not ($securityHandle))
{
$securityHandle = findSecurityWindow(); # wait for security
window to pop-up...
}

# window has been found - clear it
clearSecurityWindow2($securityHandle);
}

sub main
{

# init OLE, COINIT_OLEINITIALIZE required when using MAPI.Session
objects
Win32::OLE->Initialize(Win32::OLE::COINIT_OLEINITIALIZE);
die Win32::OLE->LastError(),"\n" if Win32::OLE->LastError( );

# Create a new MAPI session
my $session = Win32::OLE->new('MAPI.Session') or die "Failed to
create a new MAPI Session!";

# Logon to server
my $res = $session->Logon('MS Exchange
Settings','password','False');
die "Could not log on to exchange server!" if ($res);

# Create a new message
my $msg = $session->Outbox->Messages->Add();

# Add the recipient and resolve the address
my $recipient = $msg->Recipients->Add();
$recipient->{Name} = $email;
$recipient->Resolve();

# Add your text
$msg->{Subject} = "DCA for SR $sr";
$msg->{Text} = qq/
Hello $to.

As the Assessor\/Technical Authority for the SR $sr, you are
required to participate in its Defect Case Analysis.
Please complete in the appropriate Perspective section(s) of the
DCA tool to the best of your knowledge and ability.


The due date for completion of this task is $cd.

Please work with your Lead for this SR, $lead, and be prepared to
answer relating questions if called by the Defect Council.

Thanks you for your cooperation.





Regards
The Council

Please note that this is an auto generated email. Contact your Lead
with any questions.
/;

# Send the email
# 1st argument = save copy of message
# 2nd argument = allows user to change message w/dialog box before
sent
# 3rd argument = parent window of dialog if 2nd argument is True
$msg->Update();
$msg->Send(1, 0, 0);

# Log off
$session->Logoff();

}

chkOpts();
main();
 
Ad

Advertisements

M

Michael Goerz

Jack wrote, on 03/11/2008 11:39 AM:
Hi for some reason I can send email and the target receives it with my
James mailserver via Outlook Express, but not via BLAT. I am using
user/pass authentication.. can anyone point me to a script that works
and is simple for
1- send an email directly (problem with below is email never shows up
at destination)

Does this help you?
http://www.physik.fu-berlin.de/~goerz/programs/mailpl/mailpl.html

Michael Goerz
 
J

Jack

Jack wrote, on 03/11/2008 11:39 AM:


Does this help you?http://www.physik.fu-berlin.de/~goerz/programs/mailpl/mailpl.html

Michael Goerz

Hi excellent for those looking for all the bells and whistles
(encryption, etc etc) - good work .however way too much code,
complexity, dependencies requiring install, etc for me, and when I got
it to run, ctrl-D didnt work.. is there anyway you can post a very
stripped down version that just does the very basic with basic user
authentication so I can run this ?

perl testemail4.pl [email protected] [email protected] --s
ubject=test --host=smtp.sss.com --port=25 --user=steve --pass=8989dud3

Thank you,
Jack
 
M

Michael Goerz

Jack wrote, on 03/11/2008 03:59 PM:
Hi excellent for those looking for all the bells and whistles
(encryption, etc etc) - good work .however way too much code,
complexity, dependencies requiring install, etc for me,
You can try to comment out some of the dependencies. You can definitely
get rid of 'use Mail::GPG;' and 'use Archive::Zip qw( :ERROR_CODES
:CONSTANTS );'. The program will fail if you actually use encryption or
zipped attachments, of course, but you should be find for you basic
functionality. Most of the other dependencies are probably essential,
all the network communication stuff. You can play around with commenting
out any of them, see what works and and doesn't.... But, there will
always be some dependencies left. What's the problem with using 'perl
-MCPAN'? Installing missing modules in Perl should be quite easy
and when I got
it to run, ctrl-D didnt work..
You're on Windows, right?... I don't think I ever tested the script on
Windows. It should work though. crtl+D is supposed to end standard
input. It's not a feature of the script, but of the shell. There's got
to be something equivalent on Windows. But, entering the email text on
the console is only a fallback anyway. You should call an editor for
that (--editor=notepad). It's best to set this up in the config file.
(at "$ENV{HOME}/.mailpl/mailpl.rc" by default, but you can change that
at the beginning of the script).

Next time I'll be on a Windows machine, I'll try to find out what's up
with this CTRL+D... it might be a while, though.
is there anyway you can post a very
stripped down version that just does the very basic with basic user
authentication so I can run this ?
Not really. Try commenting out dependencies if you must. You'll still
need some modules, which are all easily available.
perl testemail4.pl [email protected] [email protected] --s
ubject=test --host=smtp.sss.com --port=25 --user=steve --pass=8989dud3
I hope that's not your real password!
Thank you,
Jack
Good luck,
Michael
 
M

Michael Goerz

Michael Goerz wrote, on 03/11/2008 06:23 PM:
Jack wrote, on 03/11/2008 03:59 PM:
You can try to comment out some of the dependencies. You can definitely
get rid of 'use Mail::GPG;' and 'use Archive::Zip qw( :ERROR_CODES
:CONSTANTS );'. The program will fail if you actually use encryption or
zipped attachments, of course, but you should be find for you basic
functionality. Most of the other dependencies are probably essential,
all the network communication stuff. You can play around with commenting
out any of them, see what works and and doesn't.... But, there will
always be some dependencies left. What's the problem with using 'perl
-MCPAN'? Installing missing modules in Perl should be quite easy
You're on Windows, right?... I don't think I ever tested the script on
Windows. It should work though. crtl+D is supposed to end standard
input. It's not a feature of the script, but of the shell. There's got
to be something equivalent on Windows. But, entering the email text on
the console is only a fallback anyway. You should call an editor for
that (--editor=notepad). It's best to set this up in the config file.
(at "$ENV{HOME}/.mailpl/mailpl.rc" by default, but you can change that
at the beginning of the script).

Next time I'll be on a Windows machine, I'll try to find out what's up
with this CTRL+D... it might be a while, though.
I just googled it. On Windows, you have to press CTRL+Z instead of CTRL+D:
http://www.mhuffman.com/notes/dos/bash_cmd.htm
 
Ad

Advertisements

Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top