doukui4786 2015-10-04 09:25
浏览 31
已采纳

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

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条回答 默认 最新

  • dousi8237 2015-10-04 15:54
    关注

    Check your code by adding the top of it.

    error_reporting(E_ALL); ini_set('display_errors', '1');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题