Cannot connect to Gmail: Certificate failure for imap.gmail.com: unable to get local issuer certificate: /C=US/O=Google Inc/CN=Google Internet Authority
The system you execute the code on is missing that certificate. There is not much more that can be said about this by the information you provided with your question. Consult the system documentation of the operating system you have how to install certificates and how to aquire the needed certificate.
See as well:
- Certificate error using IMAP in PHP