title: POP3 with TLS in Perl - help getting starttls to work?
body: <p>I wrote a Perl script that uses POP3 to retrieve mail from a Gmail account.  A while ago, the script became broken, and I suspect it was because Gmail began requiring either SSL or TLS connections when using POP3.</p>
  
  <p>I found <a href="http://blogs.perl.org/users/mike_b/2013/05/pop3-with-tls-in-perl.html" rel="nofollow">this interesting post</a> on Mike B's blog, which includes this sample code:</p>
  
  <pre><code>#!/usr/bin/env perl
  
  use strict;
  use warnings;
  
  use Net::SSLGlue::POP3;
  use Mozilla::CA;
  
  my $host  = 'pop.aol.com';
  my $login = 'myusername@aol.com';
  my $pass  = '4Radfsai8fsfd9sdf9sdf';
  
  my $pop3 = Net::POP3-&gt;new( $host) || die "Can't connect to $host: $!";
  $pop3-&gt;starttls(
      SSL_verify_mode =&gt; 1,
      SSL_ca_file =&gt; Mozilla::CA::SSL_ca_file(),
  ) || die "Can't perform starttls: $!";
  
  my $messages = $pop3-&gt;login( $login, $pass ) 
    || die "Failed to authenticate login $login on $host: $!";
  
  print "There are $messages messages on $host for $login.\n";
  </code></pre>
  
  <p>Here's a portion of my exact code (some specifics changed for security reasons):</p>
  
  <pre><code>use YAML;
  use Net::SSLGlue::POP3;
  use Mozilla::CA;
  use Email::Find;
  use Data::Dumper;
  use LWP::Simple;
  
  my $email_finder = Email::Find-&gt;new(\&amp;process_email);
  my $verbose = 1;
  my $email_account = 'my.email@mydomain.net';
  my $email_password = 'somepassword';
  my $mail_server = "mail.mydomain.net";
  
  print("\nStarted\n") if ($verbose);
  
  $pop = Net::POP3-&gt;new($mail_server) || die("Could not log on to server.");
  
  print(" - starttls using " . Mozilla::CA::SSL_ca_file() . "\n") if ($verbose);
  
  $pop-&gt;starttls(
    SSL_verify_mode =&gt; 1,
    SSL_ca_file =&gt; Mozilla::CA::SSL_ca_file()
  ) || die "Can't perform starttls: $!";
  </code></pre>
  
  <p>My issue is that...</p>
  
  <pre><code>$pop-&gt;starttls(
    SSL_verify_mode =&gt; 1,
    SSL_ca_file =&gt; Mozilla::CA::SSL_ca_file()
  ) || die "Can't perform starttls: $!";
  </code></pre>
  
  <p>... is failing, but I can't tell why.  Any suggestions?</p>
  
  <p><strong>UPDATE...</strong></p>
  
  <p>Thank you Steffen.  I've updated my code by adding Debug => 1 and outputting $IO::Socket::SSL::SSL_ERROR:</p>
  
  <pre><code>$pop = Net::POP3-&gt;new($mail_server, Debug =&gt; 1) 
      || die("Could not log on to server.");
  
  print(" - starttls using " . Mozilla::CA::SSL_ca_file() . "\n") if ($verbose);
  
  $pop-&gt;starttls(
    SSL_verify_mode =&gt; 1,
    SSL_ca_file =&gt; Mozilla::CA::SSL_ca_file()
  ) || die "Can't perform starttls: $!\n" . $IO::Socket::SSL::SSL_ERROR;
  </code></pre>
  
  <p>The error message now is: </p>
  
  <pre><code>Net::POP3&gt;&gt;&gt; Net::POP3(2.29)
  Net::POP3&gt;&gt;&gt;   Net::Cmd(2.29)
  Net::POP3&gt;&gt;&gt;     Exporter(5.63)
  Net::POP3&gt;&gt;&gt;   IO::Socket::INET(1.31)
  Net::POP3&gt;&gt;&gt;     IO::Socket(1.31)
  Net::POP3&gt;&gt;&gt;       IO::Handle(1.28)
  Net::POP3=GLOB(0xb41980)&lt;&lt;&lt; +OK Dovecot ready.
   - starttls using /usr/local/share/perl/5.10.1/Mozilla/CA/cacert.pem
  Net::POP3=GLOB(0xb41980)&gt;&gt;&gt; STLS
  Net::POP3=GLOB(0xb41980)&lt;&lt;&lt; +OK Begin TLS negotiation now.
  Can't perform starttls:
  **SSL connect attempt failed with unknown error error:14090086:SSL     
  routines:SSL3_GET_SERVER_CERTIFICATE:certificate 
  verify failed at (my script) line 26.**
  </code></pre>
  
  <p>Is this basically saying, "The Mozilla::CA certificate is not going to work for you."?</p>