53

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("reply@example.com","my name");
    $mail->SetFrom('from@example.com', 'my name');
    $address = "to@example.com";
    $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!";
    }
?>
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
sdfor
  • 6,324
  • 13
  • 51
  • 61
  • 1
    Similar question: http://stackoverflow.com/questions/1944631/could-not-instantiate-mail-function-why-this-error-occuring – Mukesh Chapagain Aug 22 '11 at 08:26
  • Than mean PHPmailer try to use build in mail function from PHP not SMTP – user956584 Aug 25 '15 at 19:55
  • 1
    For me it was caused by server problem, I've got a segmentation fault error message via SSH while try to update system (apt-get update), so I reboot VPS and PHPMailer works fine. – Harkály Gergő Jan 16 '17 at 00:22

18 Answers18

37

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';
Mukesh Chapagain
  • 25,063
  • 15
  • 119
  • 120
22

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

sudo apt-get install postfix

It worked for me ;)

Cheers

Andrew
  • 18,680
  • 13
  • 103
  • 118
Irwuin
  • 523
  • 3
  • 9
16

This worked for me

$mail->SetFrom("from@domain.co","my name", 0); //notice the third parameter
Matías Cánepa
  • 5,770
  • 4
  • 57
  • 97
  • 2
    Thank you! For any one who's wondered why, here explains the third parameter: http://develop.wp-a2z.org/oik_api/phpmailersetfrom/ – Walty Yeung Aug 14 '17 at 23:43
  • 1
    Wow. I didn't expect this would fix it. @WaltyYeung sadly your link is down. I am still interested in why that works. – miile7 Apr 29 '21 at 20:39
15
$mail->AddAddress($address, "her name");

should be changed to

$mail->AddAddress($address);

This worked for my case..

Avinash
  • 6,064
  • 15
  • 62
  • 95
  • 2
    whats the problem with name? – Peyman abdollahy May 29 '15 at 17:56
  • @Peymanabdollahy really hard to recall after 4 years. I will check and update you. – Avinash May 29 '15 at 18:03
  • See [`addAddress(string $address, string $name = '') : boolean`](https://phpmailer.github.io/PHPMailer/classes/PHPMailer.PHPMailer.PHPMailer.html#method_addAddress) – showdev Feb 04 '21 at 10:24
  • @showdev your link 404s :( it would be good to preserve the answer here, too. – Frederick Nord Nov 23 '22 at 13:58
  • @FrederickNord Thanks for letting me know! Here is an updated link to documentation: [addAddress()](https://phpmailer.github.io/PHPMailer/classes/PHPMailer-PHPMailer-PHPMailer.html#method_addAddress). – showdev Nov 24 '22 at 02:57
12

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

D.F.
  • 185
  • 1
  • 6
12

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.example.com";    // set the SMTP server
$mail->Port       = 26;                    // set the SMTP port
$mail->Username   = "johndoe@example.com"; // SMTP account username
$mail->Password   = "********";            // 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.

Salman A
  • 262,204
  • 82
  • 430
  • 521
5

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;

Matt Pope
  • 167
  • 2
  • 11
  • For the information of anyone wondering what the `-t -i` is doing there, here's a list of sendmail options http://www.postfix.org/sendmail.1.html – Steven Feb 25 '20 at 10:14
2

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

fiskcn
  • 21
  • 1
1

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

Alex
  • 1,297
  • 1
  • 16
  • 12
1

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.

levi
  • 1,566
  • 3
  • 21
  • 37
0

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

Atif.SQL
  • 1
  • 4
0

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.

iAndy
  • 11
  • 3
0

I was having this issue while sending files with regional characters in their names like: VęryRęgióńął file - name.pdf.

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

jmarceli
  • 19,102
  • 6
  • 69
  • 67
0

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

Gianluca Demarinis
  • 1,964
  • 2
  • 15
  • 21
0

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.

MomasVII
  • 4,641
  • 5
  • 35
  • 52
0

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('internal@example.com', '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();}

future.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
michal
  • 327
  • 4
  • 15
0

A lot of people often overlook the best/right way to call phpmailer and to put these:

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

or, something like this from the composer installation:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require_once "../../vendor/autoload.php";

on TOP of the page before calling or including anything else. That causes the "Could not instantiate mail function"-error by most folks.

Best solution: put all mail handling in a different file to have it as clean as possible. And always use SMTP. If that is not working, check your DNS if your allowed to send mail.

KJS
  • 1,176
  • 1
  • 13
  • 29
-3

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