【问题标题】:Add .docx, .pdf, .txt etc as attachment with PHPMailer使用 PHPMailer 添加 .docx、.pdf、.txt 等作为附件
【发布时间】:2015-04-29 20:05:22
【问题描述】:

我需要让人们可以使用 PHPMailer 从他们的计算机发送他们的文档,无论是.docx.pdf 还是其他任何东西。在我找到的每一个解决方案中,没有一个对我有用。使用$mailer->ErrorInfo时,错误Could not access file:不断出现。

这是我的代码:

$mailer->From = $mail1;
$mailer->FromName = $name1;
$mailer->addAddress("gmfernandes@neo-e.com.br");
$mailer->Subject = $name1;
$mailer->ContentType = "Content-type: text/html; charset=utf-8";
$mailer->msgHTML($template);
$mailer->addAttachment($_FILES['anexoTrabalho']['tmp_name'], $_FILES['anexoTrabalho']['name']);

提前谢谢你

【问题讨论】:

    标签: pdf phpmailer


    【解决方案1】:

    您需要学习如何handle uploads correctly。不要直接访问$_FILES;首先使用move_uploaded_file。为了省去您查找所有内容的麻烦,请改写the example provided with PHPMailer,我在这里复制了其中的重要部分:

    $msg = '';
    if (array_key_exists('userfile', $_FILES)) {
        // First handle the upload
        // Don't trust provided filename - same goes for MIME types
        // See http://php.net/manual/en/features.file-upload.php#114004 for more thorough upload validation
        $uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['userfile']['name']));
        if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
            // Upload handled successfully
            // Now create a message
            // This should be somewhere in your include_path
            require 'PHPMailerAutoload.php';
            $mail = new PHPMailer;
            $mail->setFrom('from@example.com', 'First Last');
            $mail->addAddress('whoto@example.com', 'John Doe');
            $mail->Subject = 'PHPMailer file sender';
            $mail->msgHTML("My message body");
            // Attach the uploaded file
            $mail->addAttachment($uploadfile, 'My uploaded file');
            if (!$mail->send()) {
                $msg = "Mailer Error: " . $mail->ErrorInfo;
            } else {
                $msg = "Message sent!";
            }
        } else {
            $msg = 'Failed to move file to ' . $uploadfile;
        }
    }
    

    【讨论】:

    • 你好。感谢您的帮助,但array_key_exists() 没有找到“anexoPrograma”。我不明白为什么。根据示例,它是 input 名称,对吧?
    • 在您发布的代码中称为anexoTrabalho
    • 是的,我用过,但还是找不到。我会继续努力的。
    • 您是否在表单标签中指定了enctype="multipart/form-data" 属性?如果没有,文件上传将无法正常工作。
    • 是的,我从一开始就指定了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多