PHP 本身并不直接提供邮箱服务,但你可以使用 PHP 编写代码来与各种邮箱服务进行交互,或者使用 PHP 来发送和接收电子邮件。以下是一些常见的邮箱服务,你可以通过 PHP 与它们进行交互:
PHPMailer
或 SwiftMailer
库来发送邮件,或者使用 Gmail 的 IMAP/POP3 服务来接收邮件。PHPMailer
或 SwiftMailer
来发送邮件,或者使用 Outlook 的 IMAP/POP3 服务来接收邮件。<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'path/to/PHPMailer/src/Exception.php';
require 'path/to/PHPMailer/src/PHPMailer.php';
require 'path/to/PHPMailer/src/SMTP.php';
$mail = new PHPMailer(true);
try {
// 服务器设置
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com'; // 使用 Gmail 的 SMTP 服务器
$mail->SMTPAuth = true;
$mail->Username = 'your-email@gmail.com'; // 你的 Gmail 邮箱
$mail->Password = 'your-email-password'; // 你的 Gmail 密码
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
// 收件人
$mail->setFrom('your-email@gmail.com', 'Your Name');
$mail->addAddress('recipient-email@example.com', 'Recipient Name');
// 内容
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>
<?php
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'your-email@gmail.com';
$password = 'your-email-password';
$inbox = imap_open($hostname, $username, $password) or die('Cannot connect to Gmail: ' . imap_last_error());
$emails = imap_search($inbox, 'ALL');
if ($emails) {
rsort($emails);
foreach ($emails as $email_number) {
$overview = imap_fetch_overview($inbox, $email_number, 0);
$message = imap_fetchbody($inbox, $email_number, 1);
echo "Subject: " . $overview[0]->subject . "<br>";
echo "From: " . $overview[0]->from . "<br>";
echo "Message: " . $message . "<br><br>";
}
}
imap_close($inbox);
?>
通过这些方法,你可以使用 PHP 与各种邮箱服务进行交互,发送和接收电子邮件。