Search code examples
phpemailsmtpphpmailer

PHPMailer error: could not instantiate mailer function


Iam using PHPMailer to send mails from my localhost, I am using XAMPP server and sendgrid SMTP server. The problem is that sometimes, the mails sends successfully, and other times it gives me error: could not instantiate mailer function.

assuming i want to send 5 mails, it might end up sending only 2 and the other 3 will give the error: could not instantiate mail function.

Anybody have any idea what might be causing this??

Here is my sendmail.ini configuration file

; configuration for fake sendmail

; if this file doesn't exist, sendmail.exe will look for the settings in
; the registry, under HKLM\Software\Sendmail

[sendmail]

; you must change mail.mydomain.com to your smtp server,
; or to IIS's "pickup" directory.  (generally C:\Inetpub\mailroot\Pickup)
; emails delivered via IIS's pickup directory cause sendmail to
; run quicker, but you won't get error messages back to the calling
; application.

smtp_server=smtp.sendgrid.net

; smtp port (normally 25)

smtp_port=25

; SMTPS (SSL) support
;   auto = use SSL for port 465, otherwise try to use TLS
;   ssl  = alway use SSL
;   tls  = always use TLS
;   none = never try to use SSL

smtp_ssl=auto

; the default domain for this server will be read from the registry
; this will be appended to email addresses when one isn't provided
; if you want to override the value in the registry, uncomment and modify

;default_domain=mydomain.com

; log smtp errors to error.log (defaults to same directory as  sendmail.exe)
; uncomment to enable logging

error_logfile=error.log

; create debug log as debug.log (defaults to same directory as sendmail.exe)
; uncomment to enable debugging

;debug_logfile=debug.log

; if your smtp server requires authentication, modify the following two lines

auth_username=apikey
auth_password=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

; if your smtp server uses pop3 before smtp authentication, modify the 
; following three lines.  do not enable unless it is required.

pop3_server=
pop3_username=
pop3_password=

; force the sender to always be the following email address
; this will only affect the "MAIL FROM" command, it won't modify 
; the "From: " header of the message content

;force_sender=

; force the sender to always be the following email address
; this will only affect the "RCTP TO" command, it won't modify 
; the "To: " header of the message content

;force_recipient=

; sendmail will use your hostname and your default_domain in the ehlo/helo
; smtp greeting.  you can manually set the ehlo/helo name if required

hostname=

And this is the php code

    $mail = new PHPMailer();
    $mail->From = $_SESSION['from'];
    $mail->FromName = $_SESSION['from-name'];
    if ( isset($_SESSION['reply-email']) && isset($_SESSION['reply-name']) ){

        $mail->addReplyTo($_SESSION['reply-email'], $_SESSION['reply-name']);
    }
    $mail->addAddress($_POST['email']);
    $mail->isHTML(true);
    $mail->Subject = $_SESSION['subject'];
    $mail->Body = $_SESSION['message'];

    //try sending the mail
    if ( $mail->send() ){
        $msg = array("status" => 1, "msg" => "sent", "email" => $email, "index" => $index);
        exit( json_encode( $msg ) );
    }else{
        $msg = array('status' => 0, "msg" => $mail->ErrorInfo, "email" => $email, "index" => $index);
        exit( json_encode( $msg ) );
    }
    $msg = array("status" => 0, "msg" => "could not send message", "email" => $email, "index" => $index);
    exit(json_encode($msg));

I know this question might have been asked before, but I have tried all the given solutions, yet they didn't work for me.


Solution

  • Don't rely on PHP's mail function; it's unsafe, slow, and hard to debug. The "Could not instantiate..." error is usually because you don't have a functioning local mail server. Since you're using PHPMailer already, use it to send via SMTP directly to Sendgrid. These settings are partially guesses, and you'll need to insert your SendGrid credentials, but should work:

    $mail->isSMTP();
    $mail->Host = 'smtp.sendgrid.net';
    $mail->SMTPSecure = 'tls';
    $mail->Port = 587;
    $mail->SMTPAuth = true;
    $mail->Username = 'username';
    $mail->Password = 'password';
    

    If you also set $mail->SMTPDebug = 2; you'll be able to watch what's happening.