doukui4786
doukui4786
2015-10-04 09:25

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

  • pear
  • smtp
  • php
  • email

I am using Php Pear mail for sending an attachment to the user who fills out a form. If I hard code the "to email" address it works fine. But when I use $to = $_POST['email'] ; I get the following error.

Failed to add recipient: @localhost [SMTP: Invalid response code received from server (code: 501, response: <@localhost>: no local part)]

<?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 = "
";

$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>");
}
?>

Any help will be appreciated.

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答

为你推荐