1

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

Ali Gohar
  • 51
  • 5
  • This might sound like a radical idea, but have you tried reading the error rmessage and using the link it gives you? – Synchro Feb 22 '16 at 10:35
  • Yes I did that. I also read the inside documentation also. – Ali Gohar Feb 22 '16 at 11:19
  • But you're still using an obsolete example and chose not to share the results of all the tests that the guide suggests that might help diagnose your question? – Synchro Feb 22 '16 at 11:58
  • I am receiving the same error http://itexsis.se/zia/abc/ in this link,, If you click on link the same error is still there. I changed so many scripts but nothing help – Ali Gohar Feb 22 '16 at 13:12

1 Answers1

-2

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

m2j
  • 1,152
  • 5
  • 18
  • 43