How to implement the email receipt function through PHP?
With the widespread use of email, email receipts have become a requirement for many people when sending emails. Through the email receipt function, we can know in time whether the other party has received the email we sent. This article will introduce how to implement the email receipt function through PHP and provide specific code examples.
The core steps to implement the email receipt function are as follows:
The following is a simple example of a form to send an email:
<form action="send_email.php" method="post"> <label for="to">收件人:</label> <input type="email" id="to" name="to" required><br> <label for="subject">主题:</label> <input type="text" id="subject" name="subject" required><br> <label for="message">内容:</label><br> <textarea id="message" name="message" rows="4" cols="50" required></textarea><br> <input type="submit" value="发送"> </form>
First, we need to use PHP’s built-in mail function to send emails. The basic usage of the mail function is as follows:
mail(to, subject, message, additional_headers)
Among them, to is the recipient's email address, subject is the email subject, message is the email content, and additional_headers is optional additional email header information.
The following is a basic example of sending an email:
<?php $to = $_POST['to']; $subject = $_POST['subject']; $message = $_POST['message']; $headers = "From: webmaster@example.com" . " " . "Reply-To: webmaster@example.com" . " " . "X-Mailer: PHP/" . phpversion(); if (mail($to, $subject, $message, $headers)) { echo "邮件发送成功!"; } else { echo "邮件发送失败!"; } ?>
In this example, we obtain the recipient, subject and content in the form through the $_POST array, and use the mail function to send it mail. At the same time, we also set additional email header information, including the sender and reply address of the email.
$headers = "Disposition-Notification-To: webmaster@example.com" . " " . "Return-Receipt-To: webmaster@example.com" . " " . "X-Confirm-Reading-To: webmaster@example.com" . " " . "X-MS-Receipt: Success";
In this example, we added Disposition-Notification-To, Return-Receipt-To, X- Confirm-Reading-To and X-MS-Receipt four headers. When the recipient receives the email, they can choose whether to send a receipt email to inform the sender whether the email has been read.
$email_headers = imap_rfc822_parse_headers($email_data); $disposition_notification_to = $email_headers->headers['disposition-notification-to']; $return_receipt_to = $email_headers->headers['return-receipt-to']; $x_confirm_reading_to = $email_headers->headers['x-confirm-reading-to']; $x_ms_receipt = $email_headers->headers['x-ms-receipt'];
In this example, we use the imap_rfc822_parse_headers function provided by PHP to parse the additional header information and obtain the Disposition respectively. -The values of the four fields Notification-To, Return-Receipt-To, X-Confirm-Reading-To and X-MS-Receipt.
Through the above steps, we can implement the email receipt function through PHP. Of course, this is just a basic implementation method, and it can also be customized and expanded according to actual needs, such as adding receipt monitoring, saving receipt records, etc.
I hope this article can help you implement the email receipt function, thank you for reading!
The above is the detailed content of How to implement email receipt function through PHP?. For more information, please follow other related articles on the PHP Chinese website!