PHP傳送電子郵件
今天學習了PHP的mail函式,並且利用它傳送傳送一些簡單點的郵件,下面 開始吧:
1、首先,應該配置自己的PHP檔案(也就是php.ini檔案)然後需要安裝sendmail
2、下載後將其解壓到任意一個目錄下面,我的是和php相關檔案放在一起了(自己建立一個資料夾sendmail),比如放在 F:\AppServ\php6\sendmail下面
3、修改php.ini檔案如下:(這個檔案可以搜到,好像在C:windows下面)
sendmail_path = "F:\AppServ\php6\sendmail\sendmail.exe -t"修改為這樣
4、重啟Apache
5、用sendmail結合其他的smtp伺服器,如smtp.163.com來實現發郵件,現在大部分需要驗證,所以還需要配置一下sendmail.ini檔案,加入使用者名稱和密碼
6、修改php.ini檔案如下:
[mail function]
; For Win32 only.
SMTP = smtp.163.com
smtp_port = 25
; For Win32 only.
sendmail_from = [email protected]
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
sendmail_path = "F:\AppServ\php6\sendmail\sendmail.exe -t"
7、sendmail.ini 配置如下:
[sendmail]
smtp_server=smtp.163.com
; smtp port (normally 25)
smtp_port=25
smtp_ssl=auto
;default_domain=mydomain.com
error_logfile=error.log
;debug_logfile=debug.log
; if your smtp server requires authentication, modify the following two lines
auth_username=
auth_password=這裡寫你的163郵箱密碼。。。。
好!!配置資訊完成了,下面寫一段php程式碼吧:
建立EmailDemo.php檔案:
<html>
<?php
$name = "馬建傑";
$email = $_POST['email'];
$feedback = $_POST['feedback'];
$totaladdress = "[email protected]"; //收件地址
$subject = "反饋"; //郵件主題
$emailcontent = "顧客 名字:".$name."\n". //郵件內容
"顧客 email:".$email."\n".
"顧客評論:\n".$feedback."\n";
$fromaddress = "From: [email protected]";
mail($totaladdress,$subject,$emailcontent,$fromaddress); //傳送郵件
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>郵件傳送</title>
</head>
<body>
<h1>提交反饋</h1>
<p>你的反饋已經發送</p>
</body>
</html>
執行程式就OK啦!!