โดย ฟังก์ชั่นนี้เป็น ฟังก์ชั่น ที่ใช้ในการส่ง Email จาก hosting ของเรานะครับ แต่ใช้ username กับ paasword ของ Google App ซึ้งปัจจุบัน หลายเว็บก็นิยมใช้ Google App กันหมดแล้ว
โดยตัวอย่างนี้ผม ทำการเขียนคำอธิบายไว้ สามารถนำไปประยุกต์ใช้ต่อกันได้เล้ยยย โดยทดสอบบน Hosting ของผมแล้ว ส่งออกจริง ไรจริง เอาไปใช้ได้เล้ยยยยยย
โค้ด: เลือกทั้งหมด
<?php
require_once('class.phpmailer.php');
function sendmail($to,$subject,$detail){
$mail = new PHPMailer(true); // ใช้ร่วมกับ PHPMailer
$mail->CharSet = "UTF-8";
$mail->IsSMTP(); // ใช้ SMTP ในการส่ง
try {
$mail->Host = "mail.YOURDOMAIN.com"; // SMTP server
$mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "[email protected]"; // GMAIL username
$mail->Password = "MAIL_GOOGLE_APP_PASSWORD"; // GMAIL password
$mail->AddReplyTo('[email protected]', 'REPLY_TO'); //ได้เมลล์แล้วตอบกลับถึงใคร
$mail->AddAddress($to, ''); //ถึงใคร
$mail->AddBcc('[email protected]', 'BCC'); //สำเนาลับถึงใคร
$mail->SetFrom('[email protected]', 'SEND_FORM'); //ใครเป็นคนส่ง
//' $mail->AddReplyTo('[email protected]', 'D');
$mail->Subject = $subject;
$mail->MsgHTML($detail);
$mail->Send();
//echo "Message Sent OK\n";
} catch (phpmailerException $e) {
//echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
//echo $e->getMessage(); //Boring error messages from anything else!
}
}
sendmail("[email protected]","ทดสอบการส่ง Email","<hr>ทดสอบ Body HTML <br><hr>");
?>