Search code examples
phpsmtpgmailphpmailer

PHPMailer error: Could not instantiate mail function


I've looked through all the old posts on stackoverflow and many on other websites and find lots of posts with the same error, but none of the fixes work.

The following is the error I get:

Mailer Error: Could not instantiate mail function.

Is there something missing or incorrect?

<?php
require '../PHPMailer/PHPMailerAutoload.php';

//Create a new PHPMailer instance
$mail = new PHPMailer;

$mail->Host     = "smtp.gmail.com"; // SMTP server
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = "[email protected]";
$mail->Password = "censored";

$mail->setFrom('[email protected]');
$mail->addReplyTo('[email protected]');
$mail->addAddress('[email protected]');
$mail->Subject = 'PHPMailer mail() test';
$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));

$mail->AltBody = 'This is a plain-text message body';

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

Solution

  • I found what you're missing.

    You need to set these 2 things to get it to work properly:

    $mail->isSMTP();
    $mail->SMTPSecure = 'ssl';