Home  >  Article  >  Backend Development  >  How to send and receive emails using PHP built-in functions?

How to send and receive emails using PHP built-in functions?

PHPz
PHPzOriginal
2024-04-22 14:24:02516browse

PHP built-in functions provide the functionality to send and receive emails. To send an email, you need to specify the recipient, email subject, email content and header information, and use the mail() function to send. To receive an email, you need to open a mailbox connection, get the message, and use the pop3_get_all() function to get all the messages. For more complex application scenarios, you can also send emails with attachments by specifying the multipart/mixed content type and attaching the file.

如何使用 PHP 内置函数发送和接收电子邮件?

How to send and receive emails using PHP built-in functions

PHP provides a series of built-in functions for sending and receiving emails to remote servers. This article will guide you on how to use these functions to build a basic email handler.

Send Email

<?php
// 设置邮件参数
$to = 'recipient@example.com';
$subject = 'Test Email';
$message = 'Hello there! This is a test email.';
$headers = "From: sender@example.com\r\n";
$headers .= "Reply-To: sender@example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/plain; charset=utf-8\r\n";

// 发送邮件
if (mail($to, $subject, $message, $headers)) {
  echo "Email sent successfully.";
} else {
  echo "Email could not be sent.";
}
?>

Receive Email

<?php
// 检查邮件
$mailbox = pop3_open('{pop.example.com:110}INBOX', 'username', 'password');

// 读取消息
$messages = pop3_get_all($mailbox);

// 输出消息
foreach ($messages as $message) {
  echo 'From: ' . $message['from'] . PHP_EOL;
  echo 'Subject: ' . $message['subject'] . PHP_EOL;
  echo 'Body: ' . $message['body'] . PHP_EOL;
  echo '-----------------------' . PHP_EOL;
}

// 关闭邮箱连接
pop3_close($mailbox);
?>

Practical Case

<?php
// 发送带有附件的电子邮件
$to = 'recipient@example.com';
$subject = 'Email with Attachment';
$message = 'Please find the attached document for your review.';
$attachment = 'document.pdf';
$headers = "From: sender@example.com\r\n";
$headers .= "Reply-To: sender@example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"==BOUNDARY==\"\r\n";
$headers .= "Content-Transfer-Encoding: 7bit\r\n";

// 准备邮件正文
$body = "This is a MIME encoded message.\r\n\r\n";
$body .= "--==BOUNDARY==\r\n";
$body .= "Content-Type: text/plain; charset=\"UTF-8\"\r\n";
$body .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$body .= $message . "\r\n";

// 附加文件
$body .= "--==BOUNDARY==\r\n";
$body .= "Content-Type: application/octet-stream; name=\"" . basename($attachment) . "\"\r\n";
$body .= "Content-Disposition: attachment\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode(file_get_contents($attachment))) . "\r\n";

// 发送电子邮件
if (mail($to, $subject, $body, $headers)) {
  echo "Email with attachment sent successfully.";
} else {
  echo "Email could not be sent.";
}
?>

The above is the detailed content of How to send and receive emails using PHP built-in functions?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn