Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Send a mail in Win32 with Gmail

by saintex (Scribe)
on Jun 10, 2010 at 17:30 UTC ( [id://844071]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks,
It seems to me there is not a really simple way to send a mail through Gmail in Perl, using a windows platform (I'm a little suprised about that).


I'm writing a little program, that copy and save some files (a simple backup operation on some important files).
So I thought to send reports in emails to system admin with success or failure.

because that program runs on various computers (with both linux e windows desktops), on Win32 I would like to use a Perl distribution easy-to-install on windows.
So I used ActivestatePerl.

My first attempt was with:
use MIME::Lite; use Net:SMTP;

No problem with a linuxbox, I ported that code on Win32 with ActiveState Perl, and I found a lot of problems.

First of all, there is not
Net::SMTP:TLS on ActiveState distribution.
I tried to install it, but I will need OpenSSL and more stuff.
I installed it, but I have a DLL error in OpenSSL libaries.
So I tried with Email-Send package,
but with same result (need TLS).

Moreover there is not in ActiveState ppms repository the pacakge with Email-Send-Gmail: there is in other ppm distributions, but it has the same Email-Send and Net-SMTP-TLS issue.

Finally I tried with Mail::Sendmail ,
but only the developer version (not the main version) uses SMTP Authentication
(I mean this one:
http://search.cpan.org/~mivkovic/Mail-Sendmail-0.79_16/ )

So that is my question: how can I install Sendmail-0.79_16 on ActiveState distribution of Perl in a simple way (I have already tried to put it in a /Perl/Site/lib/Mail directory andI tried to link it through use , but without success).

I need to install it on 5/6 computers and a really hard installation is not a good idea.
Or... someone knows a simple way to send an email trough Gmail with Windows?

Sorry for my English (I'm not a natural English speaker, so maybe I have some errors on my text).

Thank you for your answers

Replies are listed 'Best First'.
Re: Send a mail in Win32 with Gmail
by technojosh (Priest) on Jun 10, 2010 at 19:19 UTC

    I personally use Net::SMTP::SSL; for Gmail on a Win32 system

    You did not mention it in your post so I thought I'd throw it out there. That is the module that has been working for me through Gmail. I don't recall if I had to add any additional repositories, but I *do* know I installed it via PPM without headache.

    If you can get that installed, check the examples or I can post a simple bit of code to help with sending a message. I don't immediately think the code will help though since your trouble seems to be with getting a proper module loaded in windows anyway...

Re: Send a mail in Win32 with Gmail
by marto (Cardinal) on Jun 10, 2010 at 17:42 UTC

    This issue has come up a few times see Using MIME::Lite with TLS or use super search to find more threads on this subject. Perhaps you need to set up more PPM repositories or simply use cpan, which has shipped with AS perl for some time now.

    Type c:\ppm install mingw from the command prompt to download a compiler and build tools required to use cpan compiling c/c++ modules or see A guide to installing modules for Win32 regarding setting up more repositories.

    Update: note the strike out.

      Done (but I think that isn't exactly a "really simple solution")!
      I have to add this repository: http://cpan.uwinnipeg.ca/PPMPackages/10xx/

      Then I have to install:
      Net-SMTP-SSL
      Net-SMTP-TSL
      Net-SSLeay
      IO-Socket-SSL


      This code works:
      use strict; use warnings; use FindBin; use Net::SMTP::TLS; use MIME::Lite; use lib ("$FindBin::Bin/CPAN"); my $msg = MIME::Lite->new( From => 'me@gmail.com', To => 'you@gmail.com', Subject => 'A message with 2 parts...', Type => 'multipart/mixed', ); #$msg->attach( # Type => 'TEXT', # Data => "Here's the GIF file you wanted", #); # $msg->attach( # Type => 'plain/txt', # Path => 'Log/output.log', # Filename => 'output.log', # ); my $mailer = new Net::SMTP::TLS( 'smtp.gmail.com', Hello => 'smtp.gmail.com', Port => 587, User => 'me@gmail.com', Password=> 'my-pwd'); $mailer->mail('me@gmail.com'); $mailer->to('you@gmail.com'); $mailer->data; $mailer->datasend($msg->as_string); $mailer->dataend; $mailer->quit;



      With this other code, until now, I have an authorization error (I don't know why):
      ---------------------------
      UPDATE: Also the second code below works fine, but you need to install an extra package: Authen-SASL
      So for the code above (TLS way), you need these packages:
      Net-SMTP-SSL
      Net-SSLeay
      IO-Socket-SSL

      For the code below (SSL way), you need these packages:
      Net-SMTP-SSL
      Net-SSLeay
      IO-Socket-SSL
      Authen-SASL

      But any suggestion, in order to have a better code, is still welcome!

      ---------------------------
      use Net::SMTP::SSL; sub send_mail { my $to = $_[0]; my $subject = $_[1]; my $body = $_[2]; my $from = 'me@gmail.com'; my $password = 'my_pwd'; my $smtp; if (not $smtp = Net::SMTP::SSL->new('smtp.gmail.com', Port => 465, Debug => 1)) { die "Could not connect to server\n"; } $smtp->auth($from, $password) || die "Authentication failed!\n"; $smtp->mail($from . "\n"); my @recepients = split(/,/, $to); foreach my $recp (@recepients) { $smtp->to($recp . "\n"); } $smtp->data(); $smtp->datasend("From: " . $from . "\n"); $smtp->datasend("To: " . $to . "\n"); $smtp->datasend("Subject: " . $subject . "\n"); $smtp->datasend("\n"); $smtp->datasend($body . "\n"); $smtp->dataend(); $smtp->quit; } # Send away! &send_mail('you@gmail.com', 'Server just blew up', 'Some more detail') +;
      Any suggestion is welcome!

        Don't set your HELO string to smtp.gmail.com. That will eventually (quickly?) get you put on a blacklist, which is annoying to get off. A HELO string is meant to be your hostname, not the hostname of the mail server you are connecting to. The mail server already knows what its hostname is ;)

        And with your new, edited code, make sure you specify the HELO string. Net::SMTP uses 'localhost.localdomain' as the default HELO string, which will also get you blacklisted.

        However, I'm not 100% sure if identifying yourself with SASL auth will protect you from being blacklisted, but it's better to be safe than sorry ;)

        "Done (but I think that isn't exactly a "really simple solution")!"

        Well it's a lot simpler than not using any of these modules/libraries and writing it all yourself :) Seriously though, which parts did you have difficulty with, installing the modules?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://844071]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (7)
As of 2024-04-24 08:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found