PHP form seems to have update issues

for some reason this form stopped working a while ago. As I have no clue how php works, someone said I should add a “from” field. No idea how to, so if anyone can help me out here or point to an already existing code that would have the same result, great! This is the code:

<?php
$ip = $_SERVER['REMOTE_ADDR']; // get ip to short variable name for logging purposes 
$to = "mail@mywebsite.com";
$subject= "order home delivery via website";
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$street = $_POST['street'];
$city = $_POST['city'];
$date = $_POST['date'];
$delivery = $_POST['delivery'];
$falafel = $_POST['falafel'];
$remarks = $_POST['remarks'];
$message = "
Name: $name
Phone: $phone
Email: $email
Address: $street, $city
Time of delivery: $date
Delivery: $delivery
Book: $book
Order: 
$falafel
Remarks: $remarks
";
  $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
     $headers = "From: $email\r\n" .
     "MIME-Version: 1.0\r\n" .
        "Content-Type: multipart/mixed;\r\n" .
        " boundary=\"{$mime_boundary}\"";
     $message = "This is a multi-part message in MIME format.\n\n" .
        "--{$mime_boundary}\n" .
        "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
        "Content-Transfer-Encoding: 7bit\n\n" .
     $message . "\n\n";
     foreach($_FILES as $userfile)
     {
        $tmp_name = $userfile['tmp_name'];
        $type = $userfile['type'];
        $name = $userfile['name'];
        $size = $userfile['size'];
        if (file_exists($tmp_name))
        {
           if(is_uploaded_file($tmp_name))
           {
              $file = fopen($tmp_name,'rb');
              $data = fread($file,filesize($tmp_name));
              fclose($file);
              $data = chunk_split(base64_encode($data));
           }
           $message .= "--{$mime_boundary}\n" .
              "Content-Type: {$type};\n" .
              " name=\"{$name}\"\n" .
              "Content-Disposition: attachment;\n" .
              " filename=\"{$fileatt_name}\"\n" .
              "Content-Transfer-Encoding: base64\n\n" .
           $data . "\n\n";
        }
     }
     $message.="--{$mime_boundary}--\n";
if (mail($to, $subject, $message, $headers))
echo "<b>Thanks for your order.</b><br>It will be processed very shortly, so I will get back to you soon.<br>Please stay healthy in the meantime.
<br>&nbsp;<br>&nbsp;<br><blockquote class='bordered layout-2'><b>This was your order:</b>
<br>&nbsp;<br>$name
<br>$street, $city
<br>$phone - $email
<br>&nbsp;<br>$book
<br>&nbsp;<br>$delivery
<br>delivery time: $date
<br>&nbsp;<br>$falafel
<br>&nbsp;<br>Remarks:<br>$remarks</blockquote>
<br>&nbsp;<br><a href='https://mywebsite.com/home-delivery.php' class='btn btn-outline-maincolor'>Missed something - go back</a>
";
else
echo "Your order has not been processed.<br>Please try again, filling in all fields.<br><a href='https://mywebsite.com/home-delivery.php'>&#187; go back</a>";
?>

Can anyone let me know what I should have to add where to make it work again, please? Thanks.

Can you give a bit more detail on what it does? That is, how far through does it get before it fails? Do you get any error messages? The old-style PHP mail() function doesn’t get a lot of love on here, people usually suggest using something like PHPMailer because it is more reliable and more capable.

That said, you might find that your mail server is getting upset when you try to send the email from the address that the person puts on the form. Most mail servers are configured to only send mail from domains that they are configured to send, otherwise it’s a thing called “Open Relay”. You could just change this line

$headers = "From: $email\r\n" .

to use an email address that your server is configured for, just by getting rid of the variable name:

$headers = "From: myaddress@mydomain.com\r\n" .

I suspect you may have some reading to do. It doesn’t seem reasonable to expect to troubleshoot some code like this in a language you are not familiar with.

1 Like

Thanks for clarifying this, Droopsnoot.
I just had no idea how to send a form so I got the code from the internet and it all worked fine. Now it just goes to the next page where I get the error message (and don’t receive an email). This happened since my provider upgraded tot PHP 7.4.

Your solution works indeed, the form gets through now.
Just one thing: when I hit reply, I am mailing myself. Is it possible to have the mailadres of the sender as the “from”?

1 Like

Not usually, because your mail server probably won’t send it, for security reasons. You can set the “Reply-to” header to the email address that comes from the form, so when you hit reply it will probably go to them.

1 Like

So I just add this f.ex at line 3
$Reply-to = "$email";
Correct?

No, not like that. All that does is create a variable called $Reply-to and give it a value, and I’m not sure what you mean by “f.ex”.

What you need to do is add the reply-to email address into the headers of the email. You can see that you are creating a variable called $headers starting with this line of code:

$headers = "From: myaddress@mydomain.com\r\n" .

You could just put it in there, I guess

$headers = "From: myaddress@mydomain.com\r\nReply-To: $email\r\n" .

You’re using the variable that you took out before but instead of putting it in the “From” header, you’re putting it in the “Reply-to” header.

I’m not sure that will work, but there’s an easy way to find out. If it doesn’t, I’m sure someone else will pick up on it, or there’s plenty of sample code on here showing email sending.

1 Like

Works like a charm, thanks!

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.