1
votes

I'm trying to create a contact form. And I searched all over the internet how to send email, and they always say that it is better to use PHPMailer than mail() function.

The contact form consists of: Name, Email, Comments, and a Send Button.

I just followed this tutorial: https://alexwebdevelop.com/phpmailer-tutorial/ but it's not working as I always got the error called: 'Could not instantiate mail function.' when I already installed the Composer.

Proof that I've already installed the composer: Composer installed with autoload.php

Here's my code inside the php tags:

use PHPMailer\PHPMailer\PHPMailer;
require 'C:\xampp\composer\vendor\autoload.php';

if(isset($_POST['send'])) {
    $em = $_POST['email'];
    $nm = $_POST['name'];
    $msg = $_POST['comments'];

    $mail = new PHPMailer();
    $mail->setFrom($em, $nm);
    $mail->addAddress('[email protected]', 'Admin');
    $mail->Subject = 'Concern';
    $mail->isHTML(TRUE);
    $mail->Body = '$em';

    if(!$mail->send()) {
        echo $mail->ErrorInfo;
    }
}

Thank you for answering this question! It'll be a big help!

UPDATE:
Content/File of the composer.json:

{
 "require": {
  "phpmailer/phpmailer": "^6.0"
 }
}

3

3 Answers

0
votes

If you read the PHPMailer troubleshooting guide linked from nearly everywhere (including in your error message), you'll find it contains a detailed explanation for this error and how to solve it. Copying from there:

This means that your PHP installation is not configured to call the mail() function correctly (e.g. sendmail_path is not set correctly in your php.ini), or you have no local mail server installed and configured. To fix this you need to do one or more of these things:

  • Install a local mail server (e.g. postfix).
  • Ensure that your sendmail_path points at the sendmail binary (usually /usr/sbin/sendmail) in your php.ini. Note that on Ubuntu/Debian you may have multiple .ini files in /etc/php5/mods-available and possibly other locations.
  • Use isSendmail() and set the path to the sendmail binary in PHPMailer ($mail->Sendmail = '/usr/sbin/sendmail';).
  • Use isSMTP() and send directly using SMTP.

This is nothing to do with how you load composer's autoloader.

0
votes

Some hosting services return this error when the email is considered a spam. Try using an email address with the same domain as your website.

-1
votes

Instead of: require 'C:\xampp\composer\vendor\autoload.php';

Try: require __DIR__ . '/vendor/autoload.php';

This is assuming that /vendor is a sub directory of your document root.