php - 带附件文件的邮件 - 邮件已加密

时间:2017-05-01 21:06:07

标签: php email chunks

我写了这段代码,用于从html格式发送带有附件的电子邮件:

<?php

if($_POST && isset($_FILES['file-upload'])){

    $from             = $_POST["email"]; 
    $rubinetteria     = 'info@domain.com'; 

    $nome             = filter_var($_POST["nome"], FILTER_SANITIZE_STRING); 
    $email            = filter_var($_POST["email"], FILTER_SANITIZE_STRING); 
    $oggetto          = filter_var($_POST["oggetto"], FILTER_SANITIZE_STRING); 
    $compagnia        = filter_var($_POST["company"], FILTER_SANITIZE_STRING);
    $ruolo            = filter_var($_POST["ruolo"], FILTER_SANITIZE_STRING);
    $messaggio        = filter_var($_POST["messaggio"], FILTER_SANITIZE_STRING); 

    $fileTmpName     = $_FILES['file-upload']['uploads'];
    $fileName        = $_FILES['file-upload']['name'];
    $fileSize        = $_FILES['file-upload']['size'];
    $fileType        = $_FILES['file-upload']['type'];
    $fileError       = $_FILES['file-upload']['error'];




    $handle = fopen($fileTmpName, "r");
    $content = fread($handle, $fileSize);
    fclose($handle);
    $encoded_content = chunk_split(base64_encode($content));

        $boundary = md5("sanwebe");

        $headers = "MIME-Version: 1.0\r\n";
        $headers .= "From:".$compagnia."\r\n";
        $headers .= "Reply-To: ".$reply_to_email."" . "\r\n";
        $headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";


        $body = "--$boundary\r\n";
        $body .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
        $body .= "Content-Transfer-Encoding: base64\r\n\r\n";
        $body .= "<b>Azienda: </b>" .$compagnia. "<br>";
        $body .= "<b>Nome e Cognome: </b>" .$nome. "<br>";
        $body .= "<b>Ruolo: </b>" .$ruolo. "<br>";
        $body .= "<br>". chunk_split(base64_encode($messaggio));


        $body .= "--$boundary\r\n";
        $body .="Content-Type: $fileType; name=".$fileName."\r\n";
        $body .="Content-Disposition: attachment; filename=".$fileName."\r\n";
        $body .="Content-Transfer-Encoding: base64\r\n";
        $body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
        $body .= $encoded_content;

    $sentMail = @mail($rubinetteria, $oggetto, $body, $headers);

}


?>

问题是,当我收到电子邮件时,某些邮件会被加密。 我猜问题就是这行代码:

$body .= "<br>". chunk_split(base64_encode($messaggio));

我尝试用这个来订阅这一行:

$body .= "<br>".$messaggio;

但在这种情况下,邮件未加密,但没有附件文件(说明确认附件实际已处理但未收到)

1 个答案:

答案 0 :(得分:2)

您有两个问题

<强> 1)

 $fileTmpName     = $_FILES['file-upload']['uploads'];

这是不正确的,您引用的$_FILES值应该是上传文件的临时位置,不是[uploads]而是[tmp_name],因此请将以上代码替换为:< / p>

 $fileTmpName     = $_FILES['file-upload']['tmp_name'];

<强> 2)

第二个问题是您为PHP提供的文件大小$fileSize而不是完整文件,因此附件被截断,因此显示不正确 (出现“加密”)

我注意到PHP在计算文件大小方面存在很多问题[可能在某些平台上,我不知道]。在这种情况下,似乎给fread函数的值与实际完整文件大小的字节数不同,因此mail函数仅加载~99%的附件,这意味着文件不完整,因此看起来会出现乱码(“加密”)。

作为stated on the manual page

  

如果您只想将文件的内容转换为字符串,请使用file_get_contents(),因为它具有比fread()更好的性能。

所以请将$handle = fopen($fileTmpName, "r"); $content = fread($handle, $fileSize);替换为:

 $content = file_get_contents($fileTmpName); 

这将捕获整个文件大小,而不是$fileSize定义的仲裁金额。
您现在应该在电子邮件中上传完整的文件。

<强>加成:

正如nogad所述,您真的应该使用Swift MailerPHPMailer进行查找,因为mail函数充满了类似这里的小问题。