我正在使用phpmailer,但我收到此错误,我不知道有什么问题。我搜索并做了很多改变。但注意到了帮助我。 我在实时服务器和localhost上测试。 我都收到了错误
无法发送消息.Mailer错误:SMTP connect()失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
请查看我的代码:
<?php
require 'PHPMailer/PHPMailerAutoload.php';
require("PHPMailer/class.phpmailer.php"); // path to the PHPMailer class
$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 = 'stockholm85@gmail.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->From = 'agohar1985@gmail.com';
$mail->FromName = 'Mailer';
//$mail->addAddress('zia_gt@yahoo.com', 'Zia Ullah'); // Add a recipient
$mail->addAddress('zia_gt@yahoo.com'); // Name is optional
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
我在名字PHPMailer上有文件夹,mail.php是文件。 我将非常感谢您的支持,因为我是php新手 提前谢谢
答案 0 :(得分:-2)
打开cmd
并输入以下内容即可获得php-mailer
,
wget https://github.com/PHPMailer/PHPMailer/archive/master.zip
unzip
主人。在代码中清楚地提到file path
。
这里
<?php
require 'PHPMailer-master/PHPMailerAutoload.php'; //file path
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'user@gmail.com';
$mail->Password = '_password_';
$mail->SMTPSecure = 'tls';
$mail->From = 'sender@example.com';
$mail->FromName = 'Your Name';
$mail->addAddress('recipient@example.com');
$mail->isHTML(true);
$mail->Subject = 'Test Mail Subject!';
$mail->Body = 'This is SMTP Email Test';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
测试它