ถ้าใช้พวก phpMailer มันง่ายกว่าไงครับ และรองรับการใช้งานได้หลากหลายกว่า
อาจจะยุ่งตอนเริ่มต้น แลัตอน set ค่าบางอย่าง แต่ถ้าเริ่มได้แล้วที่เหลือง่ายมากครับ
ส่วนเรื่องส่งเมลล์แนบไฟล์ด้วย function mail ของ php
เอาตัวอย่างของผมไปศึกษาดูครับ
**ผมข้ามหน้า form ไปนะครับ เป็นตัวที่ผมทำให้ลูกค้า เอามาแบ่งปันครับ**
**ในหน้า form ส่งเมลล์ ส่วนของไฟล์ จะให้เลือกได้ว่าจะเอาไฟล์อะไรส่งไปบ้าง (code มันเลยยุ่งๆหน่อย)**
<?
$detail = "
<html>
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-874\">
<title>".$SubJect."</title>
</head>
<body>".$MailDetail."</body>
</html>
";
$email_from = $SendFrom; // Who the email is from
$email_subject = $subject; // The Subject of the email
$email_message = $detail;// Message that the email has in it
$email_to = $SendTo; // Who the email is too
$headers = "From: ".$email_from;
if(trim($SendCc) != ""){
$headers .= "\nCc: ".$SendCc;
}
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"windows-874\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_message . "\n\n";
/********************************************** Attach File Start ********************************************/
$ChkAttnID = 0;
for($i = 0; $i < $CnAttFile; $i++){
if($AttFileCheck[$i] == "Y" and is_file($AttFilePath[$i])){
$ChkAttnID = $i;
}
}
for($i = 0; $i < $CnAttFile; $i++){
if($AttFileCheck[$i] == "Y" and is_file($AttFilePath[$i])){
$fileatt_type = $AttFileType[$i]; // ประเภทพไฟล์ เช่น pdf จะได้เป็น application/pdf
$AttachFile = $AttFilePath[$i]; // ตำแหน่งที่เก็บไฟล์บน server
$AttachFileName = $AttFileName[$i]; // ชื่อไฟล์ที่จะให้แสดงตอนส่งเมลล์ไป
$fileatt_name = $AttachFileName;
$file = fopen($AttachFile,'rb');
$data = fread($file,filesize($AttachFile));
fclose($file);
$data = chunk_split(base64_encode($data));
if($ChkAttnID == $i){
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
}else{
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}\n";
}
}
}
/********************************************** Attach File End ********************************************/
$ok = mail($email_to, $email_subject, $email_message, $headers);
?>