in reply to Re: Send a mail in Win32 with Gmail
in thread Send a mail in Win32 with Gmail

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!

Replies are listed 'Best First'.
Re^3: Send a mail in Win32 with Gmail
by ltriant (Scribe) on Jun 11, 2010 at 00:07 UTC

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

Re^3: Send a mail in Win32 with Gmail
by marto (Cardinal) on Jun 11, 2010 at 08:22 UTC

    "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?

      the "difficult part" is the installation on many computers.

      For any of these I have to:
      - install perl of course;
      - configure ppm repositories;
      - download and install the three modules;
      - configure parameters for any account;

      So nothing really difficult, but now I think that an installation program that runs these operations for me will be useful. But I don't now how I can do it...

        "the "difficult part" is the installation on many computers."

        This wasn't part of your question, and is a seperate issue to how 'simple' the task is to achieve. However you don't have to do any of this. You can use pp which will package up your code, modules etc into an executable you can then run on other machines, without having to install perl or anything else. Perhaps consider using a config file for parameters.

        See Wisdom about "packaging" or transporting Perl apps or super search for other threads on the subject of distributing applications.

        Update: read the OP again and updated as above.

      With PAR is also possible to install (not only to run) external program (such as cwrsync) o run a simply user's configuration with a graphical interface or text file? Thank you!

        I've no idea what cwrsync is, though I suspect it's an offshoot of rsync. If you can do it (install this app or whatever) in your existing perl code pp will simply package that all up for you. If you're looking for your app to have a GUI there are several options, Win32::Gui, Tk and others.

        See http://par.perl.org for more about pp and PAR, what it can and can't do etc.