1

这是我的第一篇文章,我希望能在我的 phpMailer 联系表单中添加附件字段方面获得一些帮助。我已经在 html 网站中添加了一个上传器 [browse] 栏,但我不知道如何将它与 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;
}
?>
4

3 回答 3

8

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

$mail->AddAttachment($_SERVER['DOCUMENT_ROOT'].'/file-name.pdf');
于 2013-05-10T13:24:12.487 回答
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);
}
于 2012-05-13T05:59:25.313 回答
0

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

例如:[文件名.pdf] 应该类似于 [文件名.pdf]

于 2017-04-24T02:50:41.040 回答