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

Discussion in 'Perl Misc' started by Jack, Mar 11, 2008.

  1. Jack

    Jack Guest

    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("");

    # We're sending this to three people
    @recipients = (
    ""
    );

    $smtp->recipient(@recipients);

    # Build the mail's headers, including the From: and To: address
    $headers = <<"EOH";
    From: Barry <>
    To: Bill <>
    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();
     
    Jack, Mar 11, 2008
    #1
    1. Advertising

  2. 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
     
    Michael Goerz, Mar 11, 2008
    #2
    1. Advertising

  3. Jack

    Jack Guest

    On Mar 11, 11:08 am, Michael Goerz <4ward.com>
    wrote:
    > 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


    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 --to= --from= --s
    ubject=test --host=smtp.sss.com --port=25 --user=steve --pass=8989dud3

    Thank you,
    Jack
     
    Jack, Mar 11, 2008
    #3
  4. Jack wrote, on 03/11/2008 03:59 PM:
    > On Mar 11, 11:08 am, Michael Goerz <4ward.com>
    > wrote:
    >> 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

    >
    > 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 --to= --from= --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
     
    Michael Goerz, Mar 11, 2008
    #4
  5. Michael Goerz wrote, on 03/11/2008 06:23 PM:
    > Jack wrote, on 03/11/2008 03:59 PM:
    >> On Mar 11, 11:08 am, Michael Goerz <4ward.com>
    >> wrote:
    >>> 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

    >>
    >> 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.

    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
     
    Michael Goerz, Mar 12, 2008
    #5
  6. Jack

    Jack Guest

    On Mar 11, 8:52 pm, Michael Goerz <4ward.com>
    wrote:
    > Michael Goerz wrote, on 03/11/2008 06:23 PM:
    >
    >
    >
    > > Jack wrote, on 03/11/2008 03:59 PM:
    > >> On Mar 11, 11:08 am, Michael Goerz <4ward.com>
    > >> wrote:
    > >>> 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

    >
    > >> 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.

    >
    > 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- Hide quoted text -
    >
    > - Show quoted text -


    Michael - no sweat - got it to work - GREAT work on your end..just
    added --text=junk and it didnt prompt me for ctrl-d.. thanks for all
    your help.

    Best,
    Jack
     
    Jack, Mar 12, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Curt_C [MVP]
    Replies:
    0
    Views:
    602
    Curt_C [MVP]
    Apr 29, 2004
  2. Replies:
    40
    Views:
    3,412
    Steve Holden
    Dec 27, 2004
  3. Andrew Thompson
    Replies:
    0
    Views:
    493
    Andrew Thompson
    Dec 7, 2006
  4. AAaron123
    Replies:
    0
    Views:
    596
    AAaron123
    Oct 3, 2008
  5. heycooldude
    Replies:
    0
    Views:
    193
    heycooldude
    Mar 26, 2014
Loading...

Share This Page