Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PEAR Mail, "unable to set sender"

Tags:

email

gmail

pear

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';
}
like image 267
Phantom Watson Avatar asked Nov 06 '22 11:11

Phantom Watson


1 Answers

I can't imagine why a simple [email protected] 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.

like image 186
Mike Caron Avatar answered Nov 15 '22 07:11

Mike Caron