使用PHP Pear邮件:无法添加收件人:

时间:2015-10-04 09:25:13

标签: php email smtp pear

我正在使用Php Pear邮件向填写表单的用户发送附件。如果我硬编码“到电子邮件”地址它工作正常。但是当我使用时 $ 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>");
}
?>

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

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

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