插件窝 干货文章 php写的邮箱有哪些

php写的邮箱有哪些

使用 PHPMailer Mail IMAP 698    来源:    2025-03-16

PHP 本身并不直接提供邮箱服务,但你可以使用 PHP 编写代码来与各种邮箱服务进行交互,或者使用 PHP 来发送和接收电子邮件。以下是一些常见的邮箱服务,你可以通过 PHP 与它们进行交互:

  1. Gmail - 使用 PHP 的 PHPMailerSwiftMailer 库来发送邮件,或者使用 Gmail 的 IMAP/POP3 服务来接收邮件。
  2. Outlook/Hotmail - 同样可以使用 PHPMailerSwiftMailer 来发送邮件,或者使用 Outlook 的 IMAP/POP3 服务来接收邮件。
  3. Yahoo Mail - 可以使用 Yahoo 的 SMTP 服务器来发送邮件,或者使用 IMAP/POP3 来接收邮件。
  4. iCloud Mail - 可以使用 iCloud 的 SMTP 服务器来发送邮件,或者使用 IMAP 来接收邮件。
  5. Zoho Mail - Zoho 提供了自己的 SMTP 服务器和 IMAP/POP3 服务,可以通过 PHP 进行交互。
  6. ProtonMail - ProtonMail 提供了 SMTP 和 IMAP 服务,但需要通过 ProtonMail Bridge 来访问。
  7. 自定义邮箱服务 - 如果你有自己的邮件服务器(如 Postfix、Exim、Sendmail 等),你可以使用 PHP 直接与这些服务器进行交互。

使用 PHP 发送邮件的示例代码(使用 PHPMailer)

<?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 接收邮件的示例代码(使用 IMAP)

<?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);
?>

注意事项

  • 安全性:确保不要在代码中硬编码邮箱密码,使用环境变量或配置文件来管理敏感信息。
  • SMTP/IMAP 设置:不同的邮箱服务提供商有不同的 SMTP 和 IMAP 设置,确保你使用的是正确的服务器地址和端口。
  • 两步验证:如果启用了两步验证,可能需要生成应用专用密码来替代邮箱密码。

通过这些方法,你可以使用 PHP 与各种邮箱服务进行交互,发送和接收电子邮件。