ฟังก์ชันเมล PHP ()
ตัวอย่าง
ส่งอีเมลง่ายๆ:
<?php
// the message
$msg = "First line of text\nSecond line of text";
// use wordwrap() if lines are longer than 70 characters
$msg = wordwrap($msg,70);
// send email
mail("[email protected]","My subject",$msg);
?>
ความหมายและการใช้งาน
ฟังก์ชัน mail() ช่วยให้คุณสามารถส่งอีเมลได้โดยตรงจากสคริปต์
ไวยากรณ์
mail(to,subject,message,headers,parameters);
ค่าพารามิเตอร์
Parameter | Description |
---|---|
to | Required. Specifies the receiver / receivers of the email |
subject | Required. Specifies the subject of the email. Note: This parameter cannot contain any newline characters |
message | Required. Defines the message to be sent. Each line should be separated with a LF (\n).
Lines should not exceed 70 characters.
Windows note: If a full stop is found on the beginning of a line in the message, it might be removed. To solve this problem, replace the full stop with a double dot: |
headers | Optional. Specifies additional headers, like From, Cc, and Bcc. The additional headers should be separated with a CRLF (\r\n).
Note: When sending an email, it must contain a From header. This can be set with this parameter or in the php.ini file. |
parameters | Optional. Specifies an additional parameter to the sendmail program (the one defined in the sendmail_path configuration setting). (i.e. this can be used to set the envelope sender address when using sendmail with the -f sendmail option) |
รายละเอียดทางเทคนิค
คืนมูลค่า: | ส่งกลับค่าแฮชของพารามิเตอร์ที่อยู่ หรือ FALSE เมื่อล้มเหลว หมายเหตุ:โปรดทราบว่าแม้ว่าอีเมลจะได้รับการยอมรับสำหรับการจัดส่ง แต่ก็ไม่ได้หมายความว่าอีเมลนั้นส่งและรับจริง! |
---|---|
รุ่น PHP: | 4+ |
บันทึกการเปลี่ยนแปลง PHP: | PHP 7.2: พารามิเตอร์ส่วนหัวยังยอมรับอาร์เรย์ PHP 5.4: เพิ่มการป้องกันการฉีดส่วนหัวสำหรับ พารามิเตอร์ส่วนหัว PHP 4.3.0: (Windows เท่านั้น) รองรับส่วนหัวที่กำหนดเองทั้งหมด (เช่น From, Cc, Bcc และ Date) และไม่คำนึงถึงขนาดตัวพิมพ์ PHP 4.2.3: พารามิเตอร์พารามิเตอร์ถูกปิดใช้งานในเซฟโหมด PHP 4.0.5: เพิ่ม พารามิเตอร์พารามิเตอร์แล้ว |
ตัวอย่างเพิ่มเติม
ส่งอีเมลพร้อมส่วนหัวพิเศษ:
<?php
$to = "[email protected]";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: [email protected]" . "\r\n" .
"CC: [email protected]";
mail($to,$subject,$txt,$headers);
?>
ส่งอีเมล HTML:
<?php
$to = "[email protected], [email protected]";
$subject = "HTML email";
$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>This email contains HTML Tags!</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
</tr>
</table>
</body>
</html>
";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: <[email protected]>' . "\r\n";
$headers .= 'Cc: [email protected]' . "\r\n";
mail($to,$subject,$message,$headers);
?>
❮ อ้างอิงจดหมาย PHP ที่สมบูรณ์