邮件错误:无法实例化邮件功能,PHP邮件

时间:2018-12-03 06:13:48

标签: php email phpmailer

我正在使用php mailer,使用从表单元素获取的信息将邮件发送到我域中的电子邮件帐户,但是它带来了此错误Could not Instantiate Mail Function,我已经在xamp服务器和在线服务器上尝试过在我的域中也是如此,它仍然无法正常工作,请我可能在做错什么,下面是我的代码,是因为我没有使用SMTP,请在哪里出现我的错误

<?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 = "earlybird@mendelsmore.com";
$mail->FromName = $first_name." ".$last_name;

//To address and name
$mail->addAddress("mendelsnzeh@mendelsmore.com"); //Recipient name is optional

//Address to which recipient will reply
$mail->addReplyTo("info@mendelsmore.com", "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>

2 个答案:

答案 0 :(得分:1)

这在文档中以及此处的许多重复问题中都有涉及。

您收到此错误,是因为您正在使用PHP的内置邮件功能(默认情况下PHPMailer使用PHP)进行发送,但是您没有安装或配置本地邮件服务器。安装邮件服务器,它将可以正常工作。

或者,使用SMTP连接到远程邮件服务器。有关如何执行此操作,请参见PHPMailer提供的许多示例。

答案 1 :(得分:0)

您尚未在代码中包含PHPMailer类。

将此代码放在代码顶部。

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

此方法无需使用自动加载器。尝试这种方式,看看是否可行。然后,您可以修改自动装带器。