DEV Community

Nick Scott
Nick Scott

Posted on

Assistance with PHP Code?

Hi all!

Working on creating a contact form that sends e-mail to an e-mail address. I'm THIS close to perfection, however, I notice that not all of the data is being captured.

All it captures is the e-mail address & the message itself. Not the "name" field or the drop-down data.

Any changes I make only results in the e-mail being considered spam.

Here is my code:

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$to = "myemail@example.com";
$subject = "Contact Form Submission";
$message = htmlspecialchars($_POST['message']);
$headers = "From: " . htmlspecialchars($_POST['email']);

mail($to, $subject, $message, $headers);
Enter fullscreen mode Exit fullscreen mode

}
?>

Any changes I make only results in the e-mail being considered spam.

Please help!

Top comments (4)

Collapse
 
dasfluchen profile image
DasFluchen
Collapse
 
kwnaidoo profile image
Kevin Naidoo • Edited

I wouldn't send emails like this. Just use Laravel or Symfony, even Codeigniter - all have a decent email implementation and you can use a SMTP provider like SES, sendgrid, or Zoho Mail, etc...

This will send an email from your server IP, so it probably has a low reputation and lacks DNS records like SPF, DKIM, and DMARC.

Each of the HTML selects, inputs will be a separate post data key, so this will just take the textarea content, I am assuming:

$message = htmlspecialchars($_POST['message']);
Enter fullscreen mode Exit fullscreen mode

You need to do something like this:

$message .= "<br />Customer name: ". strip_tags($_POST["name"]);
Enter fullscreen mode Exit fullscreen mode

Lastly, you need to set headers for HTML emails, something like:

$headers = "From: " . str_replace(["\r", "\n"], '', $email) . "\r\n";
$headers .= "Reply-To: " . str_replace(["\r", "\n"], '', $email) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=utf-8\r\n";
Enter fullscreen mode Exit fullscreen mode
Collapse
 
xwero profile image
david duymelinck

What are the changes you made?

Collapse
 
nick_scott_6711c9830e56a4 profile image
Nick Scott

Thanks everyone! I wound up going a different route. I went to formsubmit.co/ they do the work for you!

In the interim, I plan to take a PHP course to brush up on my coding!

Survey image

Calling All Cloud Developers - Your Insights Matter

Take the Developer Nation Survey and help shape cloud development trends. Prizes await!

Join Today