pico hosting ผู้ให้บริการ Web hosting จดโดเมนราคาประหยัด  

บทความ: PHPMailer ส่งอีเมลล์ผ่าน SMTP server

    การใช้ฟังก์ชั่น mail() ของ php ที่มีให้มานั้น มักจะเจอกับปัญหาส่งอีเมลล์แล้วไปลง junk หรือ spam box ของผู้รับ ทำให้ผู้ส่งกับผู้รับอีเมล์พลาดการติดต่อกันได้ ผมได้ศึกษาหาวิธีการแก้ไขปัญหาดังกล่าว ก็ไปเจอสาเหตุที่ทำให้อีเมล์ที่ส่งไปนั้นลง junk หรือ spam box เพราะ ฟังก์ชั่น mail() นั้นจะใช้ user DirectAdmin สร้างขึ้นมาในการส่งอีเมล์ แต่อีเมล์ผู้ส่งในโปรแกรมที่เราเขียนมักจะเป็นอีเมล์ของเรา และไม่ตรงกับชื่อ user ที่ DirectAdmin สร้างขึ้น ทำให้ mail server ของผู้รับอย่าง gmail, hotmail, yahoo มองอีเมล์ที่เราส่งไปเป็น junk หรือ spam
    สำหรับการแก้ไขปัญหานั้น หลายๆคำแนะนำ เขาแนะนำให้ส่งเมล์ผ่าน SMTP server เพราะจะทำให้ชื่ออีเมล์ผู้ส่งจริงๆเป็นชื่อที่เรากำหนดไว้ ไม่ใช่ชื่อ user ที่ DirectAdmin สร้างขึ้นนั่นเอง วันนี้เลยมาแนะนำวิธีการใช้งาน PHPMailer ครับ
    PHPMailer เป็น php class สำหรับใช้ส่งอีเมล์ สามารถส่งอีเมล์ผ่าน SMTP server ได้ ส่งได้ทั้งแบบ html และ text รวมไปถึงสามารถแนบไฟล์ในอีเมล์ที่ต้องการส่งได้ด้วย Feature ของ PHPMailer มีดังนี้

  * Supports emails digitally signed with S/MIME encryption!
  * Supports emails with multiple TOs, CCs, BCCs and REPLY-TOs
  * Works on any platform.
  * Supports Text & HTML emails.
  * Embedded image support.
  * Multipart/alternative emails for mail clients that do not read HTML email.
  * Flexible debugging.
  * Custom mail headers.
  * Redundant SMTP servers.
  * Support for 8bit, base64, binary, and quoted-printable encoding.
  * Word wrap.
  * Multiple fs, string, and binary attachments (those from database, string, etc).
  * SMTP authentication.
  * Tested on multiple SMTP servers: Sendmail, qmail, Postfix, Gmail, Imail, Exchange, etc.
  * Good documentation, many examples included in download.
  * It's swift, small, and simple.

    PHPMailer สามารถดาวน์โหลดได้ที่ http://phpmailer.worxware.com/index.php?pg=phpmailer หลังจากที่ดาวน์โหลดไฟล์มาแล้วให้แตกไฟล์ออกมา จะมีไฟล์ของ PHPMailer ให้เรา include ไฟล์ class.phpmailer.php ไปใน script ที่เราต้องการส่งอีเมล์ สำหรับตัวอย่างการเขียน php script ให้ส่งอีเมล์ผ่าน SMTP server ด้วย PHPMailer มีดังนี้
<?PHP
require("PHPMailer_v5.0.2/class.phpmailer.php");
$mail = new PHPMailer();

$body = "ทดสอบการส่งอีเมล์ภาษาไทย UTF-8 ผ่าน SMTP Server ด้วย PHPMailer.";

$mail->CharSet = "utf-8";
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->Host = "smtp.yourdomain.com"; // SMTP server
$mail->Port = 25; // พอร์ท
$mail->Username = "email@yourdomain.com"; // account SMTP
$mail->Password = "******"; // รหัสผ่าน SMTP

$mail->SetFrom("email@yourdomain.com", "yourname");
$mail->AddReplyTo("email@yourdomain.com", "yourname");
$mail->Subject = "ทดสอบ PHPMailer.";

$mail->MsgHTML($body);

$mail->AddAddress("recipient1@somedomain.com", "recipient1"); // ผู้รับคนที่หนึ่ง
$mail->AddAddress("recipient2@somedomain.com", "recipient2"); // ผู้รับคนที่สอง

if(!$mail->Send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}
?>

    สำหรับข้อมูลและเอกสารต่างๆเกี่ยวกับ PHPMailer สามารถอ่านได้ใน document ของ PHPMailer ครับ

    Download : PHPMailer_v5.0.2.zip

    หมายเหตุ การส่งอีเมล์โดยผ่าน SMTP server เป็นเพียงแค่การป้องกันอีเมล์ที่เราส่งไปนั้นลง junk หรือ spam box อีกวิธีหนึ่งเท่านั้น ยังมีปัจจัยอื่นอีกหลายๆอย่างที่ทำให้อีเมล์ของเราที่ส่งไปถูกจับให้ลง junk หรือ spam box ครับ

ใครที่นำบทความไปเผยแพร่ที่เว็บไซต์อื่น กรุณาทำลิ้งค์กลับมายังบทความต้นฉบับด้วยนะครับ