Friday 12 August 2016

Perl code for sending messages via Gmail - authentication error

Here's a comprehensive blog post to do this! http://robertmaldon.blogspot.co.uk/2006/10/sending-email-through-google-smtp-from.html Here's the chunk I used (after using CPAN to install Net::SMTP::SSL*) - gmail3.pl:
#!/usr/bin/perl -w

use Net::SMTP::SSL;

sub send_mail {
my $to = $_[0];
my $subject = $_[1];
my $body = $_[2];

my $from = 'johnny@gmail.com';
my $password = 'MySecretGmailPassword';

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('johnny@mywork.com', 'Server just blew up', 'Some more detail');
On execution, you might get this authentication error:
*******************************************************************
 Using the default of SSL_verify_mode of SSL_VERIFY_NONE for client
 is deprecated! Please set SSL_verify_mode to SSL_VERIFY_PEER
 together with SSL_ca_file|SSL_ca_path for verification.
 If you really don't want to verify the certificate and keep the
 connection open to Man-In-The-Middle attacks please set
 SSL_verify_mode explicitly to SSL_VERIFY_NONE in your application.
*******************************************************************
  at ./gmail3.pl line 15.
Net::SMTP::SSL>>> Net::SMTP::SSL(1.03)
Net::SMTP::SSL>>>   IO::Socket::SSL(1.94)
Net::SMTP::SSL>>>     IO::Socket::IP(0.21)
Net::SMTP::SSL>>>       IO::Socket(1.34)
Net::SMTP::SSL>>>         IO::Handle(1.33)
Net::SMTP::SSL>>>           Exporter(5.68)
Net::SMTP::SSL>>>   Net::Cmd(3.10)
Net::SMTP::SSL=GLOB(0x17f7a78)<<< 220 smtp.gmail.com ESMTP gg10sm6569316wjd.4 - gsmtp
Net::SMTP::SSL=GLOB(0x17f7a78)>>> EHLO localhost.localdomain
Net::SMTP::SSL=GLOB(0x17f7a78)<<< 250-smtp.gmail.com at your service, [2001:630:e4:8102:21d:d8ff:feb7:1dee]
Net::SMTP::SSL=GLOB(0x17f7a78)<<< 250-SIZE 35882577
Net::SMTP::SSL=GLOB(0x17f7a78)<<< 250-8BITMIME
Net::SMTP::SSL=GLOB(0x17f7a78)<<< 250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH
Net::SMTP::SSL=GLOB(0x17f7a78)<<< 250-ENHANCEDSTATUSCODES
Net::SMTP::SSL=GLOB(0x17f7a78)<<< 250-PIPELINING
Net::SMTP::SSL=GLOB(0x17f7a78)<<< 250-CHUNKING
Net::SMTP::SSL=GLOB(0x17f7a78)<<< 250 SMTPUTF8
Net::SMTP::SSL=GLOB(0x17f7a78)>>> AUTH LOGIN
Net::SMTP::SSL=GLOB(0x17f7a78)<<< 334 VXNlcm5hbWU6
Net::SMTP::SSL=GLOB(0x17f7a78)<<< (decoded) Username:
Net::SMTP::SSL=GLOB(0x17f7a78)>>> (decoded) johnny@gmail.com
Net::SMTP::SSL=GLOB(0x17f7a78)>>> Y2xlYXJkZWZAZ21haWwuY29t
Net::SMTP::SSL=GLOB(0x17f7a78)<<< 334 UGFzc3dvcmQ6
Net::SMTP::SSL=GLOB(0x17f7a78)<<< (decoded) Password:
Net::SMTP::SSL=GLOB(0x17f7a78)>>> (decoded) MySecretGmailPassword
Net::SMTP::SSL=GLOB(0x17f7a78)>>> YXBwbGVncmVlbg==
Net::SMTP::SSL=GLOB(0x17f7a78)<<< 534-5.7.14  Please log in via your web browser and
Net::SMTP::SSL=GLOB(0x17f7a78)<<< 534-5.7.14 then try again.
Net::SMTP::SSL=GLOB(0x17f7a78)<<< 534-5.7.14  Learn more at
Net::SMTP::SSL=GLOB(0x17f7a78)<<< 534 5.7.14  https://support.google.com/mail/answer/78754 gg10sm6569316wjd.4 - gsmtp
Authentication failed!
This is simply solved by login on to the Gmail account (via a browser) and changing the password. Remember to insert the new password into the script and away we go:
*******************************************************************
 Using the default of SSL_verify_mode of SSL_VERIFY_NONE for client
 is deprecated! Please set SSL_verify_mode to SSL_VERIFY_PEER
 together with SSL_ca_file|SSL_ca_path for verification.
 If you really don't want to verify the certificate and keep the
 connection open to Man-In-The-Middle attacks please set
 SSL_verify_mode explicitly to SSL_VERIFY_NONE in your application.
*******************************************************************
  at ./gmail3.pl line 15.
Net::SMTP::SSL>>> Net::SMTP::SSL(1.03)
Net::SMTP::SSL>>>   IO::Socket::SSL(1.94)
Net::SMTP::SSL>>>     IO::Socket::IP(0.21)
Net::SMTP::SSL>>>       IO::Socket(1.34)
Net::SMTP::SSL>>>         IO::Handle(1.33)
Net::SMTP::SSL>>>           Exporter(5.68)
Net::SMTP::SSL>>>   Net::Cmd(3.10)
Net::SMTP::SSL=GLOB(0x2ac9a78)<<< 220 smtp.gmail.com ESMTP uo4sm6542414wjc.36 - gsmtp
Net::SMTP::SSL=GLOB(0x2ac9a78)>>> EHLO localhost.localdomain
Net::SMTP::SSL=GLOB(0x2ac9a78)<<< 250-smtp.gmail.com at your service, [2001:630:e4:8102:21d:d8ff:feb7:1dee]
Net::SMTP::SSL=GLOB(0x2ac9a78)<<< 250-SIZE 35882577
Net::SMTP::SSL=GLOB(0x2ac9a78)<<< 250-8BITMIME
Net::SMTP::SSL=GLOB(0x2ac9a78)<<< 250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH
Net::SMTP::SSL=GLOB(0x2ac9a78)<<< 250-ENHANCEDSTATUSCODES
Net::SMTP::SSL=GLOB(0x2ac9a78)<<< 250-PIPELINING
Net::SMTP::SSL=GLOB(0x2ac9a78)<<< 250-CHUNKING
Net::SMTP::SSL=GLOB(0x2ac9a78)<<< 250 SMTPUTF8
Net::SMTP::SSL=GLOB(0x2ac9a78)>>> AUTH LOGIN
Net::SMTP::SSL=GLOB(0x2ac9a78)<<< 334 VXNlcm5hbWU6
Net::SMTP::SSL=GLOB(0x2ac9a78)<<< (decoded) Username:
Net::SMTP::SSL=GLOB(0x2ac9a78)>>> (decoded) johnny@gmail.com
Net::SMTP::SSL=GLOB(0x2ac9a78)>>> Y2xlYXJkZWZAZ21haWwuY29t
Net::SMTP::SSL=GLOB(0x2ac9a78)<<< 334 UGFzc3dvcmQ6
Net::SMTP::SSL=GLOB(0x2ac9a78)<<< (decoded) Password:
Net::SMTP::SSL=GLOB(0x2ac9a78)>>> (decoded) myNewSecretPassword
Net::SMTP::SSL=GLOB(0x2ac9a78)>>> QXBwbGVHcjMzbg==
Net::SMTP::SSL=GLOB(0x2ac9a78)<<< 235 2.7.0 Accepted
Net::SMTP::SSL=GLOB(0x2ac9a78)>>> MAIL FROM:
Net::SMTP::SSL=GLOB(0x2ac9a78)<<< 250 2.1.0 OK uo4sm6542414wjc.36 - gsmtp
Net::SMTP::SSL=GLOB(0x2ac9a78)>>> RCPT TO:
Net::SMTP::SSL=GLOB(0x2ac9a78)<<< 250 2.1.5 OK uo4sm6542414wjc.36 - gsmtp
Net::SMTP::SSL=GLOB(0x2ac9a78)>>> DATA
Net::SMTP::SSL=GLOB(0x2ac9a78)<<< 354  Go ahead uo4sm6542414wjc.36 - gsmtp
Net::SMTP::SSL=GLOB(0x2ac9a78)>>> From: johnny@gmail.com
Net::SMTP::SSL=GLOB(0x2ac9a78)>>> To: johnny@mywork.com
Net::SMTP::SSL=GLOB(0x2ac9a78)>>> Subject: Server just blew up
Net::SMTP::SSL=GLOB(0x2ac9a78)>>> Some more detail
Net::SMTP::SSL=GLOB(0x2ac9a78)>>> .
Net::SMTP::SSL=GLOB(0x2ac9a78)<<< 250 2.0.0 OK 1470992894 uo4sm6542414wjc.36 - gsmtp
Net::SMTP::SSL=GLOB(0x2ac9a78)>>> QUIT
Net::SMTP::SSL=GLOB(0x2ac9a78)<<< 221 2.0.0 closing connection uo4sm6542414wjc.36 - gsmtp
Installing Perl Modules with CPAN:
# perl -MCPAN -e 'shell'
Terminal does not support AddHistory.

cpan shell -- CPAN exploration and modules installation (v1.9800)
Enter 'h' for help.

cpan[1]> install Net::SMTP::SSL
Reading '/root/.cpan/Metadata'
  Database was generated on Thu, 11 Aug 2016 00:53:43 GMT
Running install for module 'Net::SMTP::SSL'
Running make for R/RJ/RJBS/Net-SMTP-SSL-1.03.tar.gz
Fetching with HTTP::Tiny:
http://cpan.netbet.org/authors/id/R/RJ/RJBS/Net-SMTP-SSL-1.03.tar.gz
Checksum for /root/.cpan/sources/authors/id/R/RJ/RJBS/Net-SMTP-SSL-1.03.tar.gz ok
Scanning cache /root/.cpan/build for sizes
............................................................................DONE

  CPAN.pm: Building R/RJ/RJBS/Net-SMTP-SSL-1.03.tar.gz

Checking if your kit is complete...
Looks good
Writing Makefile for Net::SMTP::SSL
Writing MYMETA.yml and MYMETA.json
cp lib/Net/SMTP/SSL.pm blib/lib/Net/SMTP/SSL.pm
Manifying blib/man3/Net::SMTP::SSL.3pm
  RJBS/Net-SMTP-SSL-1.03.tar.gz
  /bin/make -- OK
'YAML' not installed, will not store persistent state
Running make test
PERL_DL_NONLAZY=1 /usr/bin/perl "-MExtUtils::Command::MM" "-e" "test_harness(0, 'blib/lib', 'blib/arch')" t/*.t
t/test.t .. ok
All tests successful.
Files=1, Tests=1,  0 wallclock secs ( 0.01 usr  0.00 sys +  0.07 cusr  0.00 csys =  0.08 CPU)
Result: PASS
  RJBS/Net-SMTP-SSL-1.03.tar.gz
  /bin/make test -- OK
Running make install
Manifying blib/man3/Net::SMTP::SSL.3pm
Installing /usr/local/share/perl5/Net/SMTP/SSL.pm
Installing /usr/local/share/man/man3/Net::SMTP::SSL.3pm
Appending installation info to /usr/lib64/perl5/perllocal.pod
  RJBS/Net-SMTP-SSL-1.03.tar.gz
  /bin/make install  -- OK

cpan[2]> Terminal does not support GetHistory.
Lockfile removed.

Thursday 4 August 2016

Move files within all subdirectories - Windows CMD

So you are in a directory which have subdirectories contain files you want to move into the parent. Problem with the CMD on Windows (DOS) is that it can not do * globs for directories. This command will do:
F:\RICKRI~1>forfiles /s /m *.epub /c "cmd /c move @path %CD%"