简体   繁体   中英

I am receiving this error from phpmailer Message could not be sent.Mailer Error: SMTP connect() failed?

I am working on phpmailer but I am receiving this error, I dont know what is wrong. I search and did many changes. but noting helped me. I am testing both on live server and on localhost. on both I am receiving the error

Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

Please have a look on my code:

<?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';
}
?>

I have folder on name PHPMailer and mail.php is the file. I will appreciate your support because I am new to php Thank you in advance

open your cmd and type the following to get php-mailer ,

wget https://github.com/PHPMailer/PHPMailer/archive/master.zip

unzip the master. mention the file path clearly in your code.

Here

<?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';
}
?>

test it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM