PHP Mailer: Specific Confirmations Messages based on Empty Fields - php

I have a Contact Form which submits the data using AJAX to a PHP mailer. Once the email have been sent a confirmation message is shown to the user. It works well. My problem is how to customize the confirmation message based on which fields have been filled.
The form has four fields. Name (required), Subject (option list: by default 'Subscribe to Newsletter'), Email (required) and Message and there are two cases:
1) User is only looking to get subscribed to the Newsletter. Only 'Name' and 'Email' fields are filled. Confirmation message A.
2) User sends an email. 'Name', 'Email' and 'Message' are filled. Confirmation message B.
This is my current code, shows a general confirmation message:
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = strip_tags(trim($_POST["name"]));
$asunto = strip_tags(trim($_POST["asunto"]));
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$message = trim($_POST["message"]);
if ( empty($name) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
http_response_code(400);
echo "¡Error 400 bla bla...!
exit;
}
$recipient = "name#domain.com";
$subject = "Nauta $name";
$email_content = "Nombre: $name\n";
$email_content = "Asunto: $asunto\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Mensaje:\n$message\n";
$email_headers = "From: $name <$email>";
if (mail($recipient, $subject, $email_content, $email_headers)) {
http_response_code(200);
echo "¡Thank you! bla bla...";
}
else {
http_response_code(500);
echo "¡Error 500 bla bla...!
}
}
else {
http_response_code(403);
echo "¡Error 403 bla bla...!
}
I tried the following structure (elseif) with no luck.
if ( ) {
echo "...";
} elseif ( ) {
echo "...";
} else {
echo "...";
}
No results.

Try to ask if message is an empty string or one blank character:
if ($_POST['message']!="") {
...
}

Thank you, #Carmen. It didn't work but I have found the way to make it work.
if (empty($_POST['message'])) {
...
}

Related

Bold text in PHP form (website contact to email)

I finally manged to get my contact form on my website to work, however, when it comes through via email, it's very plain. Would it be possible to try and get some words bolded under the email content settings? I'm new to all of this and I'm slowly learning but I've been stuck on this one for hours.
Content Code:
// Build the email content.
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Company: $company\n\n";
$email_content .= "Message:\n$message\n";
Full Mailer.php code:
<?php
// Only process POST requests.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get the form fields and remove whitespace.
$name = strip_tags(trim($_POST["name"]));
$name = str_replace(array("\r","\n"),array(" "," "),$name);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$message = trim($_POST["message"]);
$company = trim($_POST["company"]);
// Check that data was sent to the mailer.
if ( empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
// Set a 400 (bad request) response code and exit.
http_response_code(400);
echo "Oops! There was a problem with your submission. Please complete the form and try again.";
exit;
}
// Set the recipient's email address.
// FIXME: Update this to your desired email address.
$recipient = "(my company email here)";
// Set the email subject.
$subject = "Website Query - $name $company";
// Build the email content.
$email_content = "<strong>Name:</strong> $name\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Company: $company\n\n";
$email_content .= "Message:\n$message\n";
// Build the email headers.
$email_headers = "From: $name <$email>";
// Send the email.
if (mail($recipient, $subject, $email_content, $email_headers)) {
// Set a 200 (okay) response code.
http_response_code(200);
echo "Thank You! Your message has been sent.";
} else {
// Set a 500 (internal server error) response code.
http_response_code(500);
echo "Oops! Something went wrong and we couldn't send your message.";
}
} else {
// Not a POST request, set a 403 (forbidden) response code.
http_response_code(403);
echo "There was a problem with your submission, please try again.";
}
You would use standard html syntax.
So, send an email with bold text in it, you'd simply send
<b>Bold Text</b>
In your case, do this
$temp_message = trim($_POST["message"]);
//Find which part you want to bold or do this to make the whole thing bold.
$message = "<b>"+$temp_message+"</b>"

Contact form sending but not receiving

This is my first time asking something here so I hope I'm writing this post correctly.
I have this contact form at the end of this page (www.emilianomelchiorre.com) and, even if everything seems ok, I still don't receive any email. I don't know if the problem is in the code or maybe it's just the server.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
# FIX: Replace this email with recipient email
$mail_to = "MYEMAIL#gmail.com";
# Sender Data
$subject = trim($_POST["subject"]);
$name = str_replace(array("\r","\n"),array(" "," ") , strip_tags(trim($_POST["name"])));
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$phone = trim($_POST["phone"]);
$message = trim($_POST["message"]);
if ( empty($name) OR !filter_var($email, FILTER_VALIDATE_EMAIL) OR empty($phone) OR empty($subject) OR empty($message)) {
# Set a 400 (bad request) response code and exit.
http_response_code(400);
echo "Please complete the form and try again.";
exit;
}
# Mail Content
$content = "Name: $name\n";
$content .= "Email: $email\n\n";
$content .= "Phone: $phone\n";
$content .= "Message:\n$message\n";
# email headers.
$headers = "From: $name <$email>";
# Send the email.
$success = mail($mail_to, $subject, $content, $headers);
if ($success) {
# Set a 200 (okay) response code.
http_response_code(200);
echo "Thank You! Your message has been sent.";
} else {
# Set a 500 (internal server error) response code.
http_response_code(500);
echo "Oops! Something went wrong, we couldn't send your message.";
}
} else {
# Not a POST request, set a 403 (forbidden) response code.
http_response_code(403);
echo "There was a problem with your submission, please try again.";
}
?>
I'm not really sure about this part of the code, maybe something doesn't match. I really would like any kind of help. Thank you in advance!

My php contact form looks fine, why do I still get spam injection?

I know these are downvoted, but I just don't understand what I'm doing wrong. I have the php contact form below and I keep getting injected, bots are uploading files on my server to send spam.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = trim($_POST["username"]);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$message = trim($_POST["message"]);
if(isset($_POST['g-recaptcha-response'])){
$captcha = $_POST['g-recaptcha-response'];
}
//Validate the data
if (empty($name) OR !filter_var($email, FILTER_VALIDATE_EMAIL) OR empty($message) OR empty($captcha)) {
http_response_code(400);
echo "<span class='glyphicon glyphicon-remove' aria-hidden='true'></span> <strong>Please fill all the form inputs and check the captcha to submit.</strong>";
exit;
}
//recipient email address.
$recipient = "mail#mail.com";
//email subject.
$subject = "New message from $name";
//email content.
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Message:\n$message\n";
//email headers.
$email_headers = "From: $name <$email>";
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=6Lf7gjIUAAAAAOxxh1Y2oLGPB9T_iPm4VYOD2LhV&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
$decoded_response = json_decode($response, true);
if($decoded_response['success'] == true) {
// Send the email.
if (mail($recipient, $subject, $email_content, $email_headers)) {
http_response_code(200);
echo "<span class='glyphicon glyphicon-ok' aria-hidden='true'></span> <strong>Thank You! Your message has been sent.</strong>";
} else {
http_response_code(500);
echo "Whoa! message could not be sent.";
}
} else {
http_response_code(400);
echo 'You are a spammer!';
}
}
?>
So what am I doing wrong here?

Send ajaxform data to multiple email addresses

I wish to send a copy of the data filled out by the visitor to myself and the other copy to the visitor. Please advice!
My email is myemail#mysite.com while the visitor's email is $email
This code is as seen below:
// Set the recipient email address.
// FIXME: Update this to your desired email address.
$recipient = "myemail#mysite.com, .$email";
I'm not getting any results after hitting the submit button!
I'll be very grateful!
<?php
// My modifications to mailer script from:
// http://blog.teamtreehouse.com/create-ajax-contact-form
// Added input sanitizing to prevent injection
// Only process POST reqeusts.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get the form fields and remove whitespace.
$name = strip_tags(trim($_POST["name"]));
$name = str_replace(array("\r","\n"),array(" "," "),$name);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$message = trim($_POST["message"]);
// Check that data was sent to the mailer.
if ( empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
// Set a 400 (bad request) response code and exit.
http_response_code(400);
echo "Oops! There was a problem with your submission. Please complete the form and try again.";
exit;
}
// Set the recipient email address.
// FIXME: Update this to your desired email address.
$recipient = "myemail#mysite.com, .$email";
// Set the email subject.
$subject = "New contact from $name";
// Build the email content.
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Message:\n$message\n";
// Build the email headers.
$email_headers = "From: $name <$email>";
// Send the email.
if (mail($recipient, $subject, $email_content, $email_headers)) {
// Set a 200 (okay) response code.
http_response_code(200);
echo "Thank You! Your message has been sent.";
} else {
// Set a 500 (internal server error) response code.
http_response_code(500);
echo "Oops! Something went wrong and we couldn't send your message.";
}
} else {
// Not a POST request, set a 403 (forbidden) response code.
http_response_code(403);
echo "There was a problem with your submission, please try again.";
}
?>
You have to put myemail and visitors emails in one array and then run a loop, the same message will be send to all in array.
e.g.
$aEmails = array();
$aEmails[] = 'myemail#something.com';
$aEmails[] = 'visitoremail#something.com';
foreach(aEmails AS aEmail){
mail($aEmail, $subject, $email_content, $email_headers);
}
If you are not getting any result then you must check your html forms' tag it should have method="POST" attribute.
Then in here remove the dot
$recipient = "myemail#mysite.com, .$email";
like this
$recipient = "myemail#mysite.com, $email";

PHP form to returning all the information

I am a complete novice when it comes to php, I have got the below php which sends me back the 'message' and sends an auto response to the user, as well as redirecting them to the 'thank you' page. Problem I am having is that it won't return the users name that they fill in on the form, any ideas?
<?php
$youremail = "ally.baird81#gmail.com"; //this is where the email will be sent to
#extract($_POST);$name = filter_var($name, FILTER_SANITIZE_STRING);
$message = filter_var($message, FILTER_SANITIZE_STRING);
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
if (mail($youremail, 'Message from website.', $message, "From: Krew Kut Hair<$email>")) {
$autoreply = "Thank you for enquiring at Krew Kut Hair, we will be in contact shortly";
$subject = "Thank you for your enquiry!";
mail($email, $subject, $autoreply, "From: Krew Kut Hair<$email>");
}
} else {
echo "Please enter a valid email address";
}
header("Location: thanks.html");
Assuming the name is in one of the form fields, you should be able to retrieve it. As Barmar says - all you have to do is use it somewhere in the body or the message. How can you tell the name is missing if you don't echo it out somewhere.
Try this:
$autoreply = "Thank you ".$name." for ...
If the name is still "missing" - you can try to see all the post variables like this:
echo "<PRE>Post Vars\n"; print_r($_POST);
If you have an input named "name" like:
<input type="text" name="name" value="" />
Check if it's containing data with e.g. :
echo 'The value of name is ['.$name.']';
If it is containing data you just can use the $name variable in your message. If it isn't there is probably something wrong in your HTML form.
<?php
$youremail = "ally.baird81#gmail.com"; //this is where the email will be sent to
#extract($_POST);$name = filter_var($name, FILTER_SANITIZE_STRING);
$message = filter_var($message, FILTER_SANITIZE_STRING);
$content = "<strong>Name:<strong><br />".$name."<br />";
$content .= "<strong>Message:<strong><br />".$message."<br />";
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
if (mail($youremail, 'Message from website.<br />', $content, "From: Krew Kut Hair<$email>")) {
$autoreply = "Hi ".$name.". Thank you for enquiring at Krew Kut Hair, we will be in contact shortly";
$subject = "Thank you for your enquiry!";
mail($email, $subject, $autoreply, "From: Krew Kut Hair<$email>");
}
} else {
echo "Please enter a valid email address";
}
header("Location: thanks.html");
Also read the comments on your question. I strongly recommend to find an other way instead of using extract().

Categories