Adokiye Iruene
Adokiye Iruene

Reputation: 870

MAILER ERROR: Could not Instantiate Mail Function, Php mailer

I am using php mailer to send a mailer to an email account on my domain, using the info gotten from the form elements, but it brings this error Could not Instantiate Mail Function, I have tried it on my xamp server and online too on my domain, It still doesn't work, Please what may I be doing wrong, Below is my code, is it because I am not using SMTP, please where may my error be

<?php 
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
if (isset($_POST['button'])) {
        $solution =     mailer($_POST['first_name'],$_POST['last_name'],$_POST['phone_number'],
        $_POST['ref_phone_number'], $_POST['type_of_vehicle'], $_POST['vehicle_model'],
        $_POST['vehicle_plate_no']) . "<br/>";
        echo $solution;   
}
function mailer($first_name, $last_name, $phone_number, $ref_phone_number,     $type_of_vehicle, $vehicle_model,$vehicle_plate_no){
require_once "vendor/autoload.php";

//PHPMailer Object
$mail = new PHPMailer;

//From email address and name
$mail->From = "[email protected]";
$mail->FromName = $first_name." ".$last_name;

//To address and name
$mail->addAddress("[email protected]"); //Recipient name is optional

//Address to which recipient will reply
$mail->addReplyTo("[email protected]", "Reply");

//Send HTML or Plain Text email


$mail->Subject = "Early Bird Subscription for ".$first_name." ".$last_name;
$message = '<html><body>';
$message .= '<table rules="all" style="border-color: #666;"  cellpadding="10">';
$message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td> <td>" . $first_name." ".$last_name . "</td></tr>";
$message .= "<tr><td><strong>Phone No:</strong> </td><td>" . $phone_number . "</td></tr>";
$message .= "<tr><td><strong>Referral Phone Number:</strong> </td><td>" .  $ref_phone_number . "</td></tr>";
$message .= "<tr><td><strong>Type of vehicle:</strong> </td><td>" .   $type_of_vehicle . "</td></tr>";
$message .= "<tr><td><strong>Vehicle Model:</strong> </td><td>" . $vehicle_model . "</td></tr>";
$message .= "<tr><td><strong>Vehicle Plate Number:</strong> </td><td>" . $vehicle_plate_no . "</td></tr>";
$mail->Body = $message;
$mail->isHTML(true);
// $mail->AltBody = "This is the plain text version of the email content";

if(!$mail->send()) 
{
return "Mailer Error: " . $mail->ErrorInfo;
} 
else 
{
return "Message has been sent successfully";
}
}
?>

<body>
    <h2>Early Bird Subscription</h2>
    <div class="row" style="margin-left: 10px;">
        <form name="mailer" method="post">
        <div class="form-group">
 <input type="text" name="first_name" class="form-control" placeholder="First Name" required>
</div>
<div class="form-group">
<input type="text" name="last_name" class="form-control" placeholder="Last Name" required>
     </div>
 <div class="form-group">
 <input type="number" name="phone_number" class="form-control" placeholder="Phone Number" required>
 </div>
 <div class="form-group">
 <input type="number" name="ref_phone_number" class="form-control"  placeholder="Referral Phone Number" required>
  </div><div class="form-group"><input type="text" name="type_of_vehicle" class="form-control" placeholder="Type of Vehicle" required>
  </div>
  <div class="form-group">
  <input type="text" name="vehicle_model" class="form-control" placeholder="Vehicle Model" required>
  </div>
  <div class="form-group">
  <input type="text" name="vehicle_plate_no" class="form-control" placeholder="Vehicle Plate Number" required>
   </div>
   <div class="form-group">
   <button type="submit" class="btn btn-primary btn-block btn-flat"    name="button">Sign Up</button>
   </div>           </form>
    </div>
  </body>

Upvotes: 1

Views: 1862

Answers (2)

Synchro
Synchro

Reputation: 37730

This is covered in the documentation and in many duplicate questions on here.

You are getting this error because you are sending using PHP’s built-in mail function (which is what PHPMailer uses by default), but you do not have a local mail server installed or configured. Install a mail server and it will be able to work.

Alternatively, use SMTP to connect to a remote mail server. See the many examples provided with PHPMailer for how to do that.

Upvotes: 1

Joseph_J
Joseph_J

Reputation: 3669

You have not included the PHPMailer class in your code.

Put this at the top of your code.

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'path/to/PHPMailer/src/Exception.php';
require 'path/to/PHPMailer/src/PHPMailer.php';
require 'path/to/PHPMailer/src/SMTP.php';

There is no need to use the autoloader for this method. Try it this way and see if it works. Then you can tinker around with the autoloader.

Upvotes: 0

Related Questions