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

时间:2016-02-04 17:25:15

标签: email phpmailer attachment email-attachments

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

<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>

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

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,但是没有附加文件。而不是文件我收到了一个长不清楚的文本和一些关于页眉和页脚附加文件的信息,如下所示:

  

边界= “b1_2d997b3e49a2cbf59277c329683b668e”   内容传输编码:8位

     

这是MIME格式的多部分邮件。

     

- b1_2d997b3e49a2cbf59277c329683b668e Content-Type:text / plain;字符集= US-ASCII

标题中的

  

- b1_2d997b3e49a2cbf59277c329683b668e Content-Type:image / jpeg; name =“apple.jpg”Content-Transfer-Encoding:base64   内容 - 处理:附件;文件名= apple.jpg

     

/ 9J / 4AAQSkZJRgABAQEASABIAAD / 4gxYSUNDX1BST0ZJTEUAAQEAAAxITGlubwIQAABtbnRyUkdC   ........(数百行像这样).............. JAkwgptNz // Z

     

- b1_2d997b3e49a2cbf59277c329683b668e -

页脚

我的PHP版本:5.5

1 个答案:

答案 0 :(得分:0)

<强>解决!!!

问题在于使用PHPMailer!

PHPMailer类无法正确创建标头。我使用 this post创建标题,并且每件事都没问题!

谢谢@synchro