以下代码在我的xampp本地服务器上运行正常,但在远程主机上无法发送电子邮件。我遇到了这个错误:
“消息无法发送。发件人错误:SMTP连接()失败。”
“消息无法发送。发件人错误:SMTP连接()失败。”
require_once('header.php');
require_once('PHPMailer/PHPMailerAutoload.php');
function sendMail($address, $message){
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'mymail@gmail.com'; // SMTP username
$mail->Password = 'mypass'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->setFrom('mymail@gmail.com', 'ID Test'); // Add a recipient
$mail->addAddress($address); // Name is optional
//$mail->addReplyTo('info@example.com', 'Information');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');
$mail->isHTML(false); // Set email format to HTML
$mail->Subject = 'Twitter search';
$mail->Body = $message;
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
}