ยินดีต้อนรับ กรุณา สมัครสมาชิก หรือเข้าสู่ระบบ

Home » webboard » PHP
เข้าชม : 6251

ส่งเมลยังไงไม่ให้เข้าไปอยู่เมลขยะ

โพสต์เมื่อ: วันพฤหัสบดี 3 มีนาคม 2554  21:00 น.

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 : http://www.picohosting.com/files/PHPMailer_v5.0.2.zip

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


ที่มา Credit : http://www.picohosting.com/howto/phpmailer

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

ส่งเมลยังไงไม่ให้เข้าไปอยู่เมล
[ ความเห็นที่ 1]
RE : ส่งเมลยังไงไม่ให้เข้าไปอยู่เมลขยะ
โพสต์เมื่อ: วันเสาร์ 26 มีนาคม 2554  01:46 น.

การส่ง Mail SMTP นั้นมีข้อดีมากกว่า การใช้ mailer ธรรมดาเนื่องจาก เป็นการส่ง E-mail ที่ต้องมีตัวต้นในการส่ง จึงทำให้ Email ที่ส่งนั้นถูกมองว่าเป็น spam น้อยลง ดังนั้นเรามาดูวิธีใช้ phpmailer จาก php กันค่ะ
PHPMailer

คุณสมบัติต่าง ๆ ของ  PHPMailer มีดังนี้:
    * สนับสนุน emails digitally signed with S/MIME encryption!
    * สนับสนุน  emails with multiple TOs, CCs, BCCs and REPLY-TOs
    * ทำงานได้หลาย platforms
    * สนับสนุนการส่งเมล์ ทั้งข้อความ และ HTML
    * สนับสนุนการส่งเมล์ โดยมีภาพ ( Embled image )
    * Multipart/alternative emails for mail clients that do not read HTML email.
    * สามารถตรวจ สอบ/ดีบัค ได้ง่าย
    * สามารถแก้ไข mail headers ได้
    * Redundant SMTP servers.
    * สนับสนุน 8bit, base64, binary, and quoted-printable encoding.
    * Word wrap.
    * Multiple fs, string, and binary attachments (those from database, string, etc).
    * สามารถ ใช้ SMTP Authentication ได้
    * ทดสอบแล้วจากหลาย ๆ SMTP servers: Sendmail, qmail, Postfix, Gmail, Imail, Exchange, etc.
    * มีคู่มือการใช้งาน
    * It's swift, small, and simple.


ข้อ ดีของการส่ง Mail แบบ SMTP

    - ใช้แก้ปัญหาสำหรับระบบ security ที่มีการป้องกันการใช้งาน mail หรือ ถูก disable mail ใน php.ini ซึ่งอาการจะเกิดขึ้นดังนี้
"Warning: mail() [function.mail]: Permission denied"

    - การส่ง Mail แบบ SMTP นั้นเป็นการส่งจาก Email ที่มีตัวตน ดังนั้นจึงเพิ่มโอกาศ ที่ Mail จะไปยังผู้รับและไม่เข้า junkbox มากขึ้น  



Download PHPMailer จากที่นี้


ขั้นตอนการใช้งาน PHPMailer หรือ วิธีการส่งเมล์จาก php ด้วย PHPMailer

1. ทำการแตกไฟล์ PHPMailer_v5.1.zip ที่ได้จากการ download มา จากนั้น upload ขึ้นไปไว้บน server ใน directory public_html
2. ทำการแก้ไขเพิ่ม code ในไฟล์ php ที่เราต้องการส่งอีเมล์ โดยเพิ่มฟังก์ชั่นนี้เข้าไป


<?PHP
require("PHPMailer_v5.1/class.phpmailer.php");  // ประกาศใช้ class phpmailer กรุณาตรวจสอบ ว่าประกาศถูก path

function smtpmail( $email , $subject , $body )
{
    $mail = new PHPMailer();
    $mail->IsSMTP();         
      $mail->CharSet = "utf-8";  // ในส่วนนี้ ถ้าระบบเราใช้ tis-620 หรือ windows-874 สามารถแก้ไขเปลี่ยนได้                        
    $mail->Host     = "mail.yourdomain.com"; //  mail server ของเรา
    $mail->SMTPAuth = true;     //  เลือกการใช้งานส่งเมล์ แบบ SMTP
    $mail->Username = "account@yourdomain.com";   //  account e-mail ของเราที่ต้องการจะส่ง
    $mail->Password = "**********";  //  รหัสผ่าน e-mail ของเราที่ต้องการจะส่ง

    $mail->From     = "account@yourdomain.com";  //  account e-mail ของเราที่ใช้ในการส่งอีเมล
    $mail->FromName = "ชื่อผู้ส่ง"; //  ชื่อผู้ส่งที่แสดง เมื่อผู้รับได้รับเมล์ของเรา
    $mail->AddAddress($email);            // Email ปลายทางที่เราต้องการส่ง(ไม่ต้องแก้ไข)
    $mail->IsHTML(false);                  // ถ้า E-mail นี้ มีข้อความในการส่งเป็น tag html ต้องแก้ไข เป็น true
    $mail->Subject     =  $subject;        // หัวข้อที่จะส่ง(ไม่ต้องแก้ไข)
    $mail->Body     = $body;                   // ข้อความ ที่จะส่ง(ไม่ต้องแก้ไข)
     $result = $mail->send();       
     return $result;
}
?>

3. เวลาเรียกใช้งาน ให้ใช้ smtpmail("อีเมล์ปลายทาง","หัวข้อส่ง email","ข้อความที่เราต้องการส่ง");


ด้วย 3 วิธีง่าย ๆ เรื่องการส่ง เมล์ php โดย smtp ก็ไม่ใช่ปัญหา อีกต่อ ไปค่ะ :D


ที่มา http://www.thaihostclub.com/%E0%B8%9A%E0%B8%97%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1/mail-smtp-phpmailer


PHPMailer
[ ความเห็นที่ 2]
RE : ส่งเมลยังไงไม่ให้เข้าไปอยู่เมลขยะ
โพสต์เมื่อ: วันเสาร์ 26 มีนาคม 2554  02:02 น.

ส่งเมล์จากเว็บของเราให้เข้า inbox hotmail ให้ได้ทุกครั้ง

Written on April 27, 2010 – 2:17 am | by admin |
 

ก็บังเอิญว่าในเว็บ mocyc.com มีระบบที่ต้องส่งเมล์ถึงสมาชิกอยู่บ่อยครั้ง และจากการใช้งานระบบส่งเมล์บน Server เพื่อส่งเมล์ไปยัง Hotmail บางทีก็เข้า Inbox บ้าง บางทีก็ไปอยู่ Junkbox บ้าง บางทีไม่เข้าเลยก็มี ก็เลยต้องหาวิธีส่งเพื่อให้เข้า Inbox ให้ได้

ลองทั้งใช้คำสั่ง mail เอง และ ใช้ SMTP เพื่อส่งก็ยังเจออาการเดิมๆ

ปกติผมจะใช้ PHPmailer ในการส่งเมล์อยู่แล้ว พอเจอปัญหาส่งเมล์ไม่เข้าหนักขึ้นๆ ผมก็เลยต้องเปลี่ยนมาเป็น ใช้ PHPMailer ส่งผ่าน Gmail ซึ่งก็สามารถส่งเมล์เข้า Inbox Hotmail ได้ทุกครั้งเช่นกัน แต่มันติดอยู่ที่ Gmail ให้ส่งได้ไม่เกิน 100 Mail ต่อวันเท่านั้น (ทำไมน้อยจังฟ่ะ)

ตามไปอ่าน เขียน php ส่งเมล์ด้วย PHPMailer ด้วย Gmail Account ได้จากเว็บ 9aum ส่วนวิธีการใช้งาน PHPMailer ขอไม่พูดถึงครับ ไปดูเพิ่มเติมได้ที่ http://phpmailer.sourceforge.net/

ก็จากที่ใช้ Gmail แล้วมีปัญหาว่ามันส่งได้แค่วันละ 100 Mail ต่อวัน นี่เอง จึงทำให้เกิดปัญหาว่าเมื่อเกิน 100 ไปเมล์ก็จะส่งไม่ถึงจุดหมาย ก็เลยต้องหาวิธีใหม่…

และแล้วก็พบทางสว่าง เมื่อได้เห็นข้อความนี้ Yahoo Mail email send limit - no more than 100 emails or recipients per hour โดยที่ทาง Yahoo.com สามารถให้ส่งเมล์ได้ถึง 100 Mail ไม่ใช่ต่อวัน แต่เป็นต่อชั่วโมง……โอ้วแม่เจ้า

ก็จัดแจงเปลี่ยนค่าตัวแปรในสคริปส่งเมล์ดูสิว่าจะส่งได้ไหม

            $mail   = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth   = true;
$mail->Host       = “smtp.mail.yahoo.com”;
$mail->Port       = 25;
$mail->Username   = “youruser@yahoo.com”;
$mail->Password   = “password”;

$mail->SetFrom(”youruser@yahoo.com”, “Mocyc Dot Com”);
$mail->AddReplyTo(”youruser@yahoo.com”,”Mocyc Dot Com”);

$message = “มีคนตอบประกาศของท่านใน Mocyc.com<br /><br />”;
$message .= “URL : <a href=\”http://www.mocyc.com/store/\” target=\”_blank\”>http://www.mocyc.com/store/</a><br /><br />”;
$message .= “หมายเหตุ : อีเมล์นี้เป็นอีเมล์อัตโนมัติจาก Mocyc.com ไม่ต้องตอบกลับ<br /><br />”;

$mail->Subject = ‘Comment on your classified Mocyc.com’;

$mail->MsgHTML($message);

$address = $arr_member[’email’];
$mail->AddAddress($address, “”.$arr_data[’name’].”");

$mail->Send();

ได้ครับ ผมลองส่งให้ตัวเองกว่า 100 ครั้งต่อชั่วโมง ยังส่งได้ ไม่มีหลุดครับ เข้า Inbox ตลอดครับ

ที่มา http://www.myblog.in.th/2010/04/27/phpmailer_with_yahoo/


PHPMailer
[ ความเห็นที่ 3]
RE : ส่งเมลยังไงไม่ให้เข้าไปอยู่เมลขยะ
โพสต์เมื่อ: วันอาทิตย์ 27 มีนาคม 2554  00:56 น.

Microsoft has recently made changes to their Hotmail ports and settings.

Our basic example on the PHPMailer website now is:

require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

$mail = new PHPMailer();

$body = file_get_contents('contents.html');
$body = eregi_replace("[\]",'',$body);

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.live.com"; // sets HOTMAIL as the SMTP server
$mail->Port = 25; // alternate is "26" - set the SMTP port for the HOTMAIL server
$mail->Username = "yourusername@hotmail.com"; // HOTMAIL username
$mail->Password = "yourpassword"; // HOTMAIL password

$mail->SetFrom('name@yourdomain.com', 'First Last');

$mail->AddReplyTo("name@yourdomain.com","First Last");

$mail->Subject = "PHPMailer Test Subject via smtp (Hotmail), basic";

$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);

$address = "whoto@otherdomain.com";
$mail->AddAddress($address, "John Doe");

$mail->AddAttachment("images/phpmailer.gif"); // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

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


สำหรับ hotmail.co.th


คุณต้องสมัครสมาชิก ถึงจะโพสกระทู้ได้

ปิดรับสมัครสมาชิกแล้ว


คำยอดฮิต :- 0