|
Update 04/03/2019 10:50 AM
1.) สร้างแบบฟอร์ม สำหรับกรอกข้อมูล เพื่อส่งอีเมล์ผ่านหน้าเว็บ ตั้งชื่อไฟล์เป็น form2mail.html
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>แบบฟอร์มติดต่อสอบถาม</title> </head> <body> <center><br> <b>แบบฟอร์มติดต่อสอบถาม<b></br></br> <form id="form1" name="form1" method="post" action="sendmail.php"> <table width="415" border="0" cellspacing="1" cellpadding="1"> <tr> <td colspan="2"> </td> </tr> <tr> <td>ชื่อ-นามสกุลผู้ส่ง</td> <td><input name="name" type="text"></td> </tr> <tr> <td>อีเมล์ผู้ส่ง</td> <td><input name="email" type="text"></td> </tr> <tr> <td>หัวข้อ</td> <td><input name="subject" type="text" id="subject"></td> </tr> <tr> <td valign="top">ข้อความ</td> <td><textarea name="details" cols="30" rows="5" wrap="virtual" id="details"></textarea></td> </tr> <tr> <td colspan="2"><div align="center"><input type="submit" name="Submit" value="Send Mail" /></td> </tr> </table> </form> </center> </body> </html> ![]() 2.) สร้าง script php เพื่อรับค่า input จาก formmail.html แล้วส่งข้อมูลไปให้ผู้รับตามที่กำหนด โดยตั้งชื่อไฟล์เป็น sendmail.php การส่งอีเมล์โดยใช้ SMTP ของโฮสต์ <? require("phpmailer/class.phpmailer.php"); // path to the PHPMailer class. $fm = "you@yourdomain.com"; // *** ต้องใช้อีเมล์ @yourdomain.com เท่านั้น *** $to = "support@yourdomain.com"; // อีเมล์ที่ใช้รับข้อมูลจากแบบฟอร์ม $custemail = $_POST['email']; // อีเมล์ของผู้ติดต่อที่กรอกผ่านแบบฟอร์ม $subj = $_POST['subject']; /* ------------------------------------------------------------------------------------------------------------- */ $message.= "ชื่อ-นามสกุล: ".$_POST['name']."\n"; $message.= "อีเมล์: ".$_POST['email']."\n"; $message.= "หัวข้อ: ".$_POST['subject']."\n"; $message.= "รายละเอียด: ".$_POST['details']."\n"; /* ------------------------------------------------------------------------------------------------------------- */ /* หากต้องการรับข้อมูลจาก Form แบบไม่ระบุชื่อตัวแปร สามารถรับข้อมูลได้ไม่จำกัด ให้ลบบรรทัด 11-14 แล้วใช้ 19-22 แทน foreach ($_POST as $key => $value) { //echo "Field ".htmlspecialchars($key)." is ".htmlspecialchars($value)." การส่งอีเมล์โดยใช้ SMTP ของ Gmail (ต้องมีอีเมล์ you@gmail.com) * ดูคำแนะนำเพิ่มเติมด้านล่าง - Update 04/03/2019 <? require("phpmailer/class.phpmailer.php"); // path to the PHPMailer class. $fm = "you@gmail.com"; // *** ต้องใช้อีเมล์ @gmail.com เท่านั้น *** $to = "support@yourdomain.com"; // อีเมล์ที่ใช้รับข้อมูลจากแบบฟอร์ม $custemail = $_POST['email']; // อีเมล์ของผู้ติดต่อที่กรอกผ่านแบบฟอร์ม $subj = $_POST['subject']; /* ------------------------------------------------------------------------------------------------------------- */ $message.= "ชื่อ-นามสกุล: ".$_POST['name']."\n"; $message.= "อีเมล์: ".$_POST['email']."\n"; $message.= "หัวข้อ: ".$_POST['subject']."\n"; $message.= "รายละเอียด: ".$_POST['details']."\n"; /* ------------------------------------------------------------------------------------------------------------- */ /* หากต้องการรับข้อมูลจาก Form แบบไม่ระบุชื่อตัวแปร สามารถรับข้อมูลได้ไม่จำกัด ให้ลบบรรทัด 11-14 แล้วใช้ 19-22 แทน foreach ($_POST as $key => $value) { //echo "Field ".htmlspecialchars($key)." is ".htmlspecialchars($value)." การส่งอีเมล์โดยใช้ SMTP ของ MailJet (ต้องมี Account ของ MailJet) <? require("phpmailer/class.phpmailer.php"); // path to the PHPMailer class. $fm = "you@yourdomain.com"; // *** ต้องใช้อีเมล์ ที่ลงทะเบียนไว้กับ MailJet เท่านั้น *** $to = "support@yourdomain.com"; // อีเมล์ที่ใช้รับข้อมูลจากแบบฟอร์ม $custemail = $_POST['email']; // อีเมล์ของผู้ติดต่อที่กรอกผ่านแบบฟอร์ม $subj = $_POST['subject']; /* ------------------------------------------------------------------------------------------------------------- */ $message.= "ชื่อ-นามสกุล: ".$_POST['name']."\n"; $message.= "อีเมล์: ".$_POST['email']."\n"; $message.= "หัวข้อ: ".$_POST['subject']."\n"; $message.= "รายละเอียด: ".$_POST['details']."\n"; /* ------------------------------------------------------------------------------------------------------------- */ /* หากต้องการรับข้อมูลจาก Form แบบไม่ระบุชื่อตัวแปร สามารถรับข้อมูลได้ไม่จำกัด ให้ลบบรรทัด 11-14 แล้วใช้ 19-22 แทน foreach ($_POST as $key => $value) { //echo "Field ".htmlspecialchars($key)." is ".htmlspecialchars($value)." 3.) เปลี่ยนอีเมล์ผู้รับ จาก contactus@yourdomain.com เป็น ที่อยู่อีเมล์ของท่าน จากนั้น Upload ทั้ง 2 ไฟล์ ขึ้นไปไว้บน Server 4) ทดสอบการใช้งานโดยเปิดหน้า http://www.yourdomain.com/form2mail.html กรอกข้อมูลให้ครบทุกช่อง แล้วคลิกปุ่ม "Send Mail" *** คำแนะนำเพิ่มเติม *** สำหรับผู้ใช้ SMTP ของ Gmail หากพบ Mailer Error: SMTP connect() failed. ท่านจำเป็นต้อง สร้าง App password ก่อน โดยเข้าไปที่ https://myaccount.google.com/security#connectedapps <= ต้อง Login ด้วย Gmail account ของท่าน ![]() 2-Step Verification บางแห่งแนะนำให้ปิด บางแห่งแนะนำให้เปิด แต่จากการทดสอบ จะเปิดหรือปิด ก็สามารถส่งอีเมล์ผ่าน SMTP ของ Gmail ได้ ![]() (Option) เปิด หรือ ปิดการใช้งาน โดยคลิกที่ปุ่ม TURN ON / TURN OFF ถ้าเลือก TURN ON ต้องเข้าไปเลือกวิธีการยืนยันตัวตนด้วย (ไม่ขออธิบายรายละเอียด) ![]() หลังกดปุ่ม Generate แล้วจะได้ App Password ให้นำไปกรอกใน Script (ใช้แทน Gmail Password) จากนั้นทดสอบ ตามข้อ 4 ใหม่อีกครั้ง // Gmail SMTP configuration $mail->isSMTP(); $mail->Host = 'smtp.gmail.com'; $mail->SMTPAuth = true; $mail->Username = 'you@gmail.com'; $mail->Password = '********'; // App password ไม่ใช่ Gmail Password $mail->SMTPSecure = 'tls'; $mail->Port = 587; หากไม่พบ Error แสดงว่าแสดงว่า Script สามารถใช้งานได้ถูกต้องแล้ว ![]() 5) เข้าไปเช็ค inbox ของอีเมล์ผู้รับ ว่าได้รับอีเมล์หรือไม่ อย่าลืมตรวจสอบใน Junk box หรือ ถังขยะด้วย คลิกเพื่อ Download PHPMailer (เฉพาะไฟล์ที่จำเป็นต้องใช้งาน พร้อมตัวอย่าง Script) --> PHPMailer (Update: 14/3/2559) หรือ Save เก็บไว้ใน Google Drive ของคุณ แตกไฟล์ออก แล้ว Upload ขึ้นไปไว้บน Server ใน Folder เดียวกันกับ Script ที่ใช้ส่งอีเมล์ หมายเหตุการส่งอีเมล์ผ่าน SMTP บน Host ของ Siamecohost.Com ตรงอีเมล์ผู้รับ ต้องใช้อีเมล์ @yourdomain.com เท่านั้น เนื่องจาก Mail Server มีระบบป้องการ SPAM หากท่านใช้อีเมล์อื่นในการรับ เช่น xxx@hotmail.com, yyy@yahoo.com จะพบกับ Error แบบนี้PHP Warning: mail(): SMTP server response: 503 This mail server requires authentication when attempting to send to a non-local e-mail address. Please check your mail client settings or contact your administrator to verify that the domain or address is defined for this server. in C:\Inetpub\vhosts\yourdomainname.com\httpdocs\sendmail.php on line 7 ยกเว้น หากท่านส่งอีเมล์โดยใช้ SMTP ของ MailJet สามารถระบุอีเมล์ผู้รับปลายทางเป็นอีเมล์อะไรก็ได้ตามที่ท่านต้องการ ![]() ไม่สงวนสิทธิ์ในการนำไปเผยแพร่แต่ต้องแสดงแหล่งที่มา |