2 มิถุนายน 2554

ส่งเมล์ด้วย phpmailer

   ก่อนอื่นต้องเข้าไปโหลด  Class Phpmailer  มาก่อนจากเว็บ http://sourceforge.net/projects/phpmailer/files/phpmailer%20for%20php4/PHPMailer%20v2.0.4%20for%20PHP4/ โดยผมเลือกโหลดที่ เวอร์ชั่น PHPMailer_v2.0.4 หลังจากดาวน์โหลดมาเสร็จแล้วทำการ แตกไฟล์ ก็จะมีไฟล์ในนั้นมากมายเราเอาไว้แค่ 3 ไฟล์ครับ คือ

1.class.phpmailer.php
2.class.smtp.php
3.class.pop3.php

ซึ่งในขั้นตอนต่อไปก่อนที่จะทำการ ทดลองส่งเมล์ ให้เราแก้ปัญหาเบื้องต้นก่อนทำการทดลอง คือ

1.แก้ $Charset ในไฟล์ class.phpmailer.php จาก iso-8859-1 เป็น utf-8 เพื่อให้เมล์ของเรารองรับภาษาไทย
2.เปิด ;extension=php_openssl.dll ในไฟล์  php.ini โดยลบเครื่องหมาย ; ด้านหน้าแล้วทำการ รีสตาร์ท Apache หนึ่งครั้ง เพื่อทำการเปิดใช้งาน SSL (อ่านเพิ่มเติม)

หลังจากแก้ไขตาม 2 วิธีข้างต้นเสร็จแล้วก็เริ่มทำการสร้างไฟล์เพื่อทดสอบการส่งเมล์ของเราตามนี้เลยครับ

<?php
require('class.phpmailer.php');

$mail = new PHPMailer();
$mail->From = 'chaiwit@xxx-xxx.com'; // เมล์ที่เราใช้ในการส่ง
$mail->FromName = 'chaiwit'; // ชื่อที่ส่ง

$to =array ('chaiwit'=>'chaiwit@xxx-xxx.com');  // to array ใช้ในกรณีส่งหลาย ๆ คนพร้อมกัน
foreach ($to as $name=>$email){
    $mail->AddAddress($email,$name);
}

$cc =array ('chaiwit'=>'chaiwit@xxx-xxx.com'); // cc array ใช้ในกรณี cc หลาย ๆ คนพร้อมกัน
foreach ($cc as $cc_name=>$cc_email){
    $mail->AddCC($cc_email,$cc_name);
}

$bcc =array ('chaiwit'=>'chaiwit@xxx-xxx.com'); // bcc array ใช้ในกรณี bcc หลาย ๆ คนพร้อมกัน
foreach ($bcc as $bcc_name=>$bcc_email){
    $mail->AddBCC($bcc_email,$bcc_name);
}

$mail-> Subject    = 'Test Send Mail'; // หัวข้อเมล์ที่ทำการส่ง ซึ่งใช้ภาษาไทยได้ (เพราะทำการตั้งค่า Charset เป็น utp-8 แล้ว)
$mail-> Body        = 'Mail Detail'; // เนื้อหาของเมล์

$mail->IsHTML (true);
$mail->IsSMTP(); // เปิดใช้ Smtp
$mail->Host = 'ssl://smtp.gmail.com'; // ส่งผ่าน Smtp ของ google
$mail->Port = 465; // พอร์ต
$mail->SMTPAuth    = true;

$mail->Username = 'chaiwit@gmail.com'; // Account ของ Gmail
$mail->Password = 'xxx';

$mail->Send();
$mail->ClearAddresses();
?>


อ่านเพิ่มเติมได้ที่ http://www.select2web.com/php/send-email-with-phpmailer.html

1 ความคิดเห็น:


  1. Fatal error: Class 'SMTP' not found in /opt/lampp/htdocs/buriramthailand/PHPMailer/class.phpmailer.php on line 1466

    ตอบลบ