ซื้อขายบ้านมือสอง

วันอังคารที่ 9 ตุลาคม พ.ศ. 2550

ทดสอบการส่ง e-mail





ทดสอบการส่ง e-mail


เมื่อเราต้องการส่งอีเมล์ เราต้องเรียกใช้งาน ฟังชั่น mail() ซึ่งถ้าเราใช้งานบนเครื่องของเราในระบบเครือข่าย จำเป็นจะต้องแก้ไขไฟล์ php.ini ดังนี้


[mail function]
; For Win32 only.
SMTP=localhost // ระบุ mail server
smtp_port = 25


; For Win32 only.


sendmail_from = me@example.com // ต้องระบุ ค่า default ว่าส่งจากใคร



รูปแบบฟังชั่น mail
mail(str to,str subject , str message , str header);


To คือ ข้อมูลที่ระบุ e-mail address ของผู้รับปลายทาง


Subject คือ ข้อมูลชนิด string ที่ระบุหัวเรื่องของ e-mail


Message คือ ข้อมูล ที่ใช้ระบุเนื้อหาข้อความต่างๆ ของ e-mail


Headers คือ ใช้ระบุรายละเอียดต่าง ของ header ซึ่งในทีนี้คือ header ของ protocol


Example : sendmail.php


<?php


$to = "nuijang20@hotmail.com";


$from ="php@signprosoft.com";


$subject = "ทดสอบการส่งเมล์แบบ HTML";



$html = "<html>";
$html .= "<head><title>ทดสอบเมล์แบบ HTML</title></head>";


$html .= "<body>";


$html .= "ทดสอบรูปภาพ <img src=http://www.msit.mut.ac.th/newweb/images/pimages/nui_hikari.jpg><br>";


$html .= "<table border=1>";


$html .= "<tr><td colspan=3>ข้อความในตาราง</td></tr>";


$html .= "<tr align=center><td>P</td><td>H</td><td>P</td></tr>";


$html .= "</table>";


$html .= "</body>";


$html .= "</html>";



/* สร้างเลขสุ่มขึ้นมา เพื่อใช้ในการกำหนดขอบเขตเนื้อหาของเมล์ */


$boundary = uniqid("");


/* กำหนดเฮดเดอร์ของเมล์ โดยเพิ่มข้อมูลที่บอกว่าอีเมล์ฉบับนี้ถูกส่งมาจากใคร, ข้อมูลที่บอกรูปแบบของเมล์ และขอบเขตเนื้อหาของเมล์ */


$header = "From: $from\n";


$header .= "Content-type: multipart/mixed; boundary=\"$boundary\"\n";



/* ตรงนี้เป็นส่วนสำคัญ โดยเราต้องกำหนด Content-type เป็น text/html เพราะเนื้อหาของเมล์ที่เราจะส่งออกไปเป็นแบบ HTML */


$body = "--$boundary\n";


$body .= "Content-type: text/html; charset=windows-874;\n";


$body .= "Content-disposition: inline;\n";


$body .= "Content-transfer-encoding: 8bit\n\n";


$body .= "$html\n"; //แทรกเนื้อหาแบบ HTML ไว้ตรงนี้


$body .= "--$boundary--\n";



if (mail($to, $subject, $body, $header)) { /* ดำเนินการส่งเมล์ */


echo "การส่งเมล์สำเร็จ ให้ลองเช็คเมล์ดู";


}


else {


echo "ไม่สามารถส่งเมล์ได้ กรุณาตรวจสอบ";


}


?>

1 ความคิดเห็น:

Unknown กล่าวว่า...

Many people cannot distinguish between the fake watches and other Rolex replica watches. Cellini Orchid Rolex replica watches have the decorative look of eighteen-carat yellow gold, a white pearl dial and diamonds to imitate the real Rolex watch. If you are one of those people who like a quartz and champagne dial and a Damier bracelet, then jazzy Cellini replica has this to offer you; its the best you can find.