0

I'm pretty new with php coding. Recently, I made a code with html and decided to make a php code so that it can deliver messages online. But when I uploaded it to the server, the code brings this error. This is the code that I wrote in php

<?php

$name= $_POST['name'];
$email=$_POST['email'];
$number=$_POST['number'];
$enquiry=$_POST['enquiry'];
$reaction=$_POST['reaction'];
$cpreference=$_POST['cpreference'];
$comment=$_POST['comment'];

$to      = 'repoductivehealthservices@gmail.com';
$subject = 'New Get-In-Touch Form Recieved';
$message = "Name: ".$name."\nEmail: ".$email."\nPhone Number:       ".$number."\nType of Enquiry: ".$enquiry."\nContact Back:  ".$reaction."\nContact Preference: ".$cpreference."\nComments: ".$comment;
$headers = 'From: getintouch@reproductivehealthservices.com' . "\r\n" .
'Reply-To: no_reply@reproductivehealthservices.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>
<script>
    alert('Thanks! We will get back to you soon.');
    window.location.replace("../get-in-touch/index.html");
</script>

and this is the error that I got when i filled in the text-field in the website.

[06-May-2018 05:37:47 UTC] PHP Parse error: syntax error, unexpected ''X-Mailer'' (T_CONSTANT_ENCAPSED_STRING), expecting ')' in /home/reproduc/public_html/get-in-touch/redirect.php on line 16

[06-May-2018 05:38:29 UTC] PHP Warning: mail() expects parameter 4 to be string, array given in /home/reproduc/public_html/get-in-touch/redirect.php on line 18

[06-May-2018 05:41:36 UTC] PHP Warning: mail() expects parameter 4 to be string, array given in /home/reproduc/public_html/get-in-touch/redirect.php on line 18

[06-May-2018 05:42:06 UTC] PHP Warning: mail() expects parameter 4 to be string, array given in /home/reproduc/public_html/get-in-touch/redirect.php on line 18

[06-May-2018 05:55:01 UTC] PHP Warning: Cannot modify header information - headers already sent by (output started at /home/reproduc/public_html/get-in-touch/redirect.php:23) in /home/reproduc/public_html/get-in-touch/redirect.php on line 25

You can download the html file from here https://drive.google.com/file/d/1v1q-IgLxKq0hwCPbLyNU7aEwHqVc_-IL/view?usp=sharing

Community
  • 1
  • 1

1 Answers1

0

When you create an email variable with joins of other variables, conflicts will intervene. Therefore, the most recommended is to separate each assignment, as in the variable "$message"

You can correct these errors by just separating the string:

$name = $_POST['name'];
$email = $_POST['email'];
$number = $_POST['number'];
$enquiry = $_POST['enquiry'];
$reaction = $_POST['reaction'];
$cpreference = $_POST['cpreference'];
$comment = $_POST['comment'];

$to       = "repoductivehealthservices@gmail.com";
$subject  = "New Get-In-Touch Form Recieved";

//Message string
$message  = "Name: ".$name."\r\n";
$message .= "Email: ".$email."\r\n";
$message .= "Phone Number: ".$number."\r\n";
$message .= "Type of Enquiry: ".$enquiry."\r\n";
$message .= "Contact Back: ".$reaction."\r\n";
$message .= "Contact Preference: ".$cpreference."\r\n";
$message .= "Comments: ".$comment."\r\n";

//Headers
$headers  = "MIME-Version: 1.1"."\r\n";
$headers .= "Content-type: text/plain; charset=UTF-8"."\r\n";
$headers .= "From: getintouch@reproductivehealthservices.com"."\r\n";
$headers .= "Reply-To: no_reply@reproductivehealthservices.com"."\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();

$send = mail($to, $subject, $message, $headers);

if($send){
    echo "<script>alert('Thanks! We will get back to you soon.'); window.location.replace('../get-in-touch/index.html');</script>";
} else {
    echo "<script>alert('Error!');</script>";
}
?>
  • 1
    "Try this" does not make for a good answer. You should explain *how* and *why* this solves their problem. I recommend reading, "[How do I write a good answer?"](http://stackoverflow.com/help/how-to-answer) – John Conde May 18 '18 at 02:30