[php] phpmailer error "Could not instantiate mail function"

I'm using the mail() basic example modified slightly for my user id and I'm getting the error "Mailer Error: Could not instantiate mail function"

if I use the mail function -

mail($to, $subject, $message, $headers);

it works fine, though I'm having trouble sending html, which is why I'm trying PHPMailer.

this is the code:

<?php
require_once('../class.phpmailer.php');

    $mail             = new PHPMailer(); // defaults to using php "mail()"
    $body             = file_get_contents('contents.html');
    $body             = eregi_replace("[\]",'',$body);
        print ($body ); // to verify that I got the html
    $mail->AddReplyTo("[email protected]","my name");
    $mail->SetFrom('[email protected]', 'my name');
    $address = "[email protected]";
    $mail->AddAddress($address, "her name");
    $mail->Subject    = "PHPMailer Test Subject via mail(), basic";
    $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!";
    $mail->MsgHTML($body);
    $mail->AddAttachment("images/phpmailer.gif");      // attachment
    $mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

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

This question is related to php email phpmailer

The answer is


The PHPMailer help docs on this specific error helped to get me on the right path.

What we found is that php.ini did not have the sendmail_path defined, so I added that with sendmail_path = /usr/sbin/sendmail -t -i;


For what it's worth I had this issue and had to go into cPanel where I saw the error message

"Attention! Please register your email IDs used in non-smtp mails through cpanel plugin. Unregistered email IDs will not be allowed in non-smtp emails sent through scripts. Go to Mail section and find "Registered Mail IDs" plugin in paper_lantern theme."

Registering the emails in cPanel (Register Mail IDs) and waiting 10 mins got mine to work.

Hope that helps someone.


I had this issue, and after doing some debugging, and searching I realized that the SERVER (Godaddy) can have issues.

I recommend you contact your Web hosting Provider and talk to them about Quota Restrictions on the mail function (They do this to prevent people doing spam bots or mass emailing (spam) ).

They may be able to advise you of their limits, and if you're exceeding them. You can also possibly upgrade limit by going to private server.

After talking with GoDaddy for 15 minutes the tech support was able to resolve this within 20 minutes.

This helped me out a lot, and I wanted to share it so if someone else comes across this they can try this method if all fails, or before they try anything else.


I was having this issue while sending files with regional characters in their names like: VeryRegiĆ³nal file - name.pdf.

The solution was to clear filename before attaching it to the email.


In my case, it was the attachment size limit that causes the issue. Check and increase the size limit of mail worked for me.


Check if sendmail is enabled, mostly if your server is provided by another company.


If you are sending file attachments and your code works for small attachments but fails for large attachments:

If you get the error "Could not instantiate mail function" error when you try to send large emails and your PHP error log contains the message "Cannot send message: Too big" then your mail transfer agent (sendmail, postfix, exim, etc) is refusing to deliver these emails.

The solution is to configure the MTA to allow larger attachments. But this is not always possible. The alternate solution is to use SMTP. You will need access to a SMTP server (and login credentials if your SMTP server requires authentication):

$mail             = new PHPMailer();
$mail->IsSMTP();                           // telling the class to use SMTP
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->Host       = "mail.yourdomain.com"; // set the SMTP server
$mail->Port       = 26;                    // set the SMTP port
$mail->Username   = "yourname@yourdomain"; // SMTP account username
$mail->Password   = "yourpassword";        // SMTP account password

PHPMailer defaults to using PHP mail() function which uses settings from php.ini which normally defaults to use sendmail (or something similar). In the above example we override the default behavior.


I had this issue as well. My solution was to disable selinux. I tried allowing 2 different http settings in selinux (something like httpd_allow_email and http_can_connect) and that didn't work, so I just disabled it completely and it started working.


This worked for me

$mail->SetFrom("[email protected]","my name", 0); //notice the third parameter

Your code looks good, did you forget to install PostFix on your server?

sudo apt-get install postfix

It worked for me ;)

Cheers


We nee to change the values of 'SMTP' in php.ini file php.ini file is located into

EasyPHP-DevServer-14.1VC11\binaries\php\php_runningversion\php.ini

My config: IIS + php7.4

I had the same issue as @Salman-A, where small attachments were emailed but large were causing the page to error out. I have increased file and attachments limits in php.ini, but this has made no difference.

Then I found a configuration in IIS(6.0), and increased file limits in there. iis config image

Also here is my mail.php:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require '../_PHPMailer-master/src/Exception.php';
require '../_PHPMailer-master/src/PHPMailer.php';

try{
    $email = new PHPMailer(true);
    $email->SetFrom('[email protected]', 'optional name');
    $email->isHTML(true);
    $email->Subject   = 'my subject';
    $email->Body      = $emailContent;
    $email->AddAddress( $eml_to );  
    $email->AddAttachment( $file_to_attach );
    $email->Send();
  }catch(phpmailerException $e){echo $e->errorMessage();}

I hope this helps someone in the future.


An old thread, but it may help someone like me. I resolved the issue by setting up SMTP server value to a legitimate value in PHP.ini


$mail->AddAddress($address, "her name");

should be changed to

$mail->AddAddress($address);

This worked for my case..


You need to make sure that your from address is a valid email account setup on that server.


Try using SMTP to send email:-

$mail->IsSMTP();
$mail->Host = "smtp.example.com";

// optional
// used only when SMTP requires authentication  
$mail->SMTPAuth = true;
$mail->Username = 'smtp_username';
$mail->Password = 'smtp_password';

Seems in my case it was just SERVER REJECTION. Please check your mail server log / smtp connection accessibility.


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to email

Monitoring the Full Disclosure mailinglist require(vendor/autoload.php): failed to open stream Failed to authenticate on SMTP server error using gmail Expected response code 220 but got code "", with message "" in Laravel How to to send mail using gmail in Laravel? Laravel Mail::send() sending to multiple to or bcc addresses Getting "The remote certificate is invalid according to the validation procedure" when SMTP server has a valid certificate How to validate an e-mail address in swift? PHP mail function doesn't complete sending of e-mail How to validate email id in angularJs using ng-pattern

Examples related to phpmailer

require(vendor/autoload.php): failed to open stream Fatal error: Class 'PHPMailer' not found PHPMailer - SMTP ERROR: Password command failed when send mail from my server Getting error while sending email through Gmail SMTP - "Please log in via your web browser and then try again. 534-5.7.14" Mail not sending with PHPMailer over SSL using SMTP sending email via php mail function goes to spam Unable to send email using Gmail SMTP server through PHPMailer, getting error: SMTP AUTH is required for message submission on port 587. How to fix? phpmailer - The following SMTP Error: Data not accepted Send File Attachment from Form Using phpMailer and PHP phpmailer: Reply using only "Reply To" address