在CentOS上配置PHP邮件服务,通常需要以下几个步骤:
sudo yum install postfix
/etc/postfix/main.cf
,根据你的需求进行配置。例如,你可以设置系统邮件地址、监听接口等。这里是一个简单的示例配置:myhostname = mail.example.com
mydomain = example.com
myorigin = $mydomain
inet_interfaces = all
inet_protocols = ipv4
mydestination = $myhostname, localhost.$mydomain, $mydomain
mynetworks = 127.0.0.0/8, 192.168.0.0/16
home_mailbox = Maildir/
保存并退出编辑器,然后重启Postfix服务:
sudo systemctl restart postfix
sudo yum install php-mailx
send_email.php
,并使用PHPMailer发送邮件。首先,你需要引入PHPMailer类库:require 'path/to/PHPMailerAutoload.php';
然后,创建一个PHPMailer实例并配置相关设置:
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'localhost';
$mail->SMTPAuth = false;
$mail->Port = 25;
$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('recipient@example.com', 'Recipient Name');
$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';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
将from@example.com
和recipient@example.com
替换为实际的发件人和收件人邮箱地址。
php send_email.php
如果一切配置正确,你应该能够成功发送邮件。
注意:这里的示例仅用于演示目的。在实际生产环境中,你可能需要考虑更多的安全性和可靠性因素,例如使用SSL/TLS加密、配置SPF和DKIM记录等。