การส่งอีเมล์ด้วย PHPMailer (โดยส่งผ่านอีเมล์ Gmail SMTP)

PHPMailer

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 ที่ผมเขียนไปวางใส่

ความหมายแต่ละฟังก์ชั่นการทำการที่สำคัญ

ทำการเรียกหาไฟล์ 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!)
ผลลัพธ์
PHPMailer

ได้รับอีเมล์ที่ส่งมาจาก code PHPMailer
PHPMailer

สำหรับบทความนี้ก็จบแล้ว การใช้งาน PHPMailer นั้นใช้งานง่ายมีฟังก์ชั่นในการใช้งานมาให้แล้วเพียงแต่คุณต้องนำค่ามาใส่ให้ถูกต้องเท่านั้นและในตัวอย่างบทความนี้ เป็นการใช้งานพื้นฐานซึ่ง PHPMailer มีความสามารถมากมายให้ผู้ใช้งานได้ทดสอบ เช่น การส่งอีเมล์แนบไฟล์รูป เป็นต้น หวังว่าผู้อ่านทุกคนคงได้รับประโยชน์ไม่มากก็น้อยครับ หากมีคำถามให้เขียน comment ด้านล่างได้เลยครับ หากชอบก็กดแชร์และกด Like ได้ครับ ขอบคุณครับ

You May Also Like

About the Author: Tawatsak

โปรแกรมเมอร์ ติดต่อ : itoffside@hotmail.com
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

26 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
sqre
sqre
9 years ago

Mailer Error: SMTP connect() failed. ผมได้ยังงี้อ่ะ

Jaruwan
Reply to  sqre
7 years ago

เจอเหมือนกันเลยค่ะ

Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

sqre
sqre
9 years ago

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”);

thawat
8 years ago

ผมได้ 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

Krittima
Krittima
8 years ago

ขอบคุณมากนะคะ สำหรับ script ด้านบนค่ะ แต่จากที่ลองแล้ว ถ้าส่งด้วย gmail โดยใช้
$mail->Host = “smtp.gmail.com”;

ส่งไม่ได้ค่ะ อาจจะเกิดการ block ของ gmail เอง จึงเปลี่ยนเป็นส่งด้วย hotmail โดยใช้
$mail->Host = “smtp.live.com”;

ได้ผลเรียบร้อยดีค่ะ แต่มีปัญหาอยากจะปรึกษาว่าในส่วนข้อความที่จะส่ง ถ้าเรามีหลายบรรทัด ไม่ทราบว่าจะสามารถทำได้ไหมคะ เพราะพยายามใช้ “\r\n” แล้ว แต่ยังคงอยู่ที่บรรทัดเดียวค่ะ
$mail->msgHTML(“Test email by itoffside.com”);

ขอบคุณค่ะะ

Ekkaphon Saenphan
Ekkaphon Saenphan
8 years ago

ขอบคุณมากครับ เก็บไว้ศึกษา

song
song
7 years ago

Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
แก้ยังไงครับ

nobody
nobody
7 years ago

สอบถามหน่อยครับ พอดีต้องการส่งไฟล์แนบไปคับ แล้วชื่อไฟล์เป็นภาษาไทย.jpg มันไม่แนบไปด้วยครับ ใครพอมีทางแก้ได้ไหมครับ ***ถ้าใช้ชื่อไฟล์เป็นภาษาอังกฤษ ส่งไปได้นะครับ

nobody
nobody
Reply to  Tawatsak
7 years ago

แปลงยังไงครับ
$mypath=’myfile/ลานจอดรถสำหรับVIP.jpg’;
$mail->addAttachment($mypath, ‘ลานจอดรถสำหรับVIP’);

nobody
nobody
Reply to  nobody
7 years ago

ผมเซ็ท ค่านี้ไว้แล้วด้วยครับ
$mail->CharSet = ‘UTF-8’;

minmin
minmin
7 years ago

Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\followsales\PHPMailer\class.smtp.php on line 1212
แก้ยังไงอะคะ ช่วยบอกที

pisut
pisut
7 years ago

Mailer Error: Extension missing: openssl
ความว่าอย่างไงครับ

Arreeya Mokam
Arreeya Mokam
Reply to  Tawatsak
7 years ago

เออเร่อตรงนี้เหมือนกันค่ะ ต้องทำอย่างไรคะ

่jakrit
่jakrit
6 years ago

Warning: require(PHPMailer/PHPMailerAutoload.php): failed to open stream: No such file or directory in

ขึ้นอย่างงี้อะครับ

่jump
่jump
6 years ago

ขอโทษนะครับ ผมหาที่อยู่ไฟล์ PHPMailerAutoload.php ไม่เจออ่ะครับ จะเรียกใช้ก็ไม่ได้ ไฟล์มัน path ยังไง หรอครับ

วัชรพล คำน้อย
วัชรพล คำน้อย
6 years ago

PHPMailerAutoload.php
โหลดมาแล้วไฟล์นี้หายไปครับ

P
P
6 years ago

PHPMailer-master.zip ไม่มีไฟล์ PHPMailerAutoload.php นะครับ