Skip to main content
Participating Frequently
July 1, 2023
Question

Syntax Error when Specifying Email

  • July 1, 2023
  • 3 replies
  • 2656 views

I am trying to create a contact form for a webpage in Dreamweaver and my php code displays an error message saying "syntax error, unexpected '@'". I have marked the line where the error is being detected in red. My code is below:

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];
 
    // Email address where you want to receive the form submissions
    $to = ‘EMAIL REMOVED BY MODERATOR’;
 
    // Email subject
    $emailSubject = 'New Contact Form Submission';
 
    // Email content
    $emailContent = "Name: $name\n";
    $emailContent .= "Email: $email\n";
    $emailContent .= "Subject: $subject\n";
    $emailContent .= "Message: $message\n";
 
    // Send the email
    $headers = "From: $name <$email>";
    if (mail($to, $emailSubject, $emailContent, $headers)) {
        echo "Message sent successfully!";
    } else {
        echo "Failed to send the message. Please try again.";
    }
}
?>

 

 

Can someone help with this?

This topic has been closed for replies.

3 replies

Nancy OShea
Community Expert
Community Expert
July 1, 2023

I can cut a whole chapter from this thread by suggesting you use PHP Mailer instead of what you have now. 

 

PHP Mailer is a safe & secure form processing script and mail sending library that's freely available from GitHub.  It hides your email from would-be spammers and won't leave your form & server  vulnerable to exploitation by hackers.  A+++

https://github.com/PHPMailer/PHPMailer

 

Nancy O'Shea— Product User & Community Expert
BenPleysier
Community Expert
Community Expert
July 1, 2023
Wappler is the DMXzone-made Dreamweaver replacement and includes the best of their powerful extensions, as well as much more!
Legend
July 2, 2023
quote

I use this extension

https://www.dmxzone.com/go/32481/dmxzone-mailer/

 


By @BenPleysier

 

............if you have 70 quid to spare...............hummm.

Legend
July 1, 2023

Is this just an error you see in Dreamweaver? Does the php code actually work and send to the email address?

 

I cant see that there is any error in the code itself. The ' ' which wraps the string of text in the error line looks a bit different to the ones which wrap the string of text for $emailSubject although that could be as a result of this forum interpretation.

 

The code is currently insecure as you have no validation in place also  if that is the correct email address/recipient you have posted l would put a few XXXXs within it to fool email harvesters which might be looking for email addresses in the forum, and then spam them.

Participating Frequently
July 1, 2023

Hi osgood,

I managed to fix the syntax error quite easily. Now, when I'm in the browser and I fill in the boxes, it comes up with "Not Found" when I hit "Submit". The javascript code is below, if this helps:

// Retrieve form values
var name = document.getElementById("name").value;
var email = document.getElementById("email").value;
var subject = document.getElementById("subject").value;
var message = document.getElementById("message").value;

// Here you can perform additional validation or processing of the form data
// For this example, we'll simply log the form values to the console
console.log("Name: " + name);
console.log("Email: " + email);
console.log("Subject: " + subject);
console.log("Message: " + message);

// Reset the form after submission (optional)
document.getElementById("contact-form").reset();
});

Legend
July 1, 2023

Where is your forms action attribute pointing to, it should be pointing to the file that your php code is in.

 

The javascript you posted isn't doing much apart from logging the information to your browsers console.

Participating Frequently
July 1, 2023

Fixed that issue. Used the " symbols to bracket the Email address. Now, when I'm in the browser and enter the information into the form boxes, when I hit "Submit", I get the error message "Not Found"

Legend
July 1, 2023
quote

Fixed that issue. Used the " symbols to bracket the Email address. Now, when I'm in the browser and enter the information into the form boxes, when I hit "Submit", I get the error message "Not Found"


By @bradb72641428

 

Have you uploaded the php file which handles the post request to your server, l assume its in a separate file to your form.