使用 PHP Pear 邮件:添加收件人失败:

Using PHP Pear mail :Failed to add recipient:

我正在使用 Php Pear 邮件向填写表单的用户发送附件。如果我对 "to email" 地址进行硬编码,它就可以正常工作。但是当我使用 $to = $_POST['email'] ;我收到以下错误。

无法添加收件人:@localhost [SMTP:从服务器收到无效响应代码(代码:501,响应:<@localhost>:无本地部分)]

<?php

require_once 'Mail.php'; 
require_once 'Mail/mime.php';




$from = "email@domain.com";
$to = $_POST['email'] ; 
$subject = 'Free Diagnostic Test Coupon';

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

$text = 'Please find attached the coupon';// text and html versions of email.
$html = '<html><body>Please find attached the coupon</body>        </html>';

$file = 'img/coupon.jpg'; // attachment
$crlf = "\n";

$mime = new Mail_mime($crlf);
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addAttachment($file, 'image/jpeg');

//do not ever try to call these lines 
$host = "host";
$username = "username";
$password = "password";
in reverse order
$body = $mime->get();
$headers = $mime->headers($headers);
$smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => true,
 'username' => $username,'password' => $password));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
  echo("<p>" . $mail->getMessage() . "</p>");
}
else {
  echo("<p>Message successfully sent!</p>");
}
?>

如有任何帮助,我们将不胜感激。

通过添加代码顶部来检查您的代码。

error_reporting(E_ALL); ini_set('display_errors', '1');