mail() 函数是PHP内置的邮件发送函数,允许直接从脚本发送电子邮件。它是PHP核心的一部分,无需额外安装扩展即可使用。
bool mail(
string $to,
string $subject,
string $message,
string $additional_headers = "",
string $additional_parameters = ""
)
<?php
// 基本邮件发送
$to = "recipient@example.com";
$subject = "测试邮件";
$message = "这是一封测试邮件!";
$headers = "From: sender@example.com\r\n";
if(mail($to, $subject, $message, $headers)) {
echo "邮件发送成功!";
} else {
echo "邮件发送失败!";
}
?>
<?php
$to = "recipient@example.com";
$subject = "HTML格式邮件测试";
$message = '
<html>
<head>
<title>HTML邮件</title>
</head>
<body>
<h1 style="color: #3498db;">欢迎!</h1>
<p>这是一封<strong>HTML格式</strong>的测试邮件。</p>
</body>
</html>
';
// 设置Content-type为HTML
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";
$headers .= "From: sender@example.com\r\n";
$headers .= "Reply-To: reply@example.com\r\n";
mail($to, $subject, $message, $headers);
?>
<?php
function sendMailWithAttachment($to, $subject, $message, $filePath) {
$boundary = md5(time());
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: sender@example.com\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\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";
// 附件部分
$fileContent = file_get_contents($filePath);
$fileContent = chunk_split(base64_encode($fileContent));
$fileName = basename($filePath);
$body .= "--$boundary\r\n";
$body .= "Content-Type: application/octet-stream; name=\"$fileName\"\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n";
$body .= "Content-Disposition: attachment; filename=\"$fileName\"\r\n\r\n";
$body .= $fileContent . "\r\n";
$body .= "--$boundary--";
return mail($to, $subject, $body, $headers);
}
?>
要使mail()函数正常工作,需要满足以下条件:
| 配置项 | 默认值 | 描述 | PHP版本 |
|---|---|---|---|
| SMTP | "localhost" | Windows系统:SMTP服务器地址 | 所有版本 |
| smtp_port | 25 | SMTP端口号(Windows) | PHP 4.3+ |
| sendmail_from | NULL | 默认发件人地址(Windows) | 所有版本 |
| sendmail_path | /usr/sbin/sendmail | Unix系统:sendmail路径 | 所有版本 |
| mail.add_x_header | 0 | 是否添加X-PHP-Originating-Script头(PHP 7.2+) | PHP 7.2+ |
| mail.log | NULL | 邮件日志文件路径(PHP 7.2+) | PHP 7.2+ |
; php.ini 邮件配置
[mail function]
; Windows 配置
SMTP = smtp.gmail.com
smtp_port = 587
sendmail_from = your-email@gmail.com
; Unix/Linux 配置
sendmail_path = "/usr/sbin/sendmail -t -i"
; PHP 7+ 新增配置
mail.add_x_header = On
mail.log = /var/log/phpmail.log
mail.add_x_header 配置选项mail.log 配置选项,便于调试mail() 函数参数类型更加严格可能原因及解决方法:
error_log = /var/log/php_errors.logerror_get_last()获取详细错误信息解决方案:
对于生产环境,建议使用成熟的邮件库:
| 库名称 | 特点 | GitHub Stars | PHP版本要求 |
|---|---|---|---|
| PHPMailer | 功能全面,支持SMTP、HTML邮件、附件 | 19k+ | PHP 5.5+ |
| SwiftMailer | Symfony框架推荐,面向对象设计 | 9k+ | PHP 7.0+ |
| Symfony Mailer | Symfony 4.3+ 内置,现代API | - | PHP 7.2.5+ |
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'your-email@gmail.com';
$mail->Password = 'your-password';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
$mail->setFrom('from@example.com', '发件人');
$mail->addAddress('to@example.com', '收件人');
$mail->isHTML(true);
$mail->Subject = 'PHPMailer测试';
$mail->Body = '<b>HTML内容</b>';
$mail->send();
echo '邮件已发送';
} catch (Exception $e) {
echo "发送失败: {$mail->ErrorInfo}";
}
?>
| 函数 | 描述 | PHP版本 |
|---|---|---|
| mail() | 发送电子邮件 | PHP 4+ |
ezmlm_hash() |
计算EZMLM邮件列表系统所需的散列值(已弃用) | PHP 3-7.4 |
掌握PHP邮件发送是现代Web开发的重要技能。建议从原生mail()函数开始学习,理解基本原理后转向专业的邮件库。在实际项目中,务必考虑邮件发送的可靠性、安全性和性能。