jpk236 has asked for the wisdom of the Perl Monks concerning the following question:

Hey monks,
I'm making a website for one of my clients, and regardless of my instruction, he still managed to specify to his hosting company that he wanted his domain on a windows machine. This will be the first time I've ever written a perl script for a machine running windows, and was wondering if there are any differences, or things I should watch out for. My primary concern will be a feedback script I've written which sends mail via sendmail. Thanks!

- Justin

Replies are listed 'Best First'.
Re: Sending Mail on a Windows Machine
by Errto (Vicar) on Feb 23, 2005 at 02:33 UTC

    Well, you certainly cannot assume that Sendmail exists on a Windows machine (frankly, you probably shouldn't assume it exists on Unix either). Instead, you should use one of the many fine mail-sending modules on CPAN (for example Mail::Mailer and Mail::Send, or perhaps Mail::SendEasy by our own gmpassos). All of these work with SMTP. Of course, you will need to know the SMTP server address. Your ISP should be able to provide this, or else you can ask them to install the Microsoft SMTP Service on your box, in which case you can use localhost.

    Update: that only addresses the email issue. For other portability issues, look at perlport.

      I tried all three of the CPAN modules you spoke of, but none of them are found when I test the script.

      Can I just throw the SendEasy.pm in the same directory as my script? Would the use Mail::SendEasy; pick up on that file?

      Thanks for your help!

      - Justin
        Actually you'll need to install the whole distribution, which consists of five files and one directory. You need to create a subdirectory called Mail, and copy SendEasy.pm into it, then create a subdirectory called SendEasy under it and put the other files in that. Shouldn't be too hard. Other posts in this thread point to more thorough discussions.
Re: Sending Mail on a Windows Machine
by Tanktalus (Canon) on Feb 23, 2005 at 05:15 UTC

    I send email from Windows boxes all the time ... but using a unix box somewhere ;-)

    Specifically, the Net::SMTP protocol is not very difficult, IMO, if you can figure out the SMTP server to use. My guess, which you may need to ask NetworkSolutions to confirm, is to use mail.networksolutions.com:

    $ dig networksolutions.com mx ; <<>> DiG 9.2.4 <<>> networksolutions.com mx ;; global options: printcmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 15900 ;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 3, ADDITIONAL: 4 ;; QUESTION SECTION: ;networksolutions.com. IN MX ;; ANSWER SECTION: networksolutions.com. 10800 IN MX 10 mail.networksolutio +ns.com. [snip]
    I know that's not definitive, but it's really darned likely. Again, confirm with the service provider, but it likely works.

    Something like...

    use Net::SMTP; my $smtp = Net::SMTP->new(q('mail.networksolutions.com')) or die "Can't connect to SMTP server"; my $ownuser = 'me@myisp.com'; $smtp->mail($ownuser); $smtp->to('joe@somewhereelse.net'); $smtp->data(); $smtp->datasend('Subject: Email from the server.',"\n"); $smtp->datasend('Date: ', scalar(time), "\n"); $smtp->datasend('To: ', join(', ', @emailaddrs), "\n"); $smtp->datasend('From: Packaging ' . $plat->platformID() . "<$ownaddr> +\n"); $smtp->datasend('X-Mailer: Perl Net::SMTP', "\n"); $smtp->datasend("\n"); $smtp->datasend(@message_text); $smtp->dataend(); $smtp->quit();
    Of course, there's some extra error-handling involved here, and you can add the error handling that is appropriate to your scenario (e.g., put up the right error page, or something).

    Note that instead of connecting to a single server, I actually do something more like:

    sub smtp_connect { for my $s (@server_list) { my $smtp = Net::SMTP->new($s); return $smtp if $smtp; } undef; }
    This allows me to pick a set of servers to try, and I can try them (in order). Some of my work SMTP servers are more stable than others... and the stable ones are the ones I'm not supposed to use (but I'll be darned if that gets in the way of getting my job done ;->).

Re: Sending Mail on a Windows Machine
by hsinclai (Deacon) on Feb 23, 2005 at 02:51 UTC
    As far as sending mail, I can second errto's mention of Mail::SendEasy .. it works very easily in Windows, with the added advantage of supporting smtpauth, so it should be easy to use the hosting provider's outward bound SMTP server or another one of your choosing without installing any MTAs..

    ActiveState's Perl documentation for Windows seems really good, especially the Windows quirks section ..

    Update: If your script cannot find the modules, there is a problem.. perhaps they were not installed correctly. Under Windows (assuming ActiveState Perl is installed) best to use ppm to install modules. Here are a couple of threads that may give you an idea of what is happening

    Installing modules under Windows , Modules in the Windows environment
      Well -- the problem there is that I'm not an admin on this windows machine. My client simply gave me his user/pass for his domain hosted by NetworkSolutions and said "go to town". So I'm at the mercy of NetworkSolutions, if they ever do respond to my ticket. I don't even have ssh access, so I can't even try to snoop around. :-\

      - Justin

        You don't need to be an admin to install your own perl modules, as long as there is no C portion that needs to be built. Start by creating a new directory in your home directory, say it is called mylib.

        Now, download and untar/zip the distribution files from CPAN. In the lib directory of the directory created when you unpack the distribution, you will find another directory named Mail. Copy this directory to the mylib directory you created earlier. At the top of your perl script, insert the line use lib "path/to/mylib". You can use this same method to install any perl module you need, as long as it doesn't include any C code.

        One last note. You already have a Mail directory in your personal library. If you install another module in the Mail family, you just need to copy the .pm file and any provided subdirectories into the existing Mail directory.

        More experienced monks - please feel free to add/clarify anything I may have missed.

        HTH
        digger

        Ah, got it. It should work by copying the installation structure of Mail::SendEasy to a subdirectory somewhere from another working Windows installation and then specifying that subdirectory in a "use lib" statement in your script on the hosted server .. as described by digger..

        In such a tight hosted environment I would recommend asking the provider which SMTP outbound server to use, and how to authenticate to it (rather than picking some random servers based on the hosting company's name and mx), further, ensure your web server can in fact initiate connections outward bound, and the provider doesn't filter for reasons of spam prevention .. some folks have the opinion a virtual hosting web server shouldn't send anything to the Internet with a SYN flag :)

Re: Sending Mail on a Windows Machine
by FitTrend (Pilgrim) on Feb 23, 2005 at 04:30 UTC

    I personally have used MIME::LITE and Mail::Sender. I primarily program on windows machines and find if you want to send plain emails, sendmail will work. However, if you want to send attachments, the other two modules are helpful. As in the differences in windows and *NIX, the script I've written were identical. I would recommend activestate activeperl. You can use the ppm utility from activetstate to install perl modules.

    Hope this helps

Re: Sending Mail on a Windows Machine
by jpk236 (Monk) on Feb 24, 2005 at 04:58 UTC
    YES! I GOT IT! Wow -- thank you so much everyone. Every post to my Question helped me get to the final solution. I'd like to especially express thanks to digger who really honed in on the solution. Here is the code I ended up with:
    #!/usr/bin/perl use lib 'path\to\lib'; use Mail::Mailer; $sender = 'sender@email.com'; $recipient = 'recipient@email.com'; $subject = 'test'; $message = 'test'; $smtp = new Mail::Mailer 'smtp', Server => localhost; $smtp->open({ From => $sender, To => $recipient, Subject => $subject}) + or die "Can't open: $!\n"; print $smtp $message; $smtp->close(); print "Content-type: text/html\n\n"; print "hello world<br/>\n";
    Thanks again everyone! w00t!

    - Justin
Re: Sending Mail on a Windows Machine
by jpk236 (Monk) on Feb 23, 2005 at 14:59 UTC
    Wow -- this is spectactular. So many great ideas. Thanks a lot monks!

    - Justin
Re: Sending Mail on a Windows Machine
by cfreak (Chaplain) on Feb 23, 2005 at 16:37 UTC

    I think the mail thing is pretty much covered for you. I do my own business on the side, what has worked well for me is to have my clients pay me for the hosting and I work with a reseller. That way if a client doesn't already have space (if they do I deal with it) then I ensure I have something that I can work with. Just a suggestion that may make your life easier in the future.

Re: Sending Mail on a Windows Machine
by stonecolddevin (Parson) on Feb 24, 2005 at 00:41 UTC
    Well BigApache comes packaged with a mail server/client called Mercury, I'm not sure if there's a distrobution of this outside of BigApache but I would most definitely assume so. It's a nice little program, works well as far as I've been able to tell.
    meh.
Re: Sending Mail on a Windows Machine
by manav (Scribe) on Feb 25, 2005 at 13:25 UTC
    Either use smtp (Net::SMTP or Mail::Send) or see this http://www.indigostar.com/sendmail.htm
Re: Sending Mail on a Windows Machine
by Anonymous Monk on Feb 25, 2005 at 13:47 UTC
    If your problem is to install modules, take a look in PPM for Win32.

    About Mail::SendEasy be installed with our code (not in the Perl library), you just need to have this directory structure from your script.pl:

    ./Mail/SendEasy.pm ./Mail/SendEasy/AUTH.pm ./Mail/SendEasy/Base64.pm ./Mail/SendEasy/IOScalar.pm ./Mail/SendEasy/SMTP.pm