iT邦幫忙

0

使用phpmailer SMTP連線失敗

翗祤 2021-08-26 16:05:3516289 瀏覽
  • 分享至 

  • xImage

各位前輩們好,最近剛學習php,嘗試使用phpmailer寄發信件,卻遇上SMTP connect() failed的問題。

已經有至gmail帳戶的地方開啟「低安全性應用程式存取權」,在本地端測試寄信是沒問題的,但是當我把檔案丟上000webhost平台再進行寄發信件就會跳出SMTP connect() failed/images/emoticon/emoticon02.gif

請問該如何解決?

2021/08/27補

  1. <用平台開phpinfo()之後的SMTP設定>
    SMTP: localhost / localhost
    smtp_port: 25 / 25

  2. 我有嘗試這個連結的做法
    但是在extract item那一步出現「 ftp_put(): Can't open that file: No such file or directory」的問題

// 如果發問有哪裡不清楚的話可以再補

看更多先前的討論...收起先前的討論...
port你設多少?25?465?
翗祤 iT邦新手 5 級 ‧ 2021-08-26 16:58:13 檢舉
是465喔!
翗祤 iT邦新手 5 級 ‧ 2021-08-26 17:01:40 檢舉
我只有在寄發信件的那支php裡面設定port,還是000webhost也要設定呢?
haward79 iT邦研究生 2 級 ‧ 2021-08-27 06:23:10 檢舉
用 phpmailer 存取 gmail 時,你有使用「應用程式密碼」嗎?
https://support.google.com/accounts/answer/185833?hl=zh-Hant
翗祤 iT邦新手 5 級 ‧ 2021-08-27 08:20:50 檢舉
如果是下面這樣的話有喔!
因為本地端可以寄信,所以登入上應該沒問題才對(思考
$mail->Username = "xxxxxx@gmail.com"; // Gamil帳號
$mail->Password = "xxxxxxx"; // Gmail密碼
迷路 iT邦新手 2 級 ‧ 2021-08-27 08:31:17 檢舉
平台有提供SMTP功能嗎?
用phpinfo()查看SMTP的相關設定看看
翗祤 iT邦新手 5 級 ‧ 2021-08-27 08:57:29 檢舉
補在文章內了
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
0

先PO一下你的 phpmailer 設定檔 (帳密可以用 user 替代)

翗祤 iT邦新手 5 級 ‧ 2021-08-27 08:17:25 檢舉

大大是指這個嗎?(這是參考其他人寫的格式

    </script>
</head>
<body>
<?php
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;
    require 'vendor/autoload.php';
    $C_name=$_POST['C_name'];
    $C_email=$_POST['C_email'];
    $C_message=$_POST['C_message'];


    $mail= new PHPMailer();                          
    $mail->IsSMTP();                                    
    $mail->SMTPAuth = true;                       
    $mail->SMTPSecure = "ssl";                    
    $mail->Host = "smtp.gmail.com";             
    $mail->Port = 465;                                 
    $mail->CharSet = "utf-8";                      
    $mail->Username = "xxx@gmail.com"; 
    $mail->Password = "xxxxxx";                
    $mail->From = "xxx@gmail.com";        
    $mail->FromName = "xxx";                  
    $mail->Subject ="來自<".$C_name.">的留言"; 
    $mail->Body = "姓名:".$C_name."<br>信箱:".$C_email."<br>回應內容:".$C_message; 
    $mail->IsHTML(true);                             
    $mail->AddAddress("$C_email");            
    if(!$mail->Send()){
        echo "Error: " . $mail->ErrorInfo;
    }else{
        echo "5秒後進行頁面跳轉……";
    }

?>
</body>

試著將 PHPMailer/SMTP 也use 進去試試。
我自已是固定這些都會USE

PHPMailer
SMTP
Exception
OAuth

不過其實你的錯誤因該不是這個問題才對。
看你的發信程式,也沒啥問題。
或許先檢查一下是不是465 PORT發不出去好了。
搞不好不是程式問題。

翗祤 iT邦新手 5 級 ‧ 2021-08-27 09:20:40 檢舉

USE了這三個,剛剛嘗試發信還是一樣/images/emoticon/emoticon06.gif
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;

0
海綿寶寶
iT邦大神 1 級 ‧ 2021-08-27 10:17:20

我有嘗試這個連結的做法

請先完全照抄範例
只改必要的地方(eg.帳密,寄收件者)
如果成功了
再逐漸改成你要的結果

以下是我看到的差異處之一
port 由 465 改成 587

$mail->Port = 587; // TLS only
$mail->SMTPSecure = 'tls'; // ssl is deprecated
看更多先前的回應...收起先前的回應...
翗祤 iT邦新手 5 級 ‧ 2021-08-27 11:34:48 檢舉

port跟tls的部分都改了目前還是一樣狀況
上面的連結卡在extract item那一步出現「 ftp_put(): Can't open that file: No such file or directory」

我不懂你的 extract item 是指
1.程式的那一段
2.extract phpmailer.zip

看起來「ftp_put():Can't ....」這訊息跟 smtp 扯不上什麼關係...

翗祤 iT邦新手 5 級 ‧ 2021-08-27 11:51:20 檢舉

圖片中這一步,依照他的改好然後按右下角extract之後就跑出 ftp_put(): Can't open that file: No such file or directory的訊息不給過,也不確定這步跳過沒做會不會影響到後面,所以就沒有往下做了
https://www.000webhost.com/forum/uploads/default/original/3X/0/9/09121b80ec5c86456f3e33e30531bdb54af1404f.png

翗祤 iT邦新手 5 級 ‧ 2021-08-27 12:02:22 檢舉

phpmailer的設定檔是照著網上其他人的做法去改我需要的部分
在本地端測試是成功發信且可以收到的
但是丟上000webhost平台再測試就無法發送
所以在想是哪個部分沒設定好還是單純這個平台不能使用這個功能

這動作是「安裝 phpmailer」,而且是必要的
看來
大家都誤會你的問題了
/images/emoticon/emoticon10.gif

0
japhenchen
iT邦超人 1 級 ‧ 2021-08-27 10:59:24

都有人跟你說要用465 port,還要開啟TLS支援(SSL不能用了)

怎麼還在25?

看更多先前的回應...收起先前的回應...
翗祤 iT邦新手 5 級 ‧ 2021-08-27 11:23:29 檢舉

phpmailer設定檔是用465 port下去設定沒錯,不知道為甚麼SMTP那邊還是顯示25,還是還有哪邊要手動改的呢?

是虛擬空間主機嗎?(主機在ISP手上,你只能用PHP這種的)

$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Host = "smtp.gmail.com";
$mail->Mailer = "smtp";
$mail->Port = 465;
$mail->Username = "YOU@gmail.com";
$mail->Password = "YOUR_GMAIL_password";
翗祤 iT邦新手 5 級 ‧ 2021-08-27 11:56:04 檢舉

是虛擬空間主機嗎? ---> 這個我太菜看不懂
剛剛照樓上說得把ssl跟465改成tls跟587去試了還是一樣
所以是要換成tls跟465嗎?

試試不會中27億 ..

我要發表回答

立即登入回答