使用 PHPMailer 在电子邮件中加密附件

encrypted attached file in email using PHPMailer

我为我的客户创建了一个表单来提交他们的评论:

<form role="form" action="comment.php" method="post" class="col-md-6 col-md-offset-3" enctype="multipart/form-data">
    <div>
        <div class="form-group">
            <label>Email<span style="color:gray;"> (optional)</span></label>
                <input type="email" name="email" class="form-control">
        </div>

        <div class="form-group">
            <label>Description<span style="color:red;"> *</span></label>
            <textarea class="form-control" name="description" rows="3" required></textarea>
        </div>

        <div class="form-group">
            <label>Attach your file</label>
            <input type="file" name="file">
        </div>

        <div style="margin-top: 25px; direction: rtl;">
            <div class="form-group">
                <button type="submit" name="submit" value="yes" class="btn btn-info">Send Comment</button>
            </div>
        </div>
    </div>
</form>

我在 comment.php 中使用 PHPMailer 发送电子邮件:

if($g_ok == 1){
    $email = new PHPMailer();
    $email->From      = 'noreply@ideanetwork.co';
    $email->FromName  = 'Idea Network Ticketing';
    $email->Subject   = 'New comment from ticketing portal';
    $email->Body      = $bodytext;
    $email->AddAddress( 'submitcomments@ideanetwork.co' );
    $email->WordWrap = 70;

    if (isset($_FILES['file']) && $_FILES['file']['error'] == UPLOAD_ERR_OK) {
        $info = pathinfo($_FILES['file']['name']);
        $ext = $info['extension'];
        if ($ext == "php" or $ext == "exe" or $ext == "msi"){
            header("location:../php/accessdenied.php");
            exit;
        }
        else{
            if (filesize($_FILES['file']['tmp_name']) > 4194304){
                $filenote .= "Maximum file size must be 4 MB.";
            }
            else{
                $finfo = finfo_open(FILEINFO_MIME_TYPE);
                $mime_type = finfo_file($finfo, $_FILES['file']['tmp_name']);
                $email->AddAttachment($_FILES['file']['tmp_name'], $_FILES['file']['name'], $encoding = 'base64', $mime_type);
            }
        }
    }
    if(!$email->Send()){
        $filenote .= "<script>alert('Mailer Error: " . $email->ErrorInfo."')</script>";
    }
    else{
        $ok = 1;
    }
}

我也试过这个代码:

$email->AddAttachment($_FILES['file']['tmp_name'], $_FILES['file']['name']);

当我测试我的表格时,除附件外,其他一切都正常!我的电子邮件大小是正确的,例如当我附加一个大小为 150KB 的图像时,我收到的电子邮件大小也是 150KB,但没有任何附件。而不是文件,我收到了一段很长的不清楚的文本和一些关于 header 中附加文件的信息和这样的页脚:

boundary="b1_2d997b3e49a2cbf59277c329683b668e" Content-Transfer-Encoding: 8bit

This is a multi-part message in MIME format.

--b1_2d997b3e49a2cbf59277c329683b668e Content-Type: text/plain; charset=us-ascii

在 header 和:

--b1_2d997b3e49a2cbf59277c329683b668e Content-Type: image/jpeg; name="apple.jpg" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename=apple.jpg

/9j/4AAQSkZJRgABAQEASABIAAD/4gxYSUNDX1BST0ZJTEUAAQEAAAxITGlubwIQAABtbnRyUkdC ........(hundreds of lines like this).............. JAkwgptNz//Z

--b1_2d997b3e49a2cbf59277c329683b668e--

在页脚中

我的PHP版本:5.5

已解决!!!

使用 PHPMailer 时出现问题!

PHPMailer 类 无法正确创建 headers。我使用 this post 创建了 headers,一切正常!

感谢@synchro