การเขียนโค๊ดสำหรับส่ง email ใน php แบบง่ายด้วย php
- ฟังค์ชันในการทำงาน
1 |
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] ) |
- พารามิเตอร์
$to อีเมลล์ของผู้รับอีเมลล์เช่น
user@example.com
user@example.com, anotheruser@example.com
User <user@example.com>
User <user@example.com>, Another User <anotheruser@example.com>
$subject เรื่องหรือหัวข้อในการส่งอีเมลล์ ถึงผู้รับ
$message ข้อความที่ต้องการส่ง
- ตัวอย่างการใช้งาน
ตัวอย่างที่ 1 ส่งอีเมลล์แบบง่าย
1 2 3 4 5 6 7 8 9 10 |
<?php // The message $message = "Line 1\r\nLine 2\r\nLine 3"; // In case any of our lines are larger than 70 characters, we should use wordwrap() $message = wordwrap($message, 70, "\r\n"); // Send mail('caffeinated@example.com', 'My Subject', $message); ?> |
ตัวอย่างที่ 2 ส่งอีเมลล์โดยแสดงอีเมลล์ผู้ส่งสำหรับตอบกลับ
1 2 3 4 5 6 7 8 9 10 |
<?php $to = 'nobody@example.com'; $subject = 'the subject'; $message = 'hello'; $headers = 'From: webmaster@example.com' . "\r\n" . 'Reply-To: webmaster@example.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($to, $subject, $message, $headers); ?> |
ตัวอย่างที่ 3 ส่งอีเมลล์โดยแสดงอีเมลล์ผู้ส่งสำหรับตอบกลับแบบฝัง html โค๊ด
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<?php // Multiple recipients $to = 'johny@example.com, sally@example.com'; // note the comma // Subject $subject = 'Birthday Reminders for August'; // Message $message = ' <html> <head> <title>Birthday Reminders for August</title> </head> <body> <p>Here are the birthdays upcoming in August!</p> <table> <tr> <th>Person</th><th>Day</th><th>Month</th><th>Year</th> </tr> <tr> <td>Johny</td><td>10th</td><td>August</td><td>1970</td> </tr> <tr> <td>Sally</td><td>17th</td><td>August</td><td>1973</td> </tr> </table> </body> </html> '; // To send HTML mail, the Content-type header must be set $headers[] = 'MIME-Version: 1.0'; $headers[] = 'Content-type: text/html; charset=iso-8859-1'; // Additional headers $headers[] = 'To: Mary <mary@example.com>, Kelly <kelly@example.com>'; $headers[] = 'From: Birthday Reminder <birthday@example.com>'; $headers[] = 'Cc: birthdayarchive@example.com'; $headers[] = 'Bcc: birthdaycheck@example.com'; // Mail it mail($to, $subject, $message, implode("\r\n", $headers)); ?> |
อ้างอิง php.net
0 Comments