|
|
|
ขอความช่วยเหลือสร้าง Form ส่งไป Email มีแนบไฟล์ แต่ไม่มีไฟล์แนบไปด้วยครับ |
|
|
|
|
|
|
|
ตอนนี้ ข้อมูลที่เป็น text ส่งไปที่ email ได้หมดแล้วครับ ขาดแต่ ไฟล์ที่ต้องการแนบเป็น รูปภาพ ครับ
หน้าใจส่วน form
Code (PHP)
<html>
<title>Contact From</title>
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="javascript" src="calendar/calendar.js"></script>
</head>
<body >
<center>
<h2>INFORMATION CONTACT</h2><br />
<form name="contactform" method="POST" target="_blank" action="send_form_email.php" enctype="multipart/form-data">
<table width="850" border="0">
<tr>
<td valign="top" align="right" width="200">Upload Picture : </td>
<td class="comment"><input name="fileAttach" type="file" /><br />Maximum 1 mb. Only jpeg , gif , png , pjpeg</td>
</div>
<td width="350" valign="middle" align="center">
<label for="position">Position </label>
</td>
</tr>
<tr>
<td valign="top" valign="top" align="right" >
<label for="name">Name * : </label>
</td>
<td valign="top">
<input type="text" name="name" maxlength="50" size="30" />
</td>
<!-- Position-->
<td rowspan="10" valign="top">
<input type="checkbox" name="position[]" value="Goalkeeper (GK)"> Goalkeeper (GK)
<hr />
<input type="checkbox" name="position[]" value="Centre-back (CB)"> Centre-back (CB)<br />
<input type="checkbox" name="position[]" value="Right-back (RB)"> Right-back (RB)<br />
<input type="checkbox" name="position[]" value="left-back (LB)"> Left-back (LB)<br />
<input type="checkbox" name="position[]" value="Right Wingbacks (RWB) "> Right Wingbacks (RWB) <br />
<input type="checkbox" name="position[]" value="Left Wingbacks (LWB)"> Left Wingbacks (LWB) <br />
<hr />
<input type="checkbox" name="position[]" value="Defensive midfielder (DM)"> Defensive midfielder (DM) <br />
<input type="checkbox" name="position[]" value="Centre midfielder (CM) "> Centre midfielder (CM) <br />
<input type="checkbox" name="position[]" value="Attacking midfielder (AM)"> Attacking midfielder (AM) <br />
<input type="checkbox" name="position[]" value="Left Wide midfielder (LM)"> Left Wide midfielder (LM) <br />
<input type="checkbox" name="position[]" value="Right Wide Midfielder (RM)"> Right Wide Midfielder (RM)<br />
<hr />
<input type="checkbox" name="position[]" value="Striker (ST)"> Striker (ST)<br />
<input type="checkbox" name="position[]" value="Secondary striker (SS)"> Secondary striker (SS)<br />
<input type="checkbox" name="position[]" value="Centre Forward (CF)"> Centre Forward (CF) <br />
<input type="checkbox" name="position[]" value="Right Forwards (RF)"> Right Forwards (RF)<br />
<input type="checkbox" name="position[]" value="Left Forward (LF)"> Left Forward (LF)<br />
<input type="checkbox" name="position[]" value="Right Winger (RW)"> Right Winger (RW)<br />
<input type="checkbox" name="position[]" value="Left Winger (LW)"> Left Winger (LW)<br />
<hr />
</td>
</tr>
<tr>
<td valign="top" align="right">
<label for="surname">Surname * : </label>
</td>
<td valign="top">
<input type="text" name="surname" maxlength="50" size="30" />
</td>
</tr>
<tr>
<td valign="top" align="right">
<label for="date">Date of Birth * : </label>
</td>
<td valign="top"> <?php
require_once('calendar/classes/tc_calendar.php');
$myCalendar1 = new tc_calendar("dateBirth", true, false);
$myCalendar1->setIcon("calendar/images/iconCalendar.gif");
$myCalendar1->setDate(date('d'), date('m'), date('Y'));
$myCalendar1->setPath("calendar/");
$myCalendar1->setYearInterval(1913, 2015);
$myCalendar1->dateAllow('1916-01-01', '2015-12-01');
$myCalendar1->setDateFormat('j F Y');
$myCalendar1->setAlignment('left', 'bottom');
$myCalendar1->writeScript();
?>
</td></tr>
<tr>
<td valign="top" align="right">
<label for="nationality">Nationality * : </label>
</td>
<td valign="top">
<input type="text" name="nationality" maxlength="80" size="30" />
</td>
</tr>
<tr>
<td valign="top" align="right">
<label for="passport">Passport No. * : </label>
</td>
<td valign="top">
<input type="text" name="passport" maxlength="80" size="30" />
</td>
</tr>
<tr><td valign="top" align="right"><label for="VISA">VISA Type : </label></td>
<td>
<input type="radio" name="visatype" value="No VISA" checked />No VISA <br />
<input type="radio" name="visatype" value="Transit-TS" />Transit-TS <br />
<input type="radio" name="visatype" value="Tourist-TR"/>Tourist-TR <br />
<input type="radio" name="visatype" value="Non-Immigrant"/>Non-Immigrant<br />
<input type="radio" name="visatype" value="Other"/>Other
</td></tr>
<tr>
<td valign="top" align="right">
<label for="date">VISA Expired Date : </label>
</td>
<td valign="top"> <?php
require_once('calendar/classes/tc_calendar.php');
$myCalendar1 = new tc_calendar("dateVisaExpire", true, false);
$myCalendar1->setIcon("calendar/images/iconCalendar.gif");
$myCalendar1->setDate(date('d'), date('m'), date('Y'));
$myCalendar1->setPath("calendar/");
$myCalendar1->setYearInterval(1913, 2015);
$myCalendar1->dateAllow('1916-01-01', '2015-12-01');
$myCalendar1->setDateFormat('j F Y');
$myCalendar1->setAlignment('left', 'bottom');
$myCalendar1->writeScript();
?>
</td></tr>
<tr><td valign="top" align="right"><label for="Thai Work Permit ">Thai Work Permit : </label></td>
<td>
<input type="radio" name="thwork" value="Have" checked />Have <br />
<input type="radio" name="thwork" value="No Have" />No Have <br />
</td></tr>
<tr>
<td valign="top" align="right">
<label for="Language">Language : </label>
</td>
<td valign="top">
<input type="text" name="language" maxlength="80" size="30" />
</td>
<tr>
<td valign="top" align="right">
<label for="Height">Height : </label>
</td>
<td valign="top">
<input type="text" name="height" maxlength="3" size="4" /> Cm.
</td>
<tr>
<td valign="top" align="right">
<label for="Weight">Weight : </label>
</td>
<td valign="top">
<input type="text" name="weight" maxlength="3" size="4" /> Kg.
</td>
<tr>
<td valign="top" align="right">
<label for="email">Email Address * :</label>
</td>
<td valign="top">
<input type="text" name="email" maxlength="30" size="30" />
</td>
</tr>
<tr>
<td valign="top" align="right">
<label for="telephone">Telephone Number :</label>
</td>
<td valign="top">
<input type="text" name="telephone" maxlength="10" size="10" />
</td>
</tr>
<tr>
<td valign="top" align="right">
<label for="club career">Club Career :</label>
</td>
<td valign="top">
<textarea name="clubcareer" maxlength="1000" cols="30" rows="6"></textarea>
</td>
<!--
</tr>
<td valign="top" align="right">
<label for="position">Position : </label>
</td>
<td valign="top">
<input type="checkbox" name="position[]" value="Goalkeeper (GK)"> Goalkeeper (GK)
<hr />
<input type="checkbox" name="position[]" value="Centre-back (CB)"> Centre-back (CB)<br />
<input type="checkbox" name="position[]" value="Right-back (RB)"> Right-back (RB)<br />
<input type="checkbox" name="position[]" value="left-back (LB)"> Left-back (LB)<br />
<input type="checkbox" name="position[]" value="Right Wingbacks (RWB) "> Right Wingbacks (RWB) <br />
<input type="checkbox" name="position[]" value="Left Wingbacks (LWB)"> Left Wingbacks (LWB) <br />
<hr />
<input type="checkbox" name="position[]" value="Defensive midfielder (DM)"> Defensive midfielder (DM) <br />
<input type="checkbox" name="position[]" value="Centre midfielder (CM) "> Centre midfielder (CM) <br />
<input type="checkbox" name="position[]" value="Attacking midfielder (AM)"> Attacking midfielder (AM) <br />
<input type="checkbox" name="position[]" value="Left Wide midfielder (LM)"> Left Wide midfielder (LM) <br />
<input type="checkbox" name="position[]" value="Right Wide Midfielder (RM)"> Right Wide Midfielder (RM)<br />
<hr />
<input type="checkbox" name="position[]" value="Striker (ST)"> Striker (ST)<br />
<input type="checkbox" name="position[]" value="Secondary striker (SS)"> Secondary striker (SS)<br />
<input type="checkbox" name="position[]" value="Centre Forward (CF)"> Centre Forward (CF) <br />
<input type="checkbox" name="position[]" value="Right Forwards (RF)"> Right Forwards (RF)<br />
<input type="checkbox" name="position[]" value="Left Forward (LF)"> Left Forward (LF)<br />
<input type="checkbox" name="position[]" value="Right Winger (RW)"> Right Winger (RW)<br />
<input type="checkbox" name="position[]" value="Left Winger (LW)"> Left Winger (LW)<br />
<hr />
</td>
</tr>
-->
<tr>
<td valign="top" align="right">
<label for="strength">Strength :</label>
</td>
<td valign="top">
<textarea name="strength" maxlength="1000" cols="30" rows="6"></textarea>
</td>
</tr>
<tr>
<td valign="top" align="right">
<label for="contact">Contact :</label>
</td>
<td valign="top">
<textarea name="contact" maxlength="1000" cols="30" rows="6"></textarea>
</td>
</tr>
<tr>
<td valign="top" align="right">
<label for="salary">Salary Expect :</label>
</td>
<td valign="top">
<input type="text" name="salary" maxlength="12" size="12" />
</td>
</tr>
<tr><td> </td>
<td>
<input type="submit"value="Confirm" />
<input type="reset" value="Reset" />
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
หน้าในส่วน action ไป
Code (PHP)
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "[email protected],";
$email_subject = "New member profile";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['surname']) ||
!isset($_POST['dateBirth']) ||
!isset($_POST['nationality']) ||
!isset($_POST['passport']) ||
!isset($_POST['email']) ||
!isset($_POST['position'])) {
died('Please input (*) filed.');
}
$name = $_POST['name']; // required
$surname = $_POST['surname']; // required
$dateBirth = $_POST['dateBirth'];
$nationality = $_POST['nationality'];
$passport = $_POST['passport'];
$visatype = $_POST['visatype'];
$dateVisaExpire = $_POST['dateVisaExpire'];
$thwork = $_POST['thwork'];
$language = $_POST['language'];
$height = $_POST['height'];
$weight = $_POST['weight'];
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$clubcareer = $_POST['clubcareer']; // required
$position = $_POST['position'];
$strength = $_POST['strength'];
$contact = $_POST['contact'];
$salary = $_POST['salary'];
$error_message = "";
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Surname: ".clean_string($surname)."\n";
$email_message .= "dateBirth of birth: ".clean_string($dateBirth)."\n";
$email_message .= "Nationality: ".clean_string($nationality)."\n";
$email_message .= "Passport: ".clean_string($passport)."\n";
$email_message .= "Visa Type: ".clean_string($visatype)."\n";
$email_message .= "Visa Expire: ".clean_string($dateVisaExpire)."\n";
$email_message .= "Thai work permit: ".clean_string($thwork)."\n";
$email_message .= "Language: ".clean_string($language)."\n";
$email_message .= "Height: ".clean_string($height)."\n";
$email_message .= "Weight: ".clean_string($weight)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Club Career: ".clean_string($clubcareer)."\n";
$email_message .= "Position: ".clean_string($position)."\n";
$email_message .= "Strength: ".clean_string($strength)."\n";
$email_message .= "Contact: ".clean_string($contact)."\n";
$email_message .= "Salary: ".clean_string($salary)."\n";
//*** Uniqid Session ***//
$strSid = md5(uniqid(time()));
$header = "";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_to."\r\n" .
'X-Mailer: PHP/' . phpversion();
$header .= "MIME-Version: 1.0\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n\n";
$header .= "This is a multi-part message in MIME format.\n";
$header .= "--".$strSid."\n";
$header .= "Content-type: text/html; charset=windows-874\n"; // or UTF-8 //
$header .= "Content-Transfer-Encoding: 7bit\n\n";
$header .= $email_message."\n\n";
//*** Attachment ***//
if($_FILES["fileAttach"]["name"] != "")
{
$strFilesName = $_FILES["fileAttach"]["name"];
$strContent = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"])));
$header .= "--".$strSid."\n";
$header .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\n";
$header .= "Content-Transfer-Encoding: base64\n";
$header .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\n\n";
$header .= $strContent."\n\n";
}
mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
<html>
<head><meta HTTP-EQUIV="Refresh" CONTENT="5;URL=http://www.Website.com"></head>
<body>
You can wait in 5 second to Home page <br />
Thank you for contacting us.
<form >
<input type="button" alt="back" value="Return to Form" onclick="location.href='form.php';"/>
</form>
</body>
</html>
<?php
}
?>
ผมดัดแปลงจาก หน้านี้ครับ
https://www.thaicreate.com/php/php-send-email-form-upload-attach-files.html
โปรแกช่วยให้คำชี้แนะแก้ไขให้กระผมด้วยครับ ปล้ำมา 2วันยังไม่ได้ครับ จนปัญญา
ขอบคุณครับ
Tag : PHP
|
|
|
|
|
|
Date :
2012-11-08 13:47:40 |
By :
mekhong |
View :
1957 |
Reply :
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ลองบน Host จริงใช่แล้วหรือเปล่าครับ ในตัวอย่างผมก็ส่งได้ปกติน่ะครับ
|
|
|
|
|
Date :
2012-11-08 14:58:49 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ลงบน host จริงแล้วครับ
ทั้งไฟล์ที่ผมแก้ และ ไฟล์ในบทเรียนครับ ไฟล์ใน บทเรียน รู้สึกว่าจะไป hotmail ได้ครับ แต่gmail กับ host ของผมไม่ไปครับ
|
|
|
|
|
Date :
2012-11-08 15:34:56 |
By :
MEKHONG |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ผมทดลองแล้ว ผลประมาณนี้ครับ
ถ้าผมแก้code แบบนี้ จะได้ไฟล์แนบแต่ไม่ได้ ข้อความ
Code (PHP)
// create email headers
$header .= "From: ".$_POST["name"]." ".$_POST["surname"]."<".$_POST["email"].">\nReply-To: ".$_POST["email"]."";
'Reply-To: '.$email_to."\r\n" .
'X-Mailer: PHP/' . phpversion();
//*** Attachment ***//
if($_FILES["fileAttach"]["name"] != "")
{
$strFilesName = $_FILES["fileAttach"]["name"];
$strContent = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"])));
$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";
}
mail($email_to, $email_subject, null, $strHeader);
?>
ถ้าผมใช้ code นี้ จะได้ข้อความ
Code (PHP)
// create email headers
$header .= "From: ".$_POST["name"]." ".$_POST["surname"]."<".$_POST["email"].">\nReply-To: ".$_POST["email"]."";
'Reply-To: '.$email_to."\r\n" .
'X-Mailer: PHP/' . phpversion();
//*** Attachment ***//
if($_FILES["fileAttach"]["name"] != "")
{
$strFilesName = $_FILES["fileAttach"]["name"];
$strContent = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"])));
$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";
}
mail($email_to, $email_subject, $email_messag, $header);
?>
พอผมเอามาประสมกัน กลับ ได้ข้อความมั่วมาเต็มเลยครับ ไม่รู้จะไปแก้ตรงไหนครับ
Code (PHP)
// create email headers
$header .= "From: ".$_POST["name"]." ".$_POST["surname"]."<".$_POST["email"].">\nReply-To: ".$_POST["email"]."";
'Reply-To: '.$email_to."\r\n" .
'X-Mailer: PHP/' . phpversion();
//*** Attachment ***//
if($_FILES["fileAttach"]["name"] != "")
{
$strFilesName = $_FILES["fileAttach"]["name"];
$strContent = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"])));
$header .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\n";
$header .= "Content-Transfer-Encoding: base64\n";
$header .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\n\n";
$header .= $strContent."\n\n";
}
mail($email_to, $email_subject, $email_messag, $header);
?>
จนปัญญาจริงๆครับ U__U
|
|
|
|
|
Date :
2012-11-08 18:02:06 |
By :
MEKHONG |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|