0
<?php

if (isset($_POST['myName']) && empty($_POST['honeypot'])) {
    $myName = $_POST['myName'];
    $myEmail = $_POST['myEmail'];
    $myQuestion = $_POST['myQuestion'];
    date_default_timezone_set('Etc/UTC');

    require '../PHPMailer/PHPMailerAutoload.php';

     //Create a new PHPMailer instance
    $mail = new PHPMailer;

    //Tell PHPMailer to use SMTP - requires a local mail server
    //Faster and safer than using mail()
    $mail->SMTPDebug = 2;
    $mail->isSMTP();
    $mail->Host = 'mail.myname.website.com';
    $mail->Port = 587;

      //Set this to true if SMTP host requires authentication to send email
    $mail->SMTPAuth = true;


    //Provide username and password
    $mail->Username = 'phpmailer@myname.website.com';
    $mail->Password = 'AGoodPassword';

     //Use a fixed address in your own domain as the from address
    //**DO NOT** use the submitter's address here as it will be forgery
    //and will cause your messages to fail SPF checks
    $mail->setFrom('phpmailer@myname.website.com', 'My Name');

      //Send the message to yourself, or whoever should receive contact for submissions
    $mail->addAddress('firstname.lastname@email.edu', 'My Name');

     //Put the submitter's address in a reply-to header
    //This will fail if the address provided is invalid,
    //in which case we should ignore the whole request
    $mail->addReplyTo($myEmail, $myName); 
    $mail->Subject = 'PHT || Questions or Comments';

     //Keep it simple - don't use HTML
    $mail->isHTML(true);

      //Build a simple message body
    $mail->Body = <<<EOT
    Email: $myEmail<br>
    Name: $myName<br>
    Message: $myQuestion
    EOT;

      //Send the message, check for errors
        if (!$mail->send()) {
            //The reason for failing to send will be in $mail->ErrorInfo
            //but you shouldn't display errors to users - process the error, log it on your server.
            echo "Mailer error" . $mail->ErrorInfo;

        } else {
            include 'sent.html.php';
        }

    } else {
        include 'contact.html.php';
    } 
?>

I've changed the emails and password but the rest of the code is exactly the same.

For some backstory, I've been trying to figure out the problem with this code off and on for the last week. The short of it is it runs fine on Localhost, which is using Windows, but on my Webserver I'm immediately met with an HTTP Error 500. I checked the error logs and the only thing popping up is (Don't know if it was necessary but I edited some of the file names for this post):

PHP Parse error: syntax error, unexpected end of file in /home2/myname/public_html/'filepath'/'filepath'/'filepath/index.php on line 67

I ran my code through a PHP code checker here: https://phpcodechecker.com/

It gave me the same error:

PHP Syntax Check: Parse error: syntax error, unexpected end of file in your code on line 67

?>

I can't figure out why ?> would be an unexpected end of file. I thought maybe there was a syntax error earlier in the code, but after combing it several times everything seems to check out to my knowledge. Furthermore, I can't figure out why this code could run on Localhost but not my server. I've found a number posts on Stack Overflow where people are having the same problem but none of them have been helpful to my situation. Any ideas?

EDIT:

User 'ceejayoz' got it right. I fixed the indentation on the EOT; line and everything was fixed.

1 Answers1

1

https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

Warning It is very important to note that the line with the closing identifier must contain no other characters, except a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon. It's also important to realize that the first character before the closing identifier must be a newline as defined by the local operating system. This is \n on UNIX systems, including macOS. The closing delimiter must also be followed by a newline.

If this rule is broken and the closing identifier is not "clean", it will not be considered a closing identifier, and PHP will continue looking for one. If a proper closing identifier is not found before the end of the current file, a parse error will result at the last line.

Your EOT line here:

    EOT;

is indented, which means it's never spotted by PHP as the end of the heredoc statement.

Community
  • 1
  • 1
ceejayoz
  • 176,543
  • 40
  • 303
  • 368
  • Yes, this was it 100%. I had no idea this was an issue, and I might be better off using a code editor that automatically formats my code properly. Thank you! – havesomeCOAT Dec 02 '19 at 03:44