Given the constant problems with the mail function, I’m trying to use PHPMailer but I’m getting a 500 error.
Here is the test script I’m using (almost identical to the one shown in the OVH documentation, without using Composer)
require 'src/PHPMailer.php';
require 'src/SMTP.php';
require 'src/Exception.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
$mail = new PHPMailer(true);
try {
$mail->isSMTP();
$mail->Host = 'smtp.mail.ovh.net';
$mail->SMTPAuth = true;
$mail->Username = 'moi@mondomaine.com';
$mail->Password = 'mon mdp';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->Port = 465;
$mail->setFrom('moi@mondomaine.com', 'Moi');
$mail->addReplyTo('moi@mondomaine.com' ,'moi');
$mail->addAddress('moi@free.fr', 'moi');
$mail->isHTML(true);
$mail->Subject = 'Test PHPMailer';
$mail->Body = 'Ceci est un email de test en <b> HTML </b>';
$mail->AltBody = 'Ceci est un email de test en texte brut.';
$mail->send();
echo 'Email envoyé !';
}
catch (Exception $e) {
echo "Erreur l'email n'a pas été envoyé: {$mail->ErrorInfo}";
}
My test.php file is set to chmod 755 in the PHPMailer directory.
I assume I missed something, but I can’t see where.
I think your 500 error is not due to your PHPMailer configuration nor the test.php file itself, but because the require paths are relative and can't find the library files.
You should use __DIR__ . '/../PHPMailer/src/PHPMailer.php'. The /../ fragment tells PHP to go up one directory before looking for the PHPMailer folder.
The best practice is to use an absolute path defined as a constant in a shared configuration file, or even better, use Composer's autoloader if you can.
Since you don't want to use Composer, the cleanest alternative is to define a constant in a file that is always included. For example, create a config.php file in www with:
If you think any of my answers is the solution, I’d appreciate you clicking the “Solution” button you’ll find in my answers, so we can help other users with the same problem.
I tried the constant and it works fine in all directories and subdirectories.
However, if in a simple form I replace the mail function with the code from my test file above, modified with the constant, I get a 500 error and the error‑display code returns nothing.
The 500 error you’re seeing now, after the constant works fine in your isolated tests, points directly to a problem with how you’re integrating PHPMailer into the form, not to the library itself.
While we’re here to help each other, this kind of code‑integration error is a bit beyond the scope of what OVHcloud can resolve. The OVH community is great for service configuration, networking, DNS, mail, etc., but for debugging specific PHP code there are more specialized forums and communities that can give you a hand more quickly and precisely.
It’s not that we don’t want to help you, it’s just that the right place sometimes makes all the difference. You’ll get it sorted soon!
I suspect that this is probably about how to integrate PhpMailer into my form.
Indeed, I understand that the OVH community is mainly there to help with topics directly related to OVH (which is somewhat the case), but considering I won’t be the only one who has to modify its forms, or that some have already done so, I thought that the solved issue in its entirety might be of interest.