|
เปิดทำการ จันทร์ - เสาร์ เวลา 09:00 - 20:00 น. ขณะนี้เป็นเวลา: เปิดทำการ นอกเวลาทำการหรือวันหยุด กรุณาติดต่อผ่านช่องทาง Robot Support หรือ Email: support@siamecohost.com |
Update 23/10/2021 11:40 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 1. การส่งอีเมล์โดยใช้ SMTP ของโฮสต์ (ไม่แนะนำ เนื่องจากมีการลิมิต และ อีเมลล์มีโอกาสเข้าไปอยู่ Junk mail ) <? 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)." 2. การส่งอีเมล์โดยใช้ SMTP ของ Gmail / G-Suite (WorkSpace) - Update 23/10/2021 โปรดทราบ email@gmail.com ส่งฟรีได้ 500 ฉบับ/วัน, G-Suite/WorkSpace email@yourdomain.com ส่งได้ 2,000 ฉบับ/วัน <? 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)." 3.) เปลี่ยนอีเมล์ผู้รับ จาก contactus@yourdomain.com เป็น ที่อยู่อีเมล์ของท่าน จากนั้น Upload ทั้ง 2 ไฟล์ ขึ้นไปไว้บน Server 4) ทดสอบการใช้งานโดยเปิดหน้า http://www.yourdomain.com/form2mail.html กรอกข้อมูลให้ครบทุกช่อง แล้วคลิกปุ่ม "Send Mail" *** คำแนะนำเพิ่มเติม *** สำหรับผู้ใช้ SMTP ของ Gmail หากพบ Mailer Error: SMTP connect() failed. ให้ทำตามขั้นตอนดังนี้ - Update 23/10/2021 *** 1. เปิดการอนุญาตให้ใช้ smtp google http://www.google.com/accounts/DisplayUnlockCaptcha 2. ปิดการยืนยันแบบ 2 ขั้นตอน https://support.google.com/accounts/answer/1064203?hl=th 3. เปิด อนุญาตให้แอปที่มีความปลอดภัยน้อยเข้าถึงบัญชีของคุณ https://myaccount.google.com/lesssecureapps หากไม่พบ 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 ตรงอีเมล์ผู้ส่ง (From/$fm) ต้องใช้อีเมล์ @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 ไม่สงวนสิทธิ์ในการนำไปเผยแพร่แต่ต้องแสดงแหล่งที่มา |