ส่งอีเมลบน localhost ด้วย HMailServer

การส่งอีเมลด้วยฟังก์ชั่น mail() ของ php แบบธรรมดาๆนั้น หากเครื่องที่พัฒนาอยู่ไม่มี smtp server ก็จะส่งไปไม่ได้ การใช้ HMailServer มาติดตั้งจำลอง mail server บนเครื่องก็จะช่วยให้งานง่ายขึ้นได้มาก.

ในบทความนี้จะช่วยให้ติดตั้ง HMailServer เพื่อใช้รับและส่งอยู่ภายใน localhost เพื่อดูการทำงานเท่านั้น ไม่สามารถส่งไปยัง email server จริงๆอย่าง gmail, hotmail, อื่นๆได้

เริ่มต้นด้วยกำหนด domain เสมือนจริงให้กับเครื่อง.

เปิดไฟล์ C:\windows\system32\drivers\etc\host ด้วย notepad แบบ Run as administrator
เพิ่ม 127.0.0.1 localhost.com ไว้บรรทัดสุดท้าย
Save ไฟล์แล้วลองเปิด cmd สั่ง ping localhost.com จะได้หมายเลข IP 127.0.0.1 หากไม่ได้ตามนี้ให้ restart เครื่อง 1 ครั้งแล้วลองใหม่ก็น่าจะได้แล้ว.
สำหรับ OS อื่นๆโปรดค้นหา search engine วิธีเพิ่ม domain 127.0.0.1

ติดตั้ง HMailServer

ดาวน์โหลดและติดตั้งโปรดทำตามขั้นตอนในเว็บไซต์นี้ http://www.thaicreate.com/community/hmailserver-installation-step-by-step.html
โดยเมื่อทำถึงขั้นตอน Add Domain นั้น ให้กรอก Domain เป็น localhost.com ตามที่ได้เพิ่มไว้แล้วด้านบน
จากนั้นจึงเพิ่ม Address และกำหนด Password เช่น admin และ Password เป็น pass ก็จะได้อีเมล admin@localhost.com ไว้ใช้งานเรียบร้อยแล้ว.

เพิ่ม Email ให้กับ email client เช่น Outlook, Thunderbird

ตัวอย่างต่อไปนี้จะขอยกตัวอย่างวิธีทำจาก Thunderbird แทน ส่วน client อื่นๆวิธีต่างกันแต่หลักการเดียวกัน
เปิดเมนู Tools > Accoun Settings...
จากตัวเลือก Account Actions ให้เลือก Add Mail Account...
กรอกชื่อ กรอกอีเมล และรหัสผ่าน แล้วกด Continue
รอกระบวนการอัตโนมัติของ Thunderbird จนเสร็จแล้วค่อยกด Manual config
Incoming เลือก POP3 Server hostname กรอก 127.0.0.1 Port 110
Outgoing Server hostname กรอก 127.0.0.1 Port 25
ตรงช่อง Username เติม @localhost.com เข้าไปท้ายชื่อถ้ายังไม่มี. จากนั้นกด Create Account

ทดลองรับอีเมล
คลิกที่ Inbox ภายใต้ account email ที่ได้เพิ่มลง Thunderbird แล้วกดปุ่ม Get Mail ด้านบน
ดูที่ Status ด้านล่าง ถ้าขึ้น There are no new message for [emailที่กรอก] ก็หมายความว่าระบบติดตั้งถูกและทำงานได้ครึ่งหนึ่งแล้ว.

เขียนสคริปส่งอีเมลด้วย PHP mail()

ใช้สคริปต่อไปนี้ ทำแบบง่ายๆเพื่อทดลองว่าฟังก์ชั่น mail() ทำงานได้และส่งอีเมลเข้า account


<?php
if ( isset( $_POST['mailto'] ) ) {
    $subject = 'test send mail() from php';
    $from = 'From: no-reply@localhost.com';
    $message = 'this is mail message.';
    $result = mail($_POST['mailto'], $subject, $message, $from);
    if ( $result ) {
        echo 'done';
    } else {
        echo 'fail';
    }
}
?>
<form method="post">
to: <input type="text" name="mailto" value="" />
<input type="submit" value="send" />
</form>


ทดลองกรอกอีเมลผู้รับเป็น admin@localhost.com ที่ได้เพิ่มไว้ใน HMailServer แล้วกด send. ถ้าหากไม่มี error และขึ้น done หมายความว่าส่งได้
ให้เปิดโปรแกรม email client เพื่อเช็คอีเมล ถ้ามีอีเมลใหม่เข้ามา ก็แสดงว่าระบบติดตั้งถูกต้องทั้งหมด และทำงานได้ 100% อย่างไม่มีปัญหา

บางครั้งการส่งอีเมลเข้า HMailServer ก็จะถูกแบนได้เหมือนกัน เช่น ส่งถี่ๆ
ให้เปิด HMailServer Administrator แล้ว connect
ในแถบทางซ้าย เปิดไล่ไปจาก Settings > Advanced > IP Ranges มองหาบรรทัดที่มีตัวอักษรแดงๆแล้วลบออกให้หมดก็น่าจะกลับมาส่งอีเมลได้อีกตามปกติ.

4 comments on “ส่งอีเมลบน localhost ด้วย HMailServer

ใส่ความเห็น

อีเมลของคุณจะไม่แสดงให้คนอื่นเห็น ช่องข้อมูลจำเป็นถูกทำเครื่องหมาย *

คุณอาจใช้แท็กHTMLและแอททริบิวต์เหล่านี้: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>