Hàm thư PHP()
Ví dụ
Gửi một email đơn giản:
<?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);
?>
Định nghĩa và cách sử dụng
Hàm mail() cho phép bạn gửi email trực tiếp từ tập lệnh.
Cú pháp
mail( to,subject,message,headers,parameters );
Giá trị tham số
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) |
Chi tiết kỹ thuật
Giá trị trả về: | Trả về giá trị băm của tham số địa chỉ hoặc FALSE nếu thất bại. Lưu ý: Hãy nhớ rằng ngay cả khi email được chấp nhận gửi, điều đó KHÔNG có nghĩa là email thực sự được gửi và nhận! |
---|---|
Phiên bản PHP: | 4+ |
Nhật ký thay đổi PHP: | PHP 7.2: Tham số tiêu đề cũng chấp nhận một mảng PHP 5.4: Đã thêm tính năng bảo vệ chèn tiêu đề cho tham số tiêu đề . PHP 4.3.0: (Chỉ dành cho Windows) Tất cả các tiêu đề tùy chỉnh (như Từ, Cc, Bcc và Ngày) đều được hỗ trợ và không phân biệt chữ hoa chữ thường. PHP 4.2.3: Tham số tham số bị tắt ở chế độ an toàn PHP 4.0.5: Đã thêm tham số tham số |
Thêm ví dụ
Gửi email có tiêu đề bổ sung:
<?php
$to = " [email protected] ";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: [email protected] " . "\r\n" .
"CC: [email protected] ";
mail($to,$subject,$txt,$headers);
?>
Gửi email 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);
?>
❮ Hoàn thành tham chiếu thư PHP