php - 带附件的邮件 - 邮件已加密
php - mail with attachment file - message encrypted
我写了这段代码,用于以 html 格式发送带有 php 附件的电子邮件:
<?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)
$fileTmpName = $_FILES['file-upload']['uploads'];
这是不正确的,你引用的$_FILES
值应该是上传文件的临时位置,不是[uploads]
而是[tmp_name]
,所以把上面的代码换成:
$fileTmpName = $_FILES['file-upload']['tmp_name'];
2)
第二个问题是您提供给 PHP 阅读 $fileSize
的文件大小 而不是 完整文件,因此附件被截断,因此显示不正确 (出现 "encrypted")。
我注意到 PHP 计算文件大小的方式有很多问题 [可能在某些平台上,我不知道]。在这种情况下,似乎赋予 fread
函数的值与实际完整文件大小的字节数不同,因此 mail
函数仅加载附件的 ~99%,这意味着文件不完整,因此看起来是乱码 ("encrypted").
If you just want to get the contents of a file into a string, use file_get_contents() as it has much better performance [than fread()].
所以将 $handle = fopen($fileTmpName, "r");
$content = fread($handle, $fileSize);
替换为:
$content = file_get_contents($fileTmpName);
这将捕获 整个 文件大小,而不是 $fileSize
定义的任意数量。
您现在应该上传了完整的文件在您的电子邮件中。
奖金:
正如 nogad
所指出的,您确实应该使用 Swift Mailer or PHPMailer 进行查找,因为 mail
函数充满了类似此处的小问题。
我写了这段代码,用于以 html 格式发送带有 php 附件的电子邮件:
<?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)
$fileTmpName = $_FILES['file-upload']['uploads'];
这是不正确的,你引用的$_FILES
值应该是上传文件的临时位置,不是[uploads]
而是[tmp_name]
,所以把上面的代码换成:
$fileTmpName = $_FILES['file-upload']['tmp_name'];
2)
第二个问题是您提供给 PHP 阅读 $fileSize
的文件大小 而不是 完整文件,因此附件被截断,因此显示不正确 (出现 "encrypted")。
我注意到 PHP 计算文件大小的方式有很多问题 [可能在某些平台上,我不知道]。在这种情况下,似乎赋予 fread
函数的值与实际完整文件大小的字节数不同,因此 mail
函数仅加载附件的 ~99%,这意味着文件不完整,因此看起来是乱码 ("encrypted").
If you just want to get the contents of a file into a string, use file_get_contents() as it has much better performance [than fread()].
所以将 $handle = fopen($fileTmpName, "r");
$content = fread($handle, $fileSize);
替换为:
$content = file_get_contents($fileTmpName);
这将捕获 整个 文件大小,而不是 $fileSize
定义的任意数量。
您现在应该上传了完整的文件在您的电子邮件中。
奖金:
正如 nogad
所指出的,您确实应该使用 Swift Mailer or PHPMailer 进行查找,因为 mail
函数充满了类似此处的小问题。