Search code examples
phpemailsmtppear

PEAR::Mail 'failed to add recipient'


Here's the failing code:

require_once "Mail.php";

$host           = "mail.domain.com";
$username       = "USER";
$password       = "PASS";

$mailto     = $name . ' <' . $email . '>';
$subject        = 'My Subject';
$from           = 'Client Name <[email protected]>';

$headers = array (
    'From' => $from,
    'To' => $mailto,
    'Subject' => $subject
);

$smtp = Mail::factory('smtp', array (
        'host' => $host,
        'port' => '25',
        'auth' => true,
        'username' => $username,
        'password' => $password
    )
);

$mail = $smtp->send($mailto, $headers, $msg);

if (PEAR::isError($mail)) {    
    echo '<br /><br />' . $mail->getMessage();
} else {
    echo '<br /><br />Worked';
}

I am passing the $email value to the script as follows:

$name = 'Joe Schmoe';
$email = '[email protected]';
include ($_SERVER['DOCUMENT_ROOT'] . '/path/to/my/code/above.php');

When I run the script I get the following error:

Failed to add recipient: [email protected]@localhost
[SMTP: Invalid response code received from server
(code: 504, response: 5.5.2 :
    Recipient address rejected: need fully-qualified address)]

If I change the $mailto variable from:

$mailto     = $name . ' <' . $email . '>';

... to the following:

$mailto     = $name . ' <[email protected]>';

It works perfectly.

Can anyone tell me what I'm missing?


Solution

  • I think @localhost is added to your $email variable in Mail.php

    Try changing the name of the $email variable in your script.

    Also you can use die($email); in your script to test the value of email in various stages.