Welcome to PHPBuilder! When posting PHP code, please use the board's [noparse]
..
[/noparse] bbcode tags as they make your code much easier to read and analyze.
As for your problem, you've got three issues to deal with. First of all, Google's GMail servers only accept secured (SSL) connections and only on certain ports (the default '25' port is the generic unsecured port number, hence why PHP can't even connect to Google's servers on that port). See this GMail help page for more info about the correct settings to use.
Second, IMAP isn't used (at least, I've never seen it used) to send mail - only manage/read it. To send mail, you must use an SMTP server (again, see the GMail help page linked above).
Finally, once you've fixed the above two issues you'll find that PHP doesn't natively support secure SMTP servers or SMTP authentication; GMail requires both. In other words, you can't use the internal mail() function. Instead, you'll have to use some 3rd-party package to connect to GMail and send your messages. Some popular choices are (in no particular order): PHPMailer (main site appears to be having issues?), XPertMailer, PEAR::Mail.