【问题标题】:phpMailer Attachment not workingphpMailer 附件不起作用
【发布时间】:2012-05-13 05:41:19
【问题描述】:

这是我的第一篇文章,我希望能在我的 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;
}
?>

【问题讨论】:

    标签: php attachment phpmailer contact contact-form


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      首先,确保您的上传表单具有enctype='multipart/form-data',如&lt;form method=post action=file.php enctype='multipart/form-data'&gt;

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

      foreach(array_keys($_FILES['files']['name']) as $key) {
         $source = $_FILES['files']['tmp_name'][$key];
         $filename = $_FILES['files']['name'][$key];
      
         $mail->AddAttachment($source, $filename);
      }
      

      【讨论】:

      • 我用你所有的行替换了我的 AddAttachment 行。我仍然没有收到电子邮件中的任何附件。
      • 这里是 phpmailer.php,我没有包含它的任何代码 - 2shared.com/file/RcD8xdI1/phpmailer.html - 我的第一篇文章中包含的是 process.php
      • 在您的具体情况下,如果上传输入被命名为uploadbar,请使用$_FILES['uploadbar'] 而不是$_FILES['files']
      • 仍然无法正常工作。我不知道您是否需要更多信息来帮助您或文件?顺便谢谢你!
      【解决方案3】:

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

      例如:[file name.pdf] 应该类似于 [filename.pdf]

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-11
        • 1970-01-01
        • 1970-01-01
        • 2014-02-06
        • 2015-06-20
        • 2012-09-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多