Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

After upgrading to php8.2 and phpmailer 6.8.0: Could not instantiate mail function #2922

Closed
Damsjov opened this issue Jun 26, 2023 · 4 comments

Comments

@Damsjov
Copy link

Damsjov commented Jun 26, 2023

Problem description

After upgrading to php8.2 and phpmailer 6.8.0 I get this error message: Could not instantiate mail function.
It worked with php7.4 and phpmailer 6.0.8.

Code to reproduce

I don't use composer, so I include phpmailer like this:

require_once("vendor/phpmailer/phpmailer/src/Exception.php");
require_once("vendor/phpmailer/phpmailer/src/PHPMailer.php");
require_once("vendor/phpmailer/phpmailer/src/SMTP.php");
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;

This is the send code:

$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$mail->setLanguage('da', 'vendor/phpmailer/phpmailer/language/');
$mail->setFrom('[email protected]', 'Det Kongelige Akademi (kan ikke besvares)');
$mail->addAddress($email,$name);
$mail->isHTML(true);
$mail->Subject = $sub;
$mail->Body = $body;
$mail->send();

Debug output

[26-Jun-2023 15:22:51 Europe/Copenhagen] PHP Fatal error: Uncaught PHPMailer\PHPMailer\Exception: Could not instantiate mail function. in C:\inetpub\wwwroot\oplysningsskema\vendor\phpmailer\phpmailer\src\PHPMailer.php:1966
Stack trace:
#0 C:\inetpub\wwwroot\oplysningsskema\vendor\phpmailer\phpmailer\src\PHPMailer.php(1680): PHPMailer\PHPMailer\PHPMailer->mailSend()
#1 C:\inetpub\wwwroot\oplysningsskema\vendor\phpmailer\phpmailer\src\PHPMailer.php(1516): PHPMailer\PHPMailer\PHPMailer->postSend()
#2 C:\inetpub\wwwroot\oplysningsskema\oplysningsskema.php(489): PHPMailer\PHPMailer\PHPMailer->send()
#3 {main}
thrown in C:\inetpub\wwwroot\oplysningsskema\vendor\phpmailer\phpmailer\src\PHPMailer.php on line 1966

I am convinced that I have made some kind of mistake, but I can't seem to find any clues in the documentation, I have found.
Looking forward to your thoughts.

Thanks, Claus

@XL-2000
Copy link

XL-2000 commented Jun 26, 2023

Essentially, you are internally calling the native PHP mail() function and thus do not run the PHPMailer SMTP setup, is that correct?
Since you are using the PHP mail() function, the problem lies there, not with PHPMailer which looks to work fine.
It may be the case that after your PHP upgrade that the native mail() function is no longer available to you.
Try switching to proper SMTP and stop using the quirky PHP mail() function

@Damsjov
Copy link
Author

Damsjov commented Jun 27, 2023

Thanks, but I dont' understand - doesn't "$mail = new PHPMailer();" indicate that I wish to use PHPMailer? And if I am using native PHP Mail, how come it is an error in the PHPMailer script that occurs?

It is true that the configuration of the SMTP service happens in php ini though. Is that the reason?

@XL-2000
Copy link

XL-2000 commented Jun 27, 2023

You are using PHPMailer as the builder for sending emails.
In the end the email has to sent through a means of "transport". This can be a fully configured SMTP relay (which is actually the preferred way), but if you do not explicitly configure a relay, PHPMailer will use the native PHP mail function as fallback, which will in essence try to spawn a local(host) mail relay for you.

If you want to make use of the true power of PHPMailer and configure a SMTP relay, you should start with setting:
$mail->isSMTP();

And than also configure this SMTP connection with settings like:


$mail->Host = 'myRemoteHost';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Username = 'mySMTPusername';
$mail->Password = 'mySMTPpassword';

@XL-2000
Copy link

XL-2000 commented Jun 27, 2023

And if I am using native PHP Mail, how come it is an error in the PHPMailer script that occurs?

Since PHPMailer is calling the native PHP mail() function, the error of calling an undefined / unavailable function is coming from the PHPMailer script. It has nothing to do with PHPMailer itself, it is merely PHPMailer who is responsible for calling this function

It is true that the configuration of the SMTP service happens in php ini though. Is that the reason?

SMTP settings to be used for PHPMailer should be explicitly set like I mentioned in my comment above

@Synchro Synchro closed this as completed Nov 23, 2023
@PHPMailer PHPMailer deleted a comment from hellosuny Mar 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants