===================================================
在 PHP 當中可以使用 mail() 來發信,但前提是必須是一個不用驗證的SMTP
但是如果需要帳號/密碼通過SMTP,就需要額外 OpenSource,在此使用 PHPMailer
並且依照您所使用的 PHP 版本下載,下載後壓縮到您所執行的資料夾下
在主程式 include 以下檔案 : class.phpmailer.php
另外而在 Gmail 寄信中因為會用到 OpenSSL 連線
所以要將 php.ini 中的 extension=php_openssl.dll 前面的分號(;)拿掉
以下Yu小編來個小範例 :
您只需要更改12,13行的gmail帳號以及密碼就可完成送信程序。
<?php
include("PHPMailer/class.phpmailer.php"); //匯入PHPMailer類別
$mail= new PHPMailer(); //建立新物件
$mail->IsSMTP(); //設定使用SMTP方式寄信
$mail->SMTPAuth = true; //設定SMTP需要驗證
$mail->SMTPSecure = "ssl"; // Gmail的SMTP主機需要使用SSL連線
$mail->Host = "smtp.gmail.com"; //Gamil的SMTP主機
$mail->Port = 465; //Gamil的SMTP主機的SMTP埠位為465埠。
$mail->CharSet = "big5"; //設定郵件編碼
$mail->Username = "*********"; //Gmail帳號
$mail->Password = "*********"; //Gmail密碼
$mail->From = "yutoukoko@gmail.com"; //設定寄件者信箱
$mail->FromName = "Yu小編"; //設定寄件者姓名
$mail->Subject = "PHPMailer 測試信件"; //設定郵件標題
$mail->Body = "大家好,這是一封測試信件! "; //設定郵件內容
$mail->IsHTML(true); //設定郵件內容為HTML
$mail->AddAddress("yutoukoko@gmail.com", "Yu小編"); //設定收件者郵件及名稱
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
else {
echo "Message sent!";
}
?>
這樣就可完成 Gmail 發信了,而要注意的是Gmail 的 SMTP主機位址是 smtp.gmail.com
所以必須使用 465 埠以 SSL 的方式來連線
希望對您有所幫助。
不好意思,我在我的php.in中沒有看到extension=php_openssl.dll 這一行的說?
抱歉,我找的php.ini為之前刪除的,已在新的php.ini中找到了。
謝謝!
成功就好~
有任何疑問歡迎在提出唷^^
Warning: main(PHPMailer/class.phpmailer.php) [function.main]: failed to open stream: No such file or directory in C:\Apache2\htdocs\phpmailer\gmailsmtp.php on line 2
Warning: main() [function.include]: Failed opening ‘PHPMailer/class.phpmailer.php’ for inclusion (include_path=’.;C:\php5\pear’) in C:\Apache2\htdocs\phpmailer\gmailsmtp.php on line 2
Fatal error: Class ‘PHPMailer’ not found in C:\Apache2\htdocs\phpmailer\gmailsmtp.php on line 4
你在include PHPMailer的時候記得路徑要正確,看錯誤訊息應該是資料夾名稱錯誤,改正應該就可以執行了
請教一下我 php.ini中extension=php_openssl.dll 前面的分號(;)有拿掉,也有重新啟動服務,那請問以下錯誤訊息是哪裡忘了設定呢???謝謝!
Warning: fsockopen() [function.fsockopen]: unable to connect to ssl://smtp.gmail.com:465 (Unable to find the socket transport “ssl" – did you forget to enable it when you configured PHP?) in D:\www\mysite\phpmailer\class.smtp.php on line 122
可以送信了:) … 我複製 ssleay32.dll 及 libeay32.dll 到c:\windows\system32下就OK了~~
不好意思!請問我出現以下訊息~表示哪邊有錯誤呀?>"<
SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host.
對不起,方便請教一下…
為什麼我都照做了還會出現這樣子的訊息呢?
Strict Standards: date() [function.date]: It is not safe to rely on the system’s timezone settings. Please use the date.timezone setting, the TZ environment variable or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected ‘UTC’ for ’8.0/no DST’ instead in C:\AppServ\www\class.phpmailer.php on line 1925
Strict Standards: date() [function.date]: It is not safe to rely on the system’s timezone settings. Please use the date.timezone setting, the TZ environment variable or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected ‘UTC’ for ’8.0/no DST’ instead in C:\AppServ\www\class.phpmailer.php on line 1929
SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host.