PEAR Mail, "unable to set sender"
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 [username@gmail.com]
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 = "username@gmail.com";
$to = "username@gmail.com";
$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' => "username@gmail.com",
'password' => "password",
'debug' => true
)
);
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
echo 'FAILURE';
} else {
echo 'SUCCESS';
}
I can't imagine why a simple username@gmail.com address wouldn't work, so it's probably another error masquerading as that. Make sure you're not accidentally suppressing error messages (put a call to error_reporting(E_ALL) before you call send).
One thing to keep in mind, though, is that you do not need to use GMail's SMTP server to send mail to GMail addresses. You can use one provided with your hosting to the same effect.
精彩评论