我的代码有什么问题?
不带附件的电子邮件有效,但带附件的电子邮件无效:(
我尝试了百万选项,但仍然不起作用。我认为标头有问题,无法通过附件发送。
有人可以帮我解决这个问题吗?我已经损失了几个小时,不幸的是我自己无法解决这个问题。
<?php
if($_POST)
{
$to_email = "[email protected]";
$from_email = "[email protected]";
$subject = "subject";
$cnc = $_POST["cnc_wheels"];
$pain = $_POST["new_paint_wheels"];
$message_body = "email data";
$file_attached = false;
if(isset($_FILES["font_img_wheels"])) {
$file_tmp_name = $_FILES['font_img_wheels']['tmp_name'];
$file_name = $_FILES['font_img_wheels']['name'];
$file_size = $_FILES['font_img_wheels']['size'];
$file_type = $_FILES['font_img_wheels']['type'];
$file_error = $_FILES['font_img_wheels']['error'];
if($file_error>0) {
$mymsg = array(
1=>"The uploaded file exceeds the upload_max_filesize directive in php.ini",
2=>"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
3=>"The uploaded file was only partially uploaded",
4=>"No file was uploaded",
6=>"Missing a temporary folder" );
$output = json_encode(array('type'=>'error', 'text' => $mymsg[$file_error]));
die($output);
}
$handle = fopen($file_tmp_name, "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content));
$file_attached = true;
}
if($file_attached) {
# Mail headers should work with most clients
$headers = "MIME-Version: 1.0\r\n";
$headers = "X-Mailer: PHP/" . phpversion()."\r\n";
$headers .= "From: ".$from_email."\r\n";
$headers .= "Subject: ".$subject."\r\n";
$headers .= "Reply-To: ".$_POST["email_client"]."" . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=".md5('boundary1')."\r\n\r\n";
$headers .= "--".md5('boundary1')."\r\n";
$headers .= "Content-Type: multipart/alternative; boundary=".md5('boundary2')."\r\n\r\n";
$headers .= "--".md5('boundary2')."\r\n";
$headers .= "Content-Type: text/plain; charset=utf-8\r\n\r\n";
$headers .= $message_body."\r\n\r\n";
$headers .= "--".md5('boundary2')."--\r\n";
$headers .= "--".md5('boundary1')."\r\n";
$headers .= "Content-Type: ".$file_type."; ";
$headers .= "name=\"".$file_name."\"\r\n";
$headers .= "Content-Transfer-Encoding:base64\r\n";
$headers .= "Content-Disposition:attachment; ";
$headers .= "filename=\"".$file_name."\"\r\n";
$headers .= "X-Attachment-Id:".rand(1000,9000)."\r\n\r\n";
$headers .= $encoded_content."\r\n";
$headers .= "--".md5('boundary1')."--";
}else{
$headers = 'From: '.$_POST["email_client"].'' . "\r\n" .
'Reply-To: '.$_POST["email_client"].'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
}
$send_mail = mail($to_email, $subject, $message_body, $headers);
if(!$send_mail) {
$output = json_encode(array('type'=>'error', 'text' => 'Wiadomość nie została wysłana.'));
die($output);
}else {
$output = json_encode(array('type'=>'message', 'text' => 'Dziękujemy, za zapytanie. Skontaktujemy się niebawem.'));
die($output);
}
}
我期待有人可以帮助我。
我对您的
if($file_attached)
块做了一些小调整。我添加了内联评论来描述更改。
if($file_attached) {
// initialize boundary variables
$boundary1 = md5('boundary1');
$boundary2 = md5('boundary2');
$headers = "MIME-Version: 1.0\r\n";
$headers .= "X-Mailer: PHP/" . phpversion()."\r\n"; // changed from = to .=
$headers .= "From: ".$from_email."\r\n";
$headers .= "Subject: ".$subject."\r\n";
$headers .= "Reply-To: ".$_POST["email_client"]."\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=".$boundary1."\r\n\r\n";
// use the $body variable to construct the email content
// the attachment will now be included in the body of the email
$body = "--".$boundary1."\r\n";
$body .= "Content-Type: multipart/alternative; boundary=".$boundary2."\r\n\r\n";
$body .= "--".$boundary2."\r\n";
$body .= "Content-Type: text/plain; charset=utf-8\r\n\r\n";
$body .= $message_body."\r\n\r\n";
$body .= "--".$boundary2."--\r\n";
$body .= "--".$boundary1."\r\n";
$body .= "Content-Type: ".$file_type."; name=\"".$file_name."\"\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n";
$body .= "Content-Disposition: attachment; filename=\"".$file_name."\"\r\n";
$body .= "X-Attachment-Id:".rand(1000,9000)."\r\n\r\n";
$body .= $encoded_content."\r\n";
$body .= "--".$boundary1."--";
// adjusted to match $body and send email within this block
$send_mail = mail($to_email, $subject, $body, $headers);
}
通过电子邮件发送或接收文件也可能受到限制,具体取决于附件的大小。
如果是这种情况,在您的
php.ini
文件中,您应该调整以下内容以满足您的需求:
upload_max_filesize
- 指定上传文件的最大大小。它应该设置为大于您的附件的值post_max_size
- 限制 POST 数据的大小。该值必须大于upload_max_filesize
memory_limit
- 设置允许脚本分配的最大内存量。它应该大于您正在处理的文件大小