在 CentOS 系统中,要配置 PHP 的 mail 函数,你需要完成以下几个步骤:
sudo yum install postfix
/etc/postfix/main.cf 文件,根据你的需求进行配置。例如,你可以设置默认的域名、监听地址等。这里是一个简单的示例配置:myhostname = mail.example.com
inet_interfaces = all
myorigin = $myhostname
inet_protocols = ipv4
mydestination = $myhostname, localhost.$mydomain, $mydomain
mynetworks = 127.0.0.0/8, 192.168.0.0/16
home_mailbox = Maildir/
sudo systemctl start postfix
sudo systemctl enable postfix
sudo yum install php-mailx
/etc/php.ini 文件,找到 sendmail_path 设置项,将其设置为你的 MTA 的 sendmail 命令。对于 Postfix,这通常是 /usr/sbin/sendmail -t -i。例如:sendmail_path = /usr/sbin/sendmail -t -i
对于 PHP-FPM:
sudo systemctl restart php-fpm
对于 Apache:
sudo systemctl restart httpd
现在,你应该可以在 PHP 脚本中使用 mail 函数发送邮件了。这是一个简单的示例:
<?php
$to = "recipient@example.com";
$subject = "Test Email";
$message = "This is a test email sent from PHP.";
$headers = "From: sender@example.com";
if (mail($to, $subject, $message, $headers)) {
echo "Email sent successfully.";
} else {
echo "Email sending failed.";
}
?>
请确保将示例中的电子邮件地址替换为实际的收件人和发件人地址。