關於php 用 mail 函數發送郵件
使用前有必要先看看文檔:
來自php手冊:https://www.php.net/manual/zh/ref.mail.php
windows 環境下, mail 函數 當前是發送不了郵件的!
所以嘛, 下面是關鍵步驟:
– 需要下載 sendmail.zip 這個插件包 http://glob.com.au/sendmail/ (記得解壓縮哈! 路徑後面是會用到的, 別藏得太深)
– 編輯 php.ini 配置文件中 【mail function】 相關配置項 (後面附截圖)
– 編輯 上面 sendmail 配置文件 sendmail.ini 中[sendmail] 配置項 (同上)
接下來就是看圖說話了
php.ini 配置項(改了配置文件記得重啓服務 ^0 ^)
sendmail.ini配置項:
; force the sender to always be the following email address
; this will only affect the "MAIL FROM" command, it won't modify
; the "From: " header of the message content
[email protected]
force_sender=發送郵箱 這個不寫 郵件可能會發送失敗, 查看錯誤日誌(error.log)
可能會遇到 Message is missing sender’s address 這樣的錯誤~!
授權碼
以qq郵箱爲例: 進入你的郵箱,在 設置 郵箱設置----賬戶–裏面找到 smtp 服務, 開啓----得到授權碼
下面是PHP 代碼:
<?php
### 給自己發一封郵件也是可以的
$to = '[email protected]';//收件人的郵箱, 可以寫自己的qq郵箱, 發給自己咯
$subject = '郵件測試!!!!';// 郵件標題
//$message = 'hello~!';//郵件內容
//$headers = 'MIME-Version: 1.0' . "\r\n";
//$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
//$headers = 'From: [email protected]';// From: xxxx.com 表示該郵件是來自誰的 可以省略, 省略後該郵件顯示爲代發郵件
//var_dump(mail($to, $subject, $message ,$headers));
var_dump(mail($to, $subject, null ,null));
如果省略了headers 裏面的 From: 內容, 郵件的發件人是空的, 郵件爲代發郵件
下面是發送成功的真相
額。。。待續~!