GPG is always returning 2 at the result. My code is as follows
$cmd = \"/usr/bin/gpg -a --recipient $to -e -o $outfile $infile\";
Where outfile
GPG is asking whether you want to continue on with the encryption using an unsigned key. Since no user can input Y it produces an error.
Y
To fix this put the following switches
--yes and --always-trust
--yes
--always-trust