การส่งเมลล์แบบ authentication นั้น มีไว้สำหรับโฮสบางโฮสที่เราไม่สามารถใช้ คำสั่ง mail() ได้ อาจจะขึ้น warning หรือ error ก็แล้วแต่ หรืออาจจะส่งได้แต่ไปลงที่ junkbox อะไรประมาณนี้ครับ
หรือที่เจอมาหมาดๆ คือ ทางโฮสได้ปิดคำสั่ง mail() ไปซะอย่างนั้น แล้วเราจะทำอย่างไรดี ในที่นี้ให้ลืมไปเลยครับ ที่ลูกค้าหรือผู้ชมเว็บไซต์จะส่งเมลล์กลับมาหาเมล์เดิมเรา เช่นเราอาจมีเมลเดิมเป็น sales@hotmail.com หรือไม่ก็ sales@yahoo.com เพราะในบทความนี้นั้น เราจะส่งเมลล์แบบส่งไปมาระหว่างเว็บเมลล์เราเองครับ
เช่นเว็บเราคือ
http://www.gurunumber.com/ เราก็สร้างบัญชีเมลล์จากเว็บเมลล์เราขึ้นมาเช่น sales@
gurunumber.com แล้วก็สร้างบัญชีผู้รับเป็น customer@
gurunumber.com ก็เป็นอันเสร็จครับ ส่วนโค๊ดนั้นคือ
<?php
function authMail($from, $namefrom, $to, $nameto, $subject, $message)
{
$smtpServer = "mail.gurunumber.com";
$port = "25";
$timeout = "30";
$username = "sales@gurunumber.com";
$password = "password"; // พาสเวริ์ดเมลล์
$localhost = "1.1.1.1"; // ไอพีของเว็บเมลล์
$newLine = "\r\n";
$secure = 0;?
$smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
$smtpResponse = fgets($smtpConnect, 4096);
if(empty($smtpConnect))
{
$output = "Failed to connect: $smtpResponse";
return $output;
}
else
{
$logArray['connection'] = "Connected to: $smtpResponse";
}
fputs($smtpConnect, "HELO $localhost". $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['heloresponse'] = "$smtpResponse";
if($secure)
{
fputs($smtpConnect, "STARTTLS". $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['tlsresponse'] = "$smtpResponse";
fputs($smtpConnect, "HELO $localhost". $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['heloresponse2'] = "$smtpResponse";
}
fputs($smtpConnect,"AUTH LOGIN" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authrequest'] = "$smtpResponse";
fputs($smtpConnect, base64_encode($username) . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authusername'] = "$smtpResponse";
fputs($smtpConnect, base64_encode($password) . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authpassword'] = "$smtpResponse";
fputs($smtpConnect, "MAIL FROM: $from" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['mailfromresponse'] = "$smtpResponse";
fputs($smtpConnect, "RCPT TO: $to" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['mailtoresponse'] = "$smtpResponse";
fputs($smtpConnect, "DATA" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['data1response'] = "$smtpResponse";
$headers = "MIME-Version: 1.0" . $newLine;
$headers .= "To: $nameto <$to>" . $newLine;
$headers .= "From: $namefrom <$from>" . $newLine;
fputs($smtpConnect, "To: $to\r\nFrom: $from\r\nSubject: $subject\r\n$headers\r\n\r\n$message\r\n.\r\n");
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['data2response'] = "$smtpResponse";
fputs($smtpConnect,"QUIT" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['quitresponse'] = "$smtpResponse";
$logArray['quitcode'] = substr($smtpResponse,0,3);
fclose($smtpConnect);
return($logArray);
}
?>
ส่วนวิธีการใช้งานนั้น ก็ไม่ยาก ใช้แทนคำสั่ง mail() เดิมครับ
เอาอันนี้ไปแทน
$from = "customer@gurunumber.com";
$namefrom = "suched"; // ชื่อยูสเซอร์ที่กรอกข้อมูลเพื่อส่งเมลล์มาให้เรา หรือชื่อลูกค้า
$to = "sales@gurunumber.com";
$nameto = "me"; // ชื่อเรา
$subject = "สวัสดีครับ ต้องการติดต่อคุณ ... sale";
$message = "ข้อความเนื้อหา";
authMail($from, $namefrom, $to, $nameto, $subject, $message);
แค่นี้เองครับ การใช้การรับส่งเมลล์แบบ authentication นั้นยังช่วยสร้างความปลอดภัยในระดับนืงให้กับโฮสอีกด้วยครับ