phpMailer附件不起作用

时间:2012-05-13 05:41:19

标签: php attachment phpmailer contact contact-form

这是我的第一篇文章,我希望能在phpMailer联系表单中添加附件字段方面获得一些帮助。我已经在html网站上添加了一个上传者[浏览]栏,但我不知道如何将它与phpmailer链接。我是否必须在phpmailer.php包中声明它或用它做一些事情?

我真的很感激一些帮助。以下是我的绝望尝试。

代码片段:

<?
$body = ob_get_contents();

$to = 'xxx@xxxx.com>';
$email = $email;
$subject = $subject;
$fromaddress = "xxx@xxxx.com";
$fromname = "Online Contact";

require("phpmailer.php"); //<<this is the original pack

$mail = new PHPMailer();

$mail->From     = "xxx@xxxx.com";
$mail->FromName = "My Name";
$mail->AddBCC("xxx@xxxx.com","Name 1");
$mail->AddAddress( $email,"Name 2");

$mail->WordWrap = 50;
$mail->IsHTML(true);

$mail->Subject  =  $subject;
$mail->Body     =  $body;
$mail->AltBody  =  "This is the text-only body";
// attachment
$mail->AddAttachment('uploadbar', 'new_file.pdf'); // the name of my html uploader is uploadbar, clicking browse to locate a file

if(!$mail->Send()) {
    $recipient = 'xxx@xxxx.com';
    $subject = 'Contact form failed';
    $content = $body;   

  mail($recipient, $subject, $content, "From: xxx@xxxx.com\r\nReply-To: $email\r\nX-Mailer: DT_formmail");
  exit;
}
?>

3 个答案:

答案 0 :(得分:7)

遇到同样的问题,试试这个

$mail->AddAttachment($_SERVER['DOCUMENT_ROOT'].'/file-name.pdf');

答案 1 :(得分:2)

首先,请确保您的上传表单与enctype='multipart/form-data'中的<form method=post action=file.php enctype='multipart/form-data'>一样。

然后,在phpmailer处理程序文件中使用此代码

foreach(array_keys($_FILES['files']['name']) as $key) {
   $source = $_FILES['files']['tmp_name'][$key];
   $filename = $_FILES['files']['name'][$key];

   $mail->AddAttachment($source, $filename);
}

答案 2 :(得分:0)

确保附件文件名不应包含任何特殊字符,并检查文件路径是否正确。

  

例如:[file name.pdf]应该像[filename.pdf]