ในมุมมองของ Cpanel การส่งด้วย Script จะถูกมอง่าคือ nobody และ Hosting บางแห่งจะไม่อนุญาติให้ส่งด้วย Script PHPที่ไม่ผ่านการ Authen ผ่าน SMTP ฉะนั้นทางเลือกที่ดีที่สุดคือ ในการส่งอีเมล์ควรใช้แบบ PHP กับ SMTP ด้วยทุกครั้ง
Example 1 การส่งอีเมล์ไปยังปลายทางด้วย Script ของ PHP
mail.php
01.<html>02.<head>03.<title>ThaiCreate.Com</title>04.</head>05.<body>06.<?php07.$strTo = "thaicreate-admin@hotmail.com";08.$strSubject = "Testing send mail";09.$strHeader = "From: webmaster@thaicreate.com";10.$strMessage = "My Body & My Description";11.$flgSend = mail($strTo,$strSubject,$strMessage,$strHeader);12.if($flgSend)13.{14.echo "Mail sending.";15.}16.else17.{18.echo "Mail cannot send.";19.}20.?>21.</body>22.</html>เมื่อทำการทดสอบ
ปลายทางได้รับอีเมล์ อ่านเพิ่มเติม PHP Sending Email
Example 2 การส่งอีเมล์ไปยังปลายทางด้วย Script ของ PHP ผ่าน SMTP สำหรับวิธีนี้เป็นวิธีที่สามารถตรวจสอบได้ด้วยว่า Email ถึงปลายทางหรือไม่ เพราะถ้าไม่ถึงปลายทาง จะมีการตอบกลับถึงสาเหตุ และบาง Hosting จะยิมยอมให้ Script ของ PHP สามารถทำการส่ง Mail จาก Web Hosting ด้วย SMTP เท่านั้น (ไม่สามารถใช้ตามตัวอย่างแรกได้)
การสร้าง Email Account การใช้งาน SMTP อีเมล์กับ Outlook บน Cpanel
สร้าง Email Account บน Cpanel ให้เรียบร้อยก่อน
smtp.php
01.<html>02.<head>03.<title>ThaiCreate.Com</title>04.</head>05.<body>06.<?php07.require_once('class.phpmailer.php');08.$mail = new PHPMailer();09.$mail->IsHTML(true);10.$mail->IsSMTP();11.$mail->SMTPAuth = true; // enable SMTP authentication12.$mail->SMTPSecure = ""; // sets the prefix to the servier13.$mail->Host = "mail.thaicreate.com"; // sets as the SMTP server14.$mail->Port = 25; // set the SMTP port for the server15.$mail->Username = "admin@thaicreate.com"; // username16.$mail->Password = "password"; // password17.$mail->From = "admin@thaicreate.com"; // "name@yourdomain.com";18.$mail->FromName = "www.ThaiCreate.Com"; // set from Name19.$mail->Subject = "Test sending mail.";20.$mail->Body = "Test Mail Description";21.$mail->AddAddress("thaicreate-admin@hotmail.com", "Weerachai Nukitram"); // to Address22.$mail->Send();23.?>24.</body>25.</html>เมื่อทดสอบการทำงาน
อีเมล์ถุกส่งไปยังปลายทาง สำหรีบตัวอย่างที่ 2 จะใช้ library ของ phpmailer สามารถอ่านต่อได้ที่นี่ ส่งอีเมล์ด้วย SMTP / POP






0 ความคิดเห็น:
แสดงความคิดเห็น