การสร้างฟร์อมส่งเมลครับ เรื่องการแนบไฟล์ มากกว่า 1 ไฟล์
Code (PHP)
<html>
<head>
<title>ThaiCreate.Com PHP Sending Email</title>
</head>
<body>
<?
$strTo = "[email protected] ";
$strSubject = "Test Send Mail";
$strMessage = "My Body & <b>My Description</b>";
//*** Uniqid Session ***//
$strSid = md5(uniqid(time()));
$strHeader = "";
$strHeader .= "From: Mr.Weerachai Nukitram<[email protected] >\nReply-To: [email protected] \n";
$strHeader .= "Cc: Mr.Surachai Sirisart<[email protected] >";
$strHeader .= "Bcc: [email protected] ";
$strHeader .= "MIME-Version: 1.0\n";
$strHeader .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n\n";
$strHeader .= "This is a multi-part message in MIME format.\n";
$strHeader .= "--".$strSid."\n";
$strHeader .= "Content-type: text/html; charset=windows-874\n"; // or UTF-8 //
$strHeader .= "Content-Transfer-Encoding: 7bit\n\n";
$strHeader .= $strMessage."\n\n";
//*** Attachment Files ***//
$arrFiles[] = "thaicreate1.txt";
$arrFiles[] = "thaicreate2.txt";
$arrFiles[] = "thaicreate3.txt";
$arrFiles[] = "thaicreate4.txt";
$arrFiles[] = "thaicreate5.txt";
for($i=0;$i<count($arrFiles);$i++)
{
if(trim($arrFiles[$i]) != "")
{
$strFilesName = $arrFiles[$i];
$strContent = chunk_split(base64_encode(file_get_contents($strFilesName)));
$strHeader .= "--".$strSid."\n";
$strHeader .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\n";
$strHeader .= "Content-Transfer-Encoding: base64\n";
$strHeader .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\n\n";
$strHeader .= $strContent."\n\n";
}
}
$flgSend = @mail($strTo,$strSubject,null,$strHeader); // @ = No Show Error //
if($flgSend)
{
echo "Email Sending.";
}
else
{
echo "Email Can Not Send.";
}
?>
</body>
</html>
Go to : PHP Sending Email Attachment Files
Date :
2010-11-25 10:01:04
By :
webmaster
รบกวนพี่วินหน่อยนะครับ ที่ผมถามมาข้างต้นผมทำได้หมดแหละครับ
ตอนนี้ปัญหาอยู่ที่ตอนส่งแล้วครับ ข้อมูลไปหมดครับ แต่ไฟล์แนบก็ไปเหมือนกันนะครับแต่ มันเปิดไม่ได้อะครับ เหมือนไฟล์มันเสียอะครับ
อันนี้เป็น code บางส่วน ช่วยดูหน่อยครับ เข้าจะตรวจสอบงานพรึ่งนี้แหละ ขอบคุณครับ
Code (PHP)
$boundary = md5(uniqid(time()));
$strHeader = "From: " . $_POST[from_email] . "\n";
$strHeader .= "Content-Type: multipart/mixed; boundary=$boundary\n";
$body = "--$boundary\n";
$body .= "Content-Type: text/plain; charset=windows-874\n";
$body .= "Content-Transfer-Encoding: 8bit\n\n";
$body .= $_POST['message'] . "\n";
$body .= "--$boundary\n";
foreach ($_SESSION['session_files_up'] as $itemnumber =>$files_up_show) {
$file_tmpname =$files_up_show['tmp_name'];// $_FILES['uploadfile']['tmp_name'];
$file = fopen($file_tmpname,"rb");
$file_content = fread($file ,filesize($file_tmpname));
fclose($file);
$b64_enc = base64_encode($file_content) . "\n";
$file_split = chunk_split($b64_enc);
$mime_type = $files_up_show['name_type'];//$_FILES['uploadfile']['type'];
$file_name = $files_up_show['name_files'];//$_FILES['uploadfile']['name'];
$body .= "Content-Type: $mime_type; name=\"$file_name\"\n";
$body .= "Content-Transfer-Encoding: base64\n";
$body .= "Content-Disposition: attachment; filename=\"$file_name\"\n\n";
$body .= "$file_split\n";
$body .= "--$boundary--";
}
$flgSend = @mail($strTo,$strSubject,$body,$strHeader); // @ = No Show Error //
if($flgSend){
echo "$id_std ถูกส่งแล้ว <br>";
}else{
echo "$id_std ไม่สามารถส่งได้ <br>";
}
ผมนำ code ที่พี่ให้มาประยุกนิดหน่อยครับ
$files_up_show['tmp_name'];
$files_up_show['name_type'];
$files_up_show['name_files'];
มาจาก session array ครับ โดย ใช้ foreach วนแทน for ของพี่อะครับ
Date :
2010-11-25 16:16:49
By :
aimoomoo
พี่วินช่วยดูหน่อยครับ นี่ code ล่าสุด
Code (PHP)
$boundary = md5(uniqid(time()));
$strHeader = "From: " . $_POST[from_email] . "\n";
$strHeader .= "Content-Type: multipart/mixed; boundary=$boundary\n";
$body = "--$boundary\n";
$body .= "Content-Type: text/plain; charset=windows-874\n";
$body .= "Content-Transfer-Encoding: 8bit\n\n";
$body .= $_POST['message'] . "\n";
foreach ($_SESSION['session_files_up'] as $itemnumber =>$files_up_show) {
$mime_type = trim($files_up_show['name_type']);//$_FILES['uploadfile']['type'];
$file_name = trim($files_up_show['name_files']);//$_FILES['uploadfile']['name'];
$file_split = chunk_split(base64_encode(file_get_contents($file_name)));
$body .= "--".$boundary."\n";
$body .= "Content-Type: application/octet-stream; name=\"".$file_name."\"\n";
$body .= "Content-Transfer-Encoding: base64\n";
$body .= "Content-Disposition: attachment; filename=\"".$file_name."\"\n\n";
$body .= $file_split."\n\n";
}
$flgSend = @mail($strTo,$strSubject,$body,$strHeader); // @ = No Show Error //
if($flgSend){
echo "$id_std ถูกส่งแล้ว <br>";
}else{
echo "$id_std ไม่สามารถส่งได้ <br>";
}
ประวัติการแก้ไข 2010-11-26 14:59:57
Date :
2010-11-26 13:37:39
By :
aimoomoo
ขนาดของไฟล์มันก็น้อยมากครับ
Date :
2010-11-26 15:19:40
By :
aimoomoo
ไม่มีใครตอบเลย อะอย่างน้อยช่วยดูหน่อยว่า error นี้น่าจะเกิดจากอะไร
Warning: fopen(C:\WINDOWS\Temp\phpA3.tmp) [function.fopen]: failed to open stream: No such file or directory in C:\AppServ\www\mail\send_mail2.php on line 44
Warning: filesize() [function.filesize]: stat failed for C:\WINDOWS\Temp\phpA3.tmp in C:\AppServ\www\mail\send_mail2.php on line 45
Warning: fread(): supplied argument is not a valid stream resource in C:\AppServ\www\mail\send_mail2.php on line 45
Warning: fclose(): supplied argument is not a valid stream resource in C:\AppServ\www\mail\send_mail2.php on line 46
Date :
2010-11-26 16:41:18
By :
aimoomoo
Load balance : Server 00