Adding security to PHP mail file - php

I've created a working contact form with a PHP file. I have read so many post about security, to the point I'm really confused about what code is needed to filter out potential spammers. Code below is what I have so far. Would you be so kind as to provide any code that would secure this php file so I can learn the correct way.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$website = $_POST['website'];
$message = $_POST['message'];
$formcontent=" From: $name \n Phone: $phone \n Call Back: $call \n Website: $website \n Priority: $priority \n Type: $type \n Message: $message";
$recipient = "";
$subject = "Contact Form Enquiry";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
$homepage = file_get_contents('http://www.fashionablefondants.co.uk/response.html');
echo $homepage;
?>

Related

Phone number is not displaying in $formcontent of my PHP code

<?php
$Name = $_POST['Name'];
$Email = $_POST['Email'];
$Number= $_POST['Number'];
$Message= $_POST['Message'];
$formcontent="
Name: $Name \n
Number: $Number \n
Email: $Email \n
Message: $Message";
$recipient = "info#domain.com";
$subject = "Contact Form";
$mailheader = "From: $Email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>
The number is not displaying but name, email, message are displaying in formcontent. Empty space is showing in place of number while execution.
san.
Try echoing the value of $_POST['Number']. If it's still an empty string, then there must be a problem on the process of fetching the data via post method or in your HTML field for Number.

Contact form sends everything to spam

My contact form works fine but sends everything to spam when I set the recipient as Gmail account and sends nothing if I set it as my domain email client (eg info#mydomain.com). Is there something wrong in the code? What do I need to do?
<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "info#mydomain.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header('Location: message-sent.html#contact'); exit();
?>
Second question. I set up the location to redirect a user to a thank-you page. How can I set it to open in a new tab instead?

When receiving email's I only get my email address as "sent from." although it was sent from another email address. What should I add or change?

So I'm using this code for php mail and I keep getting MY email address rather than the actualy senders email, when I test it on my website's contact form. Any help? By the way, I use my email in the recipients address.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$call = $_POST['call'];
$website = $_POST['website'];
$priority = $_POST['priority'];
$type = $_POST['type'];
$message = $_POST['message'];
$formcontent=" From: $name \n Phone: $phone \n Call Back: $call \n Type: $type \n Message: $message";
$recipient = "myemail#address.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
?>
Have a look at php:mail manual in the example#3 you can see,
<?php
mail('nobody#example.com', 'the subject', 'the message', null,'-fwebmaster#example.com');
?>
You can see
The additional_parameters parameter can be used to pass an additional parameter to the program configured to use when sending mail using the sendmail_path.

PHP mail $email = $_POST['email'] in $recipients

I have the following php mail file :
<?php $name = $_POST['name'];
$email = $_POST['email'];
$choice = $_POST['choice'];$num = $_POST['num'];
$choice1 = $_POST['choice1'];$num1 = $_POST['num1'];
$phone= $_POST['phone'];
$town= $_POST['town'];
$formcontent="
Name: $name \n
Number: $phone \n
Choice: $choice Amount:$num
Choice: $choice1 Amount:$num1
Town: $town \n
Email: $email" ;
$recipient = "info#domain.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";?>
Can I add the $email to the recipients? I want to add the email entered in the form into the recipients that receive the email. (info#domain.com and the email entered in the form ($email = $_POST['email'];) will receive the email.
You can add multiple email addresses to $recipient by separating them with commas. Try this.
$recipient = "info#domain.com, $email";
Edit
Here is the whole thing.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$choice = $_POST['choice']; $num = $_POST['num'];
$choice1 = $_POST['choice1']; $num1 = $_POST['num1'];
$phone= $_POST['phone'];
$town= $_POST['town'];
$formcontent = "
Name: $name \n
Number: $phone \n
Choice: $choice Amount:$num
Choice: $choice1 Amount:$num1
Town: $town \n
Email: $email";
$recipient = "info#domain.com, $email";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank you";
?>

How to add text to email sent from a php contact form

Hi all this is the PHP im using:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$formcontent=" From: $name \n Phone: $phone \n Message: $message";
$recipient = "sophiec#fdb.co.uk";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!" . " -" . "<a href='contact.html' style='text-decoration:none;
color:#ff0099;'> Return Home</a>";
?>
Its working perfectly fine but i would like there to be some text sent with this email that says "This is from your website" or something similar to tell the recipient that it isnt spam (my client isn't tech friendly and sees everything plain text as spam). I'm very new to PHP with nearly 0 knowledge and have no idea how to add something like that. I did have a go at making a new variable with the string inside and then include that in the:
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
line but with no success. Thanks for taking the time any help would be welcome.
Change this line: $formcontent=" From: $name \n Phone: $phone \n Message: $message";
Add the content you want:
$formcontent="This is from your website\n From: $name \n Phone: $phone \n Message: $message";
Or if you want to change the subject, this line: $subject = "Contact Form";
Add the content you want:
$subject = "Contact Form - From your website";
You can either add it in the $subject or $message.
$subject = "Contact Form: This is from your website"; // OR
$message .= "This is from your website";
$formcontent='This is from your website\n From: ' . $name . '\n Phone: ' . $phone . '\n Message: ' . $message;

Categories