小弟我最近在做自家公司的網站遇到個問題, 希望客戶在"聯絡我們"頁面填完表單送出後, 能發一封mail到gmail信箱, 聽別人說phpmailer能利用gmail寄送郵件, 但是小弟我怎麼試都不成功, 請問有大大能幫我看一下我哪邊設定錯誤或是有什麼需要注意的地方嗎?


[contact.php]聯絡我們表單頁

<form id="form1" name="form1" method="post" action="sendmail.php">


<label>Name</label>
<input type="text" name="sendname" id="sendname">


<label>Email Address</label>
<input type="email" name="sendmail" id="sendmail">


<label>Phone Number</label>
<input type="tel" name="sendtel" id="sendtel">


<label>Message</label>
<textarea name="sendbody" id="sendbody"></textarea>


<button type="submit" name="button" id="button">Submit</button>


</form>



[sendmail.php]頁面

<?php
require 'PHPMailerAutoload.php'; // include("class.phpmailer.php");這個我也試過了
$Name=$_POST['sendname'];
$Mail=$_POST['sendmail'];
$Tel=$_POST['sendtel'];
$Sendbody=$_POST['sendbody'];
$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主機的埠號(Gmail為465)。
$mail->CharSet = "utf-8"; //郵件編碼
$mail->Username = "mymail@gmail.com"; //Gamil帳號
$mail->Password = "a123456"; //Gmail密碼
$mail->From = $Mail; //寄件者信箱
$mail->FromName = $Name; //寄件者姓名
$mail->Subject ="來自官網"; //郵件標題
$mail->Body = "姓名:".$Name."信箱:".$Mail."電話:".$Tel."回應內容:".$Sendbody; //郵件內容
$mail->IsHTML(true); //郵件內容為html ( true || false)
$mail->AddAddress("mymail@gmail.com"); //收件者郵件及名稱
if(!$mail->Send()) {
echo "Error: " . $mail->ErrorInfo;
} else {
echo "Thanks for your message , and we'll reply you soon !";
}
?>


class.phpmailer.php / class.pop3.php / class.smtp.php / PHPMailerAutoload.php 這4個檔也都丟到跟目錄下了~聯絡我們表單送出後出現 Error: SMTP connect() failed.
麻煩大大幫我解答~謝謝~感激不盡!!
Nicklaus Michaelson wrote:
$mail->Password = "a123456"; //Gmail 密碼

改用「應用程式密碼」再試試看。

參考一下:
http://www.mobile01.com/topicdetail.php?f=506&t=4030023

John wrote:
改用「應用程式密碼」...(恕刪)



還是不行...我傳到免費空間測試的~ http://www.000webhost.com/
試著多加上
$mail->SMTPDebug = 2;

看出錯後,能不能看到詳細的交握過程。
我那時是用範例做測試,有出錯,看得到交握過程,所以知道錯誤原因。建議出錯後,用檢視原始碼的方式看,不然在網頁上看,交握過程會連在一起。

John wrote:
試著多加上$mail...(恕刪)




2014-08-28 06:27:45 SMTP ERROR: Failed to connect to server: Connection timed out (110)
2014-08-28 06:27:45 SMTP connect() failed.
Error: SMTP connect() failed.


出現了這些訊息~~但是我不懂是什麼意思@@
openssl 有開嗎??

免費主機有些服務跟port會鎖
測起來會不是很正常
https://jin.tw
Nicklaus Michaelson wrote:
2014-08-28 06:27:45SMTP ERROR: Failed to connect to server: Connection timed out (110)
2014-08-28 06:27:45SMTP connect failed.
Error: SMTP connect failed.
出現了這些訊息~~ 但是我不懂是什麼意思 @@


這是指連接到伺服器逾時,可能原因就如同 Jinwei 所說的:
Jinwei wrote:
openssl 有開嗎??
免費主機有些服務跟 port 會鎖
測起來會不是很正常


建議你先確認一下你的免費空間有沒有支援 openssl

你可以把以下文字存成 test.php,再開 test.php 這網頁,就能知道 openssl 有沒有開。
<?php
phpinfo();
?>

如果只是測試性質的,你也可以在自己電腦架一個簡單 Server 當測試。
請問一下....你下載的檔案裡有class.phpmailer.php嗎?
為何我下載的...就是沒有這個檔案呢



下載的網址也是http://phpmailer.codeworxtech.com/ ........
不務正業 wrote:
請問一下.... 你下載的檔案裡有 class.phpmailer.php 嗎?
為何我下載的... 就是沒有這個檔案呢
下載的網址也是 http://phpmailer.codeworxtech.com/ ........


你下載的是 FE 或 for PHP4 的版本。那個檔案被包含在 for PHP5/PHP6 的版本裡。

差異性可看官網裡的這段說明:
If the PHPMailer class is found at the location you define, PHPMailer-FE will use the PHPMailer class to send the email(s). If the PHPMailer class is NOT found, PHP mail() will be used to send the email(s). Note: HTML formatting of sent emails is not available when using PHP mail().

http://phpmailer.worxware.com/index.php?pg=femanual#PHPMailer
文章分享
評分
評分
複製連結

今日熱門文章 網友點擊推薦!