PHP的mail()函式可以實現直接用指令碼傳送郵件。
用mail()函式傳送郵件之前,首先需要在檔案裡面設定一下郵件服務屬性,主要的`設定選項如下:
屬性 預設值 說明 Changeable
SMTP “localhost” 只適用於Windows: SMTP伺服器的 DNS 名或者 IP地址 PHP_INI_ALL
smtp_port “25″ 只適用於Windows: SMTP 埠號 PHP_INI_ALL
sendmail_from NULL 只適用於Windows: 指定要傳送郵件的From地址 PHP_INI_ALL
sendmail_path NULL 只適用於Unix: 指定傳送郵件的程式路徑 PHP_INI_SYSTEM
如下程式碼實現了一個簡單的傳送郵件例子:
$to = ”;
$subject = “歡迎來到IT公司面試手冊”;
$message = “Hello! 歡迎來到IT公司面試手冊.”;
$from = ”;
$headers = “From: $from”;
mail($to,$subject,$message,$headers);
echo “Mail Sent.”;
?>