輸入以下指令進行安裝套件
yum install -y httpd php postfix sendmail vim mail
接著將httpd開起來,並將以下程式碼分別貼到index.html以及test.php
service httpd start
index.html
<!DOCTYPE html> <html> <head> <title></title> </head> <body> <form method="post" action="test.php"> <input type="email" name="to"><br/> <input type="text" name="msg"><br/> <input type="submit"> </form> </body> </html>
test.php
<?php $to = $_POST["to"]; $msg = $_POST["msg"]; $subject = 'Hi root'; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=utf8' . "\r\n"; $headers .= 'From: cy@localhost.localdomain'; if(mail($to, $subject, $msg, $headers)){ echo "恭喜你寄信成功"; }else{ echo "寄信失敗,請確認使用者是否存在"; } ?>
並且輸入以下指令允許httpd可以傳送信件
setsebool httpd_can_sendmail on
接著就將資料填入測試看看吧
執行成功就會如下圖
執行失敗則如下圖
可以透過Mail套件來查看新的信件
參考資料:
http://php.net/manual/en/function.mail.php
http://stackoverflow.com/questions/8341678/php-mail-function-not-working-on-centos-server