添加.docx,.pdf,.txt等作为PHPMailer的附件



我需要让人们可以使用PHPMailer发送他们的文档,无论是.docx.pdf还是从他们的计算机上发送任何东西。在我找到的每个解决方案中,没有一个对我有用。使用$mailer->ErrorInfoCould 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']);

提前谢谢你

您需要学习如何正确处理上传。不要直接访问$_FILES;首先使用move_uploaded_file。为了省去查找所有内容的麻烦,请改编 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;
    }
}

最新更新