Home
Main Page

PHP Basic
Introduction
Syntax
Variables
Operators
If...Else
Switch
Arrays
Looping
Functions
Forms
GET
POST

PHP Advanced
Date
Include
File
File Upload
Cookies
Sessions
Mail
Secure Mail

MySQL Database
Introduction
Connect
Create
Insert
Select
Where
Order By
Update
Delete

PHP Database
ODBC

PHP Reference
PHP Manual

PHP Mail



คุณสามารถส่งเมล์โดยตรง ผ่านสคริปต์ PHP ได้เลย ด้วยฟังก์ชั่นข้างล่างนี้

mail(to,subject,message,headers)

  • to คือ อีเมล์ของผู้รับ
  • subject คือ หัวจดหมาย
  • message คือ ข้อความ
  • headers คือ ส่วนเพิ่มเติมจะมีหรือไม่มีก็ได้ เช่น Cc และ Bcc แต่ละส่วนจะแยกด้วย (\r\n)

    การส่งอีเมล์แบบง่ายๆ

    วิธีการส่งเมล์ที่ง่ายที่สุด คือส่งแบบ text ธรรมดา ดูตัวอย่างข้างล่างนี้

    <?php
    $to = "webmaster@phpstreet.com";
    $subject = "ทดสอบส่งอีเมล์";
    $message = "สวัสดีครับ นี่เป็นการลองส่งอีเมล์แบบ text";
    $from = "webmaster@domain.com";
    $cc = "info@domain.com";
    $bcc = "help@domain.com";
    
    $headers = "From: $from\r\n";
    $headers .= "Cc: $cc\r\n";
    $headers .= "Bcc: $bcc\r\n"; 
    
    mail($to,$subject,$message,$headers);
    echo "ส่งเมล์แล้ว";
    ?>

    ฟอร์มเมล์

    มาสร้างแบบฟอร์มส่งเมล์กัน โดยใช้ฟอร์ม html ทั่วไปคู่กับ PHP ตัวอย่างนี้จะสร้างแบบฟอร์มส่งค่าที่สำคัญไปเพื่อส่งเมล์ที่ไฟล์เดียวกัน

    ถ้ามีค่าจากฟอร์มส่งมา สคริปต์จะเช็คก่อนว่ามีค่าอีเมล์หรือไม่ ถ้ามี สคริปต์จะทำการส่งเมล์ให้ทันที แต่ถ้าไม่มีจะแสดงหน้าแบบฟอร์มแทน

    <html>
    <body>
    <?php
    if (isset($_REQUEST['email']))
      {
      $email = $_REQUEST['email'] ; 
      $subject = $_REQUEST['subject'] ;
      $message = $_REQUEST['message'] ;
      @mail( "webmaster@domain.com", $subject,
      $message, "From: $email" );
    
      echo "ขอบคุณที่ใช้ฟอร์มเมล์ของเรา";
      }
    else
      {
      echo "<form method='post' action=''>
      Email: <input name='email' type='text'><br>
      Subject: <input name='subject' type='text'><br>
      Message:<br>
      <textarea name='message' rows='15' cols='40'></textarea><br>
      <input type='submit'>
      </form>";
      }
    ?>
    </body>
    </html>
    TIP: ป้องกันความผิดพลาด เพื่อไม่ให้โชว์ข้อความ error ให้ใส่เครื่องหมาย "@" ไว้ที่หน้าบรรทัดนั้นๆ



    Related Sites
    php.net
    mysql
    phpMyAdmin
    apache
    thainuke
    microsoft
    linux
    PostgreSQL
    Zend Technologies
    CentOS

    Special Thanks
    ohohost.com
    w3schools.com

    Advertise




    Power By



     
    2464387







    Copyright 2006 by Phpstreet. All Rights Reserved.