1a.) For TLS, use the following command:

openssl s_client -connect smtp.gmail.com:587 -starttls smtp

1b.) For SSL, use the following command:

openssl s_client -connect smtp.gmail.com:465

2.) Both  outputs of the preceding commands are long but you we are just interested in the entries that starts with:

-----BEGIN CERTIFICATE-----

and ends with:

-----END CERTIFICATE-----

3.) Copy that block of entries to a new file like smtp.gmail.com.pem.

This file is now the certificate file.