欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

phpmailer透過gmail發信?

程序员文章站 2022-04-26 15:18:22
...
請問如何透過gmail寄信?
我是使用虛擬主機(BLUEHOST)且申請它內建的電子郵件服務,然後加上phpmailer是可以寄信的。
只是我想改成透過gmail寄信
於是就失敗了
我參考這裡:http://*.com/ques...
$mail = new PHPMailer();
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; 
$mail->SMTPAuth = true; 
$mail->SMTPSecure = 'ssl'; 
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; 
$mail->IsHTML(true);
$mail->Username = "***@gmail.com";
$mail->Password = "***";
$mail->SetFrom("***@gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("xxxxxxxx@gmail.com");

 if(!$mail->Send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
 } else {
    echo "Message has been sent";
 }

跳出的錯誤

SMTP -> ERROR: Failed to connect to server: Connection timed out (110) 
The following From address failed: ***@gmail.com : Called Mail() without being connected Mailer Error: The following From address failed: ***@gmail.com : Called Mail() without being connected

確定gmail帳號密碼沒錯呀


看來確實bluehost不能用gmail

回复内容:

請問如何透過gmail寄信?
我是使用虛擬主機(BLUEHOST)且申請它內建的電子郵件服務,然後加上phpmailer是可以寄信的。
只是我想改成透過gmail寄信
於是就失敗了
我參考這裡:http://*.com/ques...

$mail = new PHPMailer();
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; 
$mail->SMTPAuth = true; 
$mail->SMTPSecure = 'ssl'; 
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; 
$mail->IsHTML(true);
$mail->Username = "***@gmail.com";
$mail->Password = "***";
$mail->SetFrom("***@gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("xxxxxxxx@gmail.com");

 if(!$mail->Send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
 } else {
    echo "Message has been sent";
 }

跳出的錯誤

SMTP -> ERROR: Failed to connect to server: Connection timed out (110) 
The following From address failed: ***@gmail.com : Called Mail() without being connected Mailer Error: The following From address failed: ***@gmail.com : Called Mail() without being connected

確定gmail帳號密碼沒錯呀


看來確實bluehost不能用gmail

提示连接超时啊,主机哪的啊,可以访问google服务么?

先用你的代码在本地测试,看是否有问题,如果没问题再咨询虚拟主机供应商。

错误信息上看,很大可能是代码所在服务器的网络根本不能访问gmail邮件服务器。

没连接到`$mail->Host = "smtp.gmail.com";`

通过smtp POP3或IMAP协议