我想使用phpmailer发送邮件。我已经在我的Godaddy服务器上上传了我的phpmailer文件。下面的代码在我的本地主机上运行,但不在我的服务器上运行。
2019-02-03 16:54:12 SMTP错误:无法连接到服务器:连接被拒绝(111)
以下是php的代码
<?php
include_once('PHPMailer/src/PHPMailer.php');
include_once('PHPMailer/src/SMTP.php');
$mail = new PHPMailer\PHPMailer\PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587,465
$mail->IsHTML(true);
$mail->Username = "mail@gmail.com";
$mail->Password = "password";
$mail->SetFrom("mail@gmail.com");
$mail->Subject = "Test mail";
$mail->Body = "Hello World";
$mail->AddAddress("mail@gmail.com");
if($mail->Send()) {
echo "Message has been sent";
}
?>
答案 0 :(得分:0)
use PHPMailer\PHPMailer\PHPMailer; <-- make sure these are not in a function
use PHPMailer\PHPMailer\Exception;
require 'path/src/Exception.php';
require 'path/src/PHPMailer.php';
require 'path/src/SMTP.php';
使用tls而不是ssl可能会引起错误
$mail->SMTPSecure = "tls";
$mail->Port = 587;
查看我的答案here,这将告诉您使用phpmailer连接gmail所需的一切