วิธีส่งเมล์ด้วย PHP mail() ผ่าน SMTP Authentication
PHP June 1st, 2008
ส่งเมล์ด้วย PHP mail() ผ่าน SMTP Authentication
<?php
require_once "Mail.php";
$from = "Sandra Sender <sender@example.com>";
$to = "Ramona Recipient <recipient@example.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "mail.example.com";
$username = "smtp_username";
$password = "smtp_password";
$headers = array ('From' => $from, 'To' => $to, 'Subject' => $subject);
$smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => true, 'username' => $username, 'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo $mail->getMessage();
}
else {
echo "Message successfully sent!";
}
?>
ส่งเมล์ด้วย PHP mail() ผ่าน SMTP Authentication และ SSL
<?php
require_once "Mail.php";
$from = "Sandra Sender <sender@example.com>";
$to = "Ramona Recipient <recipient@example.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "ssl://mail.example.com";
$port = "465";
$username = "smtp_username";
$password = "smtp_password";
$headers = array ('From' => $from, 'To' => $to, 'Subject' => $subject);
$smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo $mail->getMessage();
}
else {
echo "Message successfully sent!";
}
?>
หมายเหตุ:
- วิธีนี้จำเป็นต้องใช้ PEAR Mail Package (Mail.php) ซึ่งปกติจะมาพร้อมกับ PHP4+ อยู่แล้วครับ เราไม่จำเป็นต้องเขียน script นี้ขึ้นมาเองครับ
- smtp_username / smtp_password คือ อีเมล์ account และรหัสผ่านของเมล์, host หมายถึง ค่า SMTP หรือ Outgoing Mail Server ของอีเมล์ที่เราใช้
About