0

I have an issue in sending mail from PHPMailer. When I try to send a message, I get an error message which is

Message could not be sent. Mailer Error:Could not instantiate mail function.

My SMTP is correct, I can't find the problem. Can anyone help me find the problem? Thanks

<?php

  require "autoload.php";           

  if(isset($_POST["submit"])){

    $mail = new PHPMailer(true);

    $sender = "demo@gmail.com";

    //SMTP
    $mail->Host = 'smtp.gmail.com';                       
    $mail->SMTPAuth = true;                    // Enable SMTP authentication
    $mail->Username = demo@gmail.com;          // SMTP username
    $mail->Password = '1234567';               // SMTP password
    $mail->SMTPSecure = 'tls';                 // Enable TLS 
    $mail->Port = 587;                         // TCP port to connect

    $mail->From = "demo@gmail.com";
    $mail->FromName = 'Mailer';
    $mail->setFrom(demo@gmail.com, 'Mailer');
    $mail->addAddress($_POST["receiver"]);     // Name is optional

    $mail->Subject = $_POST["subject"];
    $mail->Body    = $_POST["message"];

    if(!$mail->send()) {    
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        echo 'Message has been sent';
    }               
  }
?>
barbsan
  • 3,418
  • 11
  • 21
  • 28
slidejones55
  • 105
  • 1
  • 1
  • 5

2 Answers2

0

You have set various SMTP-related properties, but you have not told PHPMailer to use SMTP!

Add this line:

$mail->isSMTP();

You're also enabling exceptions (passing true to the constructor), but have not wrapped your code in a try/catch block. See the examples provided with PHPMailer for how to deal with that.

Synchro
  • 35,538
  • 15
  • 81
  • 104
0

please recheck your configuration like , user/password, PORT number Connect to smtp.gmail.com on port 465, if you're using SSL. Else use 587.

Also make sure less secure app is turn on https://support.google.com/accounts/answer/6010255?hl=en

Anoop P S
  • 754
  • 1
  • 12
  • 31