Search code examples
phpphpmailer

Phpmailer not working on webserver


I am trying to get phpmailer to work on a web server. It does work with no problem on my xampp server but as soon as I put it on the web server it cant find these and it gives me the error. I also tried putting it on another web server and that doesn't work. I also copied the files from my xampp server to the web server and it still doesn't work.

use PHPMailer\PHPMailer\PHPMailer; (line 3)
use PHPMailer\PHPMailer\Exception; (line 4)


Error: parse error: syntax error, unexpected T_STRING, expecting T_CONSTANT_ENCAPSED_STRING or '('email.php on line 3

the code that the error points to is

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
function sendemail(){



require '../mail/src/PHPMailer.php';
require '../mail/src/SMTP.php';

$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
    include "removed(was database)";
    $name = $_POST['name'];
    $Uemail = $_POST['email'];
    $select = $_POST['select'];
    $issue = $_POST['issue'];
    $resolution = $_POST['resolution'];
    $datesubmited = date("Y-m-d H:i:s");
    //Server settings
    $mail->SMTPDebug = 4;  //1-4                               // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'removed';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'removed';                 // SMTP username
    $mail->Password = 'removed';                           // SMTP password
    $mail->SMTPSecure = '';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = removed;                                    // TCP port to connect to

    //Recipients
    $email = 'removed'; //put in default email that corresponds with the username / passwoard.
    $mail->setFrom( $email, 'removed');
    $mail->addAddress($Uemail, $name);
    $mail->addAddress('removed', 'removed');
    $mail->addAddress('removed', 'removed');     // Add a recipient
    // $mail->addAddress('', $name);


    //Attachments
    // $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    // $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Request from ' . $name;
    $mail->Body    = 'Details ' . "<br>" .
    "Date: " . $datesubmited . "<br>" .
    "Name: " . $name . "<br>" .
    "Email Address: " . $email . "<br>" .
    "Area of Concern: " . $select . "<br>" . 
    "Issue: " . $issue . "<br>" .
    "Suggested Resolution: " . $resolution . "<br>"; 




    $mail->AltBody = 'Hello '. $name . ' your request has been sumbited!'.'</b>';
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
    $mail->send();
    echo "Thank you " . $name . " for your submission." . "<br>" . "Someone will repsond to you shortly.";
    echo "<br>";
    echo "Go back home " . "removed" ;
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
} 




?>

Solution

  • At first glance I would say that the use function seems to call the problem. Could you try adding these lines before your first use call and check the result:

    $funcDefinition = new ReflectionFunction('use');
    print $funcDefinition->getFileName() . ', ' .
      $funcDefinition->getStartLine();
    

    Check the results of the call on your local and your remote systems and compare the outcome. I don't think that you are deploying on Pre-PHP-5.3 systems where the use command was not available but please also add some information regarding the different PHP versions used locally and remote.

    In fact it seems that you get your error message in case you are deploying on PHP 5.2 systems as this SO thread mentions. So first check has to be the PHP version (i.e. by adding a phpinfo(); before your first use call).

    Conclusion: question author received the error due to deploying on a PHP 5.2 system without use support.