Home > OS >  PHPMailer not working - no specific error
PHPMailer not working - no specific error

Time:02-16

I have an old website that was not created by me, I noticed that the contact form doesn't work and when you press send it doesn't do anything. In the console it says:

POST path of the mailer file 500 (Internal Server Error)

I can't find a solution so if anyone can help I would be very grateful. Here's the code:

<?php
require ('libphp-phpmailer/PHPMailerAutoload.php');
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$experience = $_POST['experience'];
$note = $_POST['note'];
$cv = $_POST['cv'];
$sended = 0;
if($firstname != '' && $lastname != '' && $email != '' && $phone != '' && $experience != '' && $note != '' && $cv != ''){
    switch($experience){
        case "1-5":
            $exp = "1 to 5 years";
            break;
        case "5-more":
            $exp = "more than 5 years";
            break;
        default:
            $exp = "nothing";
            break;
    }
    $body = "firstname: " . ucfirst($firstname) . "\n" . "lastname: " . ucfirst($lastname) . "\n" . "phone: " . $phone . "\n" . "Experience: " . $exp . "\n" . $note . "\n\n";
    $mailer = new PHPMailer();
    $mailer->IsSMTP();
    $mailer->Host = '---the email host----';
    $mailer->SMTPAuth = true;
    $mailer->Username = '---the right email---';
    $mailer->Password = '---email password---';
    $mailer->Port = 25;
    $mailer->Encoding = "base64";
    $mailer->CharSet = 'UTF-8';
    $mailer->AuthType = 'PLAIN';
    $mailer->setFrom($email, ucfirst($firstname) . ' ' . ucfirst($lastname));
    $mailer->AddReplyTo($email, ucfirst($firstname) . ' ' . ucfirst($lastname));
    $mailer->AddAddress('---the receiving address---');
    $mailer->isHTML(false);
    $mailer->ContentType = 'text/plain';
    $mailer->Subject = 'Contact form';
    $mailer->Body = $body;
    $mailer->AddAttachment($cv , 'Cv-' . $firstname . '-' . $lastname . '.pdf');
    if($mailer->Send()){
        $sended = 1;
    }
    $response = array(
        'sended'    => $sended
    );
    echo json_encode($response);
}
?>

CodePudding user response:

I added ini_set("display_errors", "On"); at the top of the file and mailer->SMTPDebug = 2; after $mailer = new PHPMailer(); then I looked at the error in the Network section of the browser toolbox. I found out that the error was in the line:

require ('libphp-phpmailer/PHPMailerAutoload.php');

Because PHPMailerAutoload didn't exist so I deleted the line and I uploaded the PHPMailer files (downloaded here) in my website via FTP, then I added these lines at the beginning of my code:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;

require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
  • Related