I have a PHP mail script that successfully sends emails to everything BUT GMail addresses, so now I'm trying to create one with PEAR's Mail and Mail_Mime packages that can send to GMail. I've gotten the impression that this is only possible if I connect through GMail's SMTP server to send the messages. Upon trying a simple test script, I get the following error:
unable to set sender to [[email protected]]
There is nothing wrong with the address, and this site suggests that if there's nothing incorrectly formatted about the address, then it's a server connectivity issue. But how do I troubleshoot a connectivity problem in this situation? Or is there something else I'm doing wrong? Or is there another, easier way to get a PHP script to successfully send mail to GMail?
My code is as follows (email address and password changed, obviously)
$from = "[email protected]";
$to = "[email protected]";
$subject = "Test";
$crlf = "\n";
$text = 'Text message';
$html = '<html><body>HTML message</body></html>';
$headers = array (
'From' => $from,
'Return-Path' => $from,
'Subject' => $subject
);
$mime = new Mail_mime($crlf);
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$body = $mime->get();
$headers = $mime->headers($headers);
$smtp = Mail::factory(
'smtp',
array (
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'auth' => true,
'username' => "[email protected]",
'password' => "password",
'debug' => true
)
);
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
echo 'FAILURE';
} else {
echo 'SUCCESS';
}