
PHPMailer เป็น Class ที่ถูกพัฒนาขึ้นในการส่งอีเมล์ เพื่อความสะดวกสบายในการเรียกใช้งานซึ่งโดยปกติทาง PHP มีฟังก์ชั่นการส่งอีเมล์คือ mail() แต่ติดปัญหาคือเมล์ที่ส่งไปนั้นไม่ได้รับเนื่องจากอีเมล์ฉบับนั้นอยู่ใน Junkmail จึงทำให้รับความลำบากในการแก้ไขปัญหา ดังนั้น การใช้งาน PHPMailer จึงเป็นทางเลือกหนึ่งในการแก้ไขปัญหาและ อีกทั้ง PHPMailer สามารถใช้งานได้ง่ายหากให้ gmail.com ในการส่งอีเมล์ โดยตัวอย่างบทความใช้ gmail SMTP
สำหรับ PHPMailer คุณสามารถดาวน์โหลดได้ที่ คลิกที่นี้เพื่อดาวน์โหลด
เว็บหลักของ PHPMailer https://github.com/PHPMailer/
การติดตั้ง
1. หลังจากดาวน์โหลดมาแล้วให้ทำการแตกไฟล์ออกจะได้โฟล์เดอร์ PHPMailer
2. Copy โฟล์เดอร์ PHPMailer มาไว้ในโปรเจคของคุณ
การใช้งาน
1. สร้างไฟล์ขึ้นมาหนึ่งไฟล์เพื่อทำการส่งอีเมล์ในตัวอย่างนี้สร้างไฟล์ชื่อ send.php แล้วให้คุณนำ code ที่ผมเขียนไปวางใส่
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
<?php /** * This example shows making an SMTP connection with authentication. */ //SMTP needs accurate times, and the PHP time zone MUST be set //This should be done in your php.ini, but this is how to do it if you don't have access to that date_default_timezone_set('Asia/Bangkok'); require 'PHPMailer/PHPMailerAutoload.php'; //Create a new PHPMailer instance $mail = new PHPMailer; //Tell PHPMailer to use SMTP $mail->isSMTP(); //Enable SMTP debugging // 0 = off (for production use) // 1 = client messages // 2 = client and server messages $mail->SMTPDebug = 0; //Ask for HTML-friendly debug output $mail->Debugoutput = 'html'; //Set the hostname of the mail server $mail->Host = "smtp.gmail.com"; //Set the SMTP port number - likely to be 25, 465 or 587 $mail->Port = 587; //Set the encryption system to use - ssl (deprecated) or tls $mail->SMTPSecure = 'tls'; //Whether to use SMTP authentication $mail->SMTPAuth = true; //Username to use for SMTP authentication $mail->Username = "your@gmail.com"; //Password to use for SMTP authentication $mail->Password = "yourpassword"; //Set who the message is to be sent from $mail->setFrom('your@gmail.com', 'Tony jone'); //Set who the message is to be sent to $mail->addAddress('test@itoffside.com', 'itoffside'); //Set the subject line $mail->Subject = 'itOffside.com test email'; //Read an HTML message body from an external file, convert referenced images to embedded, //convert HTML into a basic plain-text alternative body //$mail->msgHTML(file_get_contents('content.html'), dirname(__FILE__)); $mail->msgHTML("Test email by itoffside.com"); //send the message, check for errors if (!$mail->send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { echo "Message sent!"; } |
ความหมายแต่ละฟังก์ชั่นการทำการที่สำคัญ
1 |
require 'PHPMailer/PHPMailerAutoload.php'; |
ทำการเรียกหาไฟล์ PHPMailerAutoload.php เพื่อใช้ฟังก์ชั่นส่งอีเมล์
$mail->SMTPDebug = 0; คือต้องการ debug หรือไม่หากไม่ใส่ 0
$mail->Host = “smtp.gmail.com”; คือ ค่า smtp ที่คุณจะต้องใส่หากส่งโดย gmail ให้ใส่ตามตัวอย่างส่วน hotmail ผมยังไม่ได้ทดสอบ
$mail->Port = 587; คือ เลขพอร์ทของอีเมล์ หากส่งโดย gmail ให้ใส่ตามตัวอย่าง
$mail->Username = “yourmail@gmail.com”;? ชื่ออีเมล์ที่จะส่ง(แนะนำให้ใช้ gmail)
$mail->Password = “yourpassword”; รหัสผ่านที่ใช้ในการเข้าอีเมล์ของคุณ
$mail->setFrom(‘tangeaim.ball@gmail.com’, ‘Tony jone’); ใส่ชื่ออีเมล์คุณและชื่อ นามสกุลคุณตามตัวอย่าง
$mail->addAddress(‘xxxxxxx@hotmail.com’, ‘itoffside’); ใส่ชื่ออีเมล์ที่รับและชื่อ นามสกุลของคนที่รับอีเมล์
$mail->Subject = ‘itOffside.com test email’; ชื่อเรื่องของอีเมล์
$mail->msgHTML(“Test email by itoffside.com”); ข้อความอีเมล์
*หมายเหตุหากต้องการข้อความที่เป็น html ให้เปลี่ยนเป็นดังนี้ $mail->msgHTML(file_get_contents(‘content.html’), dirname(__FILE__));
//send the message, check for errors
if (!$mail->send()) {
echo “Mailer Error: ” . $mail->ErrorInfo;
} else {
echo “Message sent!”;
}
แสดงผลการส่งอีเมล์ (หากส่งสำเร็จส่งข้อความ Message sent!)
ผลลัพธ์
ได้รับอีเมล์ที่ส่งมาจาก code PHPMailer
สำหรับบทความนี้ก็จบแล้ว การใช้งาน PHPMailer นั้นใช้งานง่ายมีฟังก์ชั่นในการใช้งานมาให้แล้วเพียงแต่คุณต้องนำค่ามาใส่ให้ถูกต้องเท่านั้นและในตัวอย่างบทความนี้ เป็นการใช้งานพื้นฐานซึ่ง PHPMailer มีความสามารถมากมายให้ผู้ใช้งานได้ทดสอบ เช่น การส่งอีเมล์แนบไฟล์รูป เป็นต้น หวังว่าผู้อ่านทุกคนคงได้รับประโยชน์ไม่มากก็น้อยครับ หากมีคำถามให้เขียน comment ด้านล่างได้เลยครับ หากชอบก็กดแชร์และกด Like ได้ครับ ขอบคุณครับ
Mailer Error: SMTP connect() failed. ผมได้ยังงี้อ่ะ
smpt ไม่ถูกครับ คุณใช้ smpt อะไรในการส่ง
เจอเหมือนกันเลยค่ะ
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
You must provide at least one recipient email address. Eror ยังงี้ครับ
เเก้ยังงัยครับผมงมมา3วันละนี้ครับโค้ต
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
$strTo->$objResult[‘Email’] ;
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
//Ask for HTML-friendly debug output
$mail->Debugoutput = ‘html’;
//Set the hostname of the mail server
$mail->Host = “smtp.gmail.com”;
//Set the SMTP port number – likely to be 25, 465 or 587
$mail->Port = 587;
//Set the encryption system to use – ssl (deprecated) or tls
$mail->SMTPSecure = ‘tls’;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username =”kkanthong@gmail.com”;
//Password to use for SMTP authentication
$mail->Password = “xxxxxxxxx”;
//Set who the message is to be sent from
$mail->setFrom(‘kkanthong@gmail.com’, ‘DIVNE.COM’);
//Set who the message is to be sent to
$mail->addAddress(‘$strTo’);
//Set the subject line
$mail->Subject = ‘Activate Member Account’;
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
//$mail->msgHTML(file_get_contents(‘content.html’), dirname(__FILE__));
$mail->msgHTML(“$strMessage”);
error บอกว่า ไม่มีชื่ออีเมล์ที่จะรับ
ผมได้ Error แบบนี้ แก้ยังงัยดีครับ
Warning: fsockopen(): SSL: Handshake timed out in C:\xampp\htdocs\chkarnchang\class.smtp.php on line 105
Warning: fsockopen(): Failed to enable crypto in C:\xampp\htdocs\chkarnchang\class.smtp.php on line 105
Warning: fsockopen(): unable to connect to ssl://smtp.gmail.com:465 (Unknown error) in C:\xampp\htdocs\chkarnchang\class.smtp.php on line 105
ตรงนี้ตั้งค่าตรง php.ini ครับ
ขอบคุณมากนะคะ สำหรับ script ด้านบนค่ะ แต่จากที่ลองแล้ว ถ้าส่งด้วย gmail โดยใช้
$mail->Host = “smtp.gmail.com”;
ส่งไม่ได้ค่ะ อาจจะเกิดการ block ของ gmail เอง จึงเปลี่ยนเป็นส่งด้วย hotmail โดยใช้
$mail->Host = “smtp.live.com”;
ได้ผลเรียบร้อยดีค่ะ แต่มีปัญหาอยากจะปรึกษาว่าในส่วนข้อความที่จะส่ง ถ้าเรามีหลายบรรทัด ไม่ทราบว่าจะสามารถทำได้ไหมคะ เพราะพยายามใช้ “\r\n” แล้ว แต่ยังคงอยู่ที่บรรทัดเดียวค่ะ
$mail->msgHTML(“Test email by itoffside.com”);
ขอบคุณค่ะะ
🙂
ขอบคุณมากครับ เก็บไว้ศึกษา
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
แก้ยังไงครับ
สอบถามหน่อยครับ พอดีต้องการส่งไฟล์แนบไปคับ แล้วชื่อไฟล์เป็นภาษาไทย.jpg มันไม่แนบไปด้วยครับ ใครพอมีทางแก้ได้ไหมครับ ***ถ้าใช้ชื่อไฟล์เป็นภาษาอังกฤษ ส่งไปได้นะครับ
แปลงเป็น utf8 ก่อนครับ
แปลงยังไงครับ
$mypath=’myfile/ลานจอดรถสำหรับVIP.jpg’;
$mail->addAttachment($mypath, ‘ลานจอดรถสำหรับVIP’);
ผมเซ็ท ค่านี้ไว้แล้วด้วยครับ
$mail->CharSet = ‘UTF-8’;
ถ้าอย่างนั้น น่าจะไม่ได้ครับ
Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\followsales\PHPMailer\class.smtp.php on line 1212
แก้ยังไงอะคะ ช่วยบอกที
อันนี้ ตอนกดส่ง แล้วขึ้นหรือว่าไงครับ
Mailer Error: Extension missing: openssl
ความว่าอย่างไงครับ
error การเ ปิด port ssl ครับ
เออเร่อตรงนี้เหมือนกันค่ะ ต้องทำอย่างไรคะ
Warning: require(PHPMailer/PHPMailerAutoload.php): failed to open stream: No such file or directory in
ขึ้นอย่างงี้อะครับ
ไม่มีไฟล์ที่ถูกเรียกครับ
ขอโทษนะครับ ผมหาที่อยู่ไฟล์ PHPMailerAutoload.php ไม่เจออ่ะครับ จะเรียกใช้ก็ไม่ได้ ไฟล์มัน path ยังไง หรอครับ
PHPMailerAutoload.php
โหลดมาแล้วไฟล์นี้หายไปครับ
PHPMailer-master.zip ไม่มีไฟล์ PHPMailerAutoload.php นะครับ