4

I am trying to use the native PHP function to send a simple test email through gmail SMTP, but it's not working. Going through various forums including Stack Overflow and the only solutions I see are the ones that recommend third-party email libraries/frameworks/api's. Does this mean that PHP's mail() does not work with Gmail, and if so, why?

The following is the code:

<?php
$to ="mail2@yahoo.com";
$sbj ="test mail";
$msg ="testing! testing!! testing!!!";

ini_set("SMTP", "ssl://smtp.gmail.com");
ini_set("smtp_port", 465);
ini_set("sendmail_from", "mail1@gmail.com");

$header ="From: " .ini_get("sendmail_from");

mail($to, $sbj, $msg, $header);

?>

And the error message I got:

Warning: mail(): Failed to connect to mailserver at "ssl://smtp.gmail.com" port 465, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\wamp\www\test\web1\test1.php on line 12

Alexander
  • 23,432
  • 11
  • 63
  • 73
okey_on
  • 2,888
  • 8
  • 28
  • 36
  • There's an answer without a 3rd party solution at http://stackoverflow.com/questions/712392/send-email-using-gmail-smtp-server-from-php-page – GDP Jun 24 '12 at 15:59
  • @GDP, i'd already seen that solution. It does recommend the PEAR mail package, but i'm looking for an answer on the PHP native mail() function, does it work with gmail or not? – okey_on Jun 24 '12 at 16:11
  • Take a look at http://stackoverflow.com/questions/10453926/phpmailer-and-100k-file-limit-for-attachments/10455296#10455296. –  Jun 24 '12 at 16:11
  • ah, I may have pasted from the wrong tab, sorry. Found this though: http://www.vishalkumar.in/2009/06/php-mail-using-gmail-smtp-tutorial/ – GDP Jun 24 '12 at 16:14
  • @holodoc, i'd also seen that one. PHPMailer is not PHP native. – okey_on Jun 24 '12 at 16:14
  • Sorry I could have sworn that I saw PHPMailer mentioned in your post. Either way mailing libraries like PHPMailer are still the recommended way of handling such complex things as SMTP authentication etc. –  Jun 24 '12 at 16:50

3 Answers3

1

The best class for you is phpmailer. There are good examples for Gmail (simple and advanced).

Farahmand
  • 2,731
  • 1
  • 24
  • 27
0

Does this mean that PHP's mail() does not work with Gmail, and if so, why?

Gmail servers require user to authenticate before sending any message, mail() does not provide any authentication method

dev-null-dweller
  • 29,274
  • 3
  • 65
  • 85
0

I use Pear along with Gmail. Works great.

http://pear.php.net/package/Mail/redirected

Mike
  • 980
  • 3
  • 15
  • 29