1

i want to send an email from localhost to gmail, but it says "SMTP connect() failed" here is my code. I've also tried to change configuration php.ini and sendmail.ini but no luck. Thank you.

    $mail = new PHPMailer;

    $mail->isSMTP();       
    $mail->SMTPSecure = 'ssl'; 
    $mail->Host = 'smtp.gmail.com'; 
    $mail->Port = 465;
    $mail->SMTPAuth = true;                               
    $mail->Username = 'asd@gmail.com';               
    $mail->Password = 'password';                   

    $mail->From = 'from@example.com';
    $mail->FromName = 'Mailer';
    $mail->addAddress('asd@gmail.com');           

    $mail->WordWrap = 50;                            
    $mail->isHTML(true);                          

    $mail->Subject = 'Email'
    $mail->Body    = "This is body";

    if(!$mail->send()) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        echo 'Message has been sent';
    }
lie
  • 31
  • 4
  • possible duplicate of [Send email using the GMail SMTP server from a PHP page](http://stackoverflow.com/questions/712392/send-email-using-the-gmail-smtp-server-from-a-php-page) –  Aug 04 '14 at 06:38
  • Take a look at [this answer](http://stackoverflow.com/a/8629554/1864610) –  Aug 04 '14 at 06:39
  • There's nothing wrong with your code, those other answers won't help you. You should be using `Port = 587` amd `SMTPSecure = 'tls'`, but that's not the problem - you have a DNS or network issue preventing you from connecting, and the same thing would prevent any other SMTP solution from working. – Synchro Aug 04 '14 at 07:32

2 Answers2

1

Try out the following :

  • Ensure that the PHPMailer library is included in the code.
  • Ensure that you are internet connection from localhost is not inhibited by any proxy.

Happy Coding :)

0

check the line $mail->Subject = 'Email' for a missing ;

Jacko
  • 723
  • 1
  • 5
  • 8