I want to send the email from the server when someone submits the contact form. Here is the sample php code.
<?php
if($_SERVER['REQUEST_METHOD'] == "POST"){
$name = strip_tags(trim($_POST['name']));
$email = strip_tags(trim($_POST['email']));
$dept = strip_tags(trim($_POST['dept']));
$message = strip_tags(trim($_POST['message']));
if((!isset($email) || empty($email)) || (!isset($name) || empty($name)) || (!isset($dept) || empty($dept)) || (!isset($message) || empty($message))){
echo -1;
return;
}
if(filter_var($email, FILTER_VALIDATE_EMAIL)===false){
echo -2;
return;
}
$to = 'farooqahmadkhan003#gmail.com';
$email_subject = "Splendenti Eye Wear | Contact Us";
$email_body = "Contacted by\nName: $name\nEmail: $email\nDepartment: $dept\nMessage: $message";
$headers = "noreply#farooq.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply#yourdomain.com.
$headers .= "Reply-To: ".$_POST['email'];
if(mail($to,$email_subject,$email_body,$headers)){
echo 1; // email sent
}
else{
echo "failed to send email";
}
}else{
echo "Invalid Request";
}
?>
but the mail function is returning false. Can anybody guide??
I suggest you to use PHPMailer to send emails from your PHP files.
Check this link and download it. There are many documentations about PHPMailer online. You can control all the details about your mail with PHPMailer.
try to use # to suppress warnings:
if(#mail($to,$email_subject,$email_body,$headers)){
echo 1; // email sent
}
If this doesn't help you have a configuration issue.
Related
I am using php mail() for sending email. with this function mail is being sent properly. But after sending mail page is not not loaded properly. Actually I want to land on same page after sending email.
following is the code that I have written
if (isset($_POST['submit'])) { // Check if form was submitted
if (empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['phone']) ||
empty($_POST['subject']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
echo "No arguments Provided!";
return false;
}
$name = strip_tags(htmlspecialchars($_POST['name']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$phone = strip_tags(htmlspecialchars($_POST['phone']));
$subject = strip_tags(htmlspecialchars($_POST['subject']));
$message = strip_tags(htmlspecialchars($_POST['message']));
// Create the email and send the message
$to = 'myemail#gmail.com'; // Add your email address inbetween the '' replacing yourname#yourdomain.com - This is where the form will send a message to.
$email_subject = "Subjet : $subject";
$email_body = "You have received a new message from your website contact form.\n\n" . "Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$message";
$headers = "From: myemail#gmail.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply#gmail.com.
$headers .= "Reply-To: $email_address";
mail($to, $email_subject, $email_body, $headers);
header("Refresh:0");
return true;
// exit();
}
this is contact us page
after sending email this page is not loaded completely only header is loaded
It is probably because the mail sending fails.
You can see the error it is returning by adding the following on top of the PHP file:
ini_set('display_errors', 1);
Also, it is advisable to use PHPMailer, because the mail() function doesn't always work.
So my website has multiple pages where I want people to have an option to send us a message. I already have setup a php mailer but all the mails have the same layout.
How can I make to where if the message has been send form a specific page I add specific words to the mail?
The mailsender works on both the pages but the layout and everything is the same.
THIS IS THE CODE THAT WORKS FOR ME:
<?php
// Check for empty fields
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['phone']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}
$name = strip_tags(htmlspecialchars($_POST['name']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$phone = strip_tags(htmlspecialchars($_POST['phone']));
$message = strip_tags(htmlspecialchars($_POST['message']));
$URL = $_SERVER['HTTP_REFERER'];
// Create the email and send the message
$to = 'boonwijkkermis#gmail.com'; // Add your email address inbetween the '' replacing yourname#yourdomain.com - This is where the form will send a message to.
$email_subject = "Website Contact Form: $name";
$email_body = "Nieuwe mail ontvangen via boonwijkkermis.com.\n\nNaam:\n $name \n\nEmail:\n $email_address \n\nTelefoon nummer:\n $phone \n\nBericht:\n$message \n\nURL:\n $URL";
$headers = "From: no-reply#boonwijkkermis.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply#yourdomain.com.
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
?>
What you need to do is to add an hidden field in each specific page to be able to understand where the message request came from, such as:
On a page:
<input type='hidden' name='from' value='page1'>
On another page:
<input type='hidden' name='from' value='page2'>
Then in your php file check for the $_POST['from'] value to be able to tell from which page the request came from.
So you would do something like that:
<?php
// Check for empty fields
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['phone']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}
$name = strip_tags(htmlspecialchars($_POST['name']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$phone = strip_tags(htmlspecialchars($_POST['phone']));
$message = strip_tags(htmlspecialchars($_POST['message']));
if(!empty($_POST['from'])) { //if the from post var is not empty
if($_POST['from'] == 'page1') { //if request came from page1
$message .= " requested from page1"; //we append this text in the $message var
}
}
// Create the email and send the message
$to = 'boonwijkkermis#gmail.com'; // Add your email address inbetween the '' replacing yourname#yourdomain.com - This is where the form will send a message to.
$email_subject = "Website Contact Form: $name";
$email_body = "Nieuwe mail ontvangen via boonwijkkermis.com.\n\nNaam:\n $name \n\nEmail:\n $email_address \n\nTelefoon nummer:\n $phone \n\nBericht:\n$message";
$headers = "From: no-reply#boonwijkkermis.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply#yourdomain.com.
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
?>
In alternative if you are not able to add that hidden field, you can use the $_SERVER['HTTP_REFERER'] header in your php code, that would contain the full URL from where the request came from.
This works for me
<?php
// Check for empty fields
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['phone']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}
$name = strip_tags(htmlspecialchars($_POST['name']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$phone = strip_tags(htmlspecialchars($_POST['phone']));
$message = strip_tags(htmlspecialchars($_POST['message']));
$URL = $_SERVER['HTTP_REFERER'];
// Create the email and send the message
$to = 'boonwijkkermis#gmail.com'; // Add your email address inbetween the '' replacing yourname#yourdomain.com - This is where the form will send a message to.
$email_subject = "Website Contact Form: $name";
$email_body = "Nieuwe mail ontvangen via boonwijkkermis.com.\n\nNaam:\n $name \n\nEmail:\n $email_address \n\nTelefoon nummer:\n $phone \n\nBericht:\n$message \n\nURL:\n $URL";
$headers = "From: no-reply#boonwijkkermis.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply#yourdomain.com.
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
?>
I'm using code from another source but it's very different to what I'm used to coding when using standard HTML/PHP websites. Wordpress seems to have a mind of it's own. Basically I need the contact form's PHP to send the name and email within the body text of the email and not just the persons message. Currently all that comes through is the persons message.
<?php
//response generation function
$response = "";
//function to generate response
function my_contact_form_generate_response($type, $message){
global $response;
if($type == "success") $response = "<div class='success'>{$message}</div>";
else $response = "<div class='error'>{$message}</div>";
}
//response messages
$not_human = "Human verification incorrect.";
$missing_content = "Please Fill in all Required Fields.";
$email_invalid = "Your Email Address is Invalid.";
$message_unsent = "Message was not sent. Please Try Again.";
$message_sent = "Thanks! Your message has been sent.";
//user posted variables
$name = $_POST['message_name'];
$email = $_POST['message_email'];
$message = $_POST['message_text'];
$human = $_POST['message_human'];
//php mailer variables
$to = get_option('admin_email');
$subject = "New Enquiry Through Mon Voyage Website";
$headers = 'From: '. $email . "\r\n" .
'Reply-To: ' . $email . "\r\n";
if(!$human == 0){
if($human != 2) my_contact_form_generate_response("error", $not_human); //not human!
else {
//validate email
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
my_contact_form_generate_response("error", $email_invalid);
else //email is valid
{
//validate presence of name and message
if(empty($name) || empty($message)){
my_contact_form_generate_response("error", $missing_content);
}
else //ready to go!
{
$sent = wp_mail($to, $subject, strip_tags($message), $headers);
if($sent) my_contact_form_generate_response("success", $message_sent); //message sent!
else my_contact_form_generate_response("error", $message_unsent); //message wasn't sent
}
}
}
}
else if ($_POST['submitted']) my_contact_form_generate_response("error", $missing_content);
?>
Any ideas?
add this in just before if(!$human == 0){:
$message.='Name: '.$name.'email: '$email;
this is all standard php form checking, 1. function to display a reponse 2. check the $_POST data 3. fail if errors in place 4. if no errors, send email. Put a few echo's in there and play around with it to see what is happening.
This does not solve the issue you are having directly but instead of writing the code yourself you could use Contact form 7 plugin instead. It will save you time and is very quick to implement.
You can get the plugin from this url.
Here is the documentation for implementing the plugin.
You can add a contact form by adding the short code generated by the plugin to the page you want it to appear like this:
[contact-form-7 id="1234" title="Contact form 1"]
I am new to php and I am having issues with the php mail function. I am testing on a live server, not localhost. When I complete my form I get no response from the mail function. I'm not sure what i'm doing wrong.
<?php
if (($_SERVER['REQUEST_METHOD'] == 'POST') && (!empty($_POST['action']))):
if (isset($_POST['fullname'])) { $fullname = $_POST['fullname']; }
if (isset($_POST['email'])) { $email = $_POST['email']; }
if (isset($_POST['phone'])) { $phone = $_POST['phone']; }
if (isset($_POST['form_message'])) {
$form_message = filter_var($_POST['form_message'], FILTER_SANITIZE_STRING); }
$form_errors = false;
if( $fullname === '') :
$form_errors = true;
endif;
if ($form_message === '') :
$form_errors = true;
endif;
if (!$form_errors) :
$to = "email#address.com";
$subject = "From $fullname --VTS Specialist Contact Form";
$message = "$form_message";
$replyto = "From: $email \r\n" .
"Reply-To: email#address.com";
if(mail($to, $subject, $message)):
$msg = "Thanks for reaching out to VTS Specialist, We will get back to you as soon as possible!";
else:
$msg = " Sorry your message could not be sent, try again.";
endif; # mail form data
endif; #check for form errors
endif;
?>
Try adding an echo statement at the bottom - as Mario mentions, you are only declaring it, you're not displaying it.
if(mail($to, $subject, $message)):
$msg = "Thanks for reaching out to VTS Specialist, We will get back to you as soon as possible!";
else:
$msg = " Sorry your message could not be sent, try again.";
endif; # mail form data
echo $msg;
I have put together a form with validation with help from this tutorial: http://www.w3resource.com/php/form/php-form-validation.php
I am hoping somebody can point out where I have gone wrong? As my validation works, I get a thankyou message but no mail comes through to my inbox :(
<?php
if (isset($_POST['submit'])) {
//checking name
if(empty($_POST['full_name']))
$msg_name = "*";
$name_subject = $_POST['full_name'];
//check phone number
if(empty($_POST['Phone_Num']))
$msg_Phone = "*";
$email_subject = $_POST['Phone_Num'];
//check email
if(empty($_POST['email_addr']))
$msg_email = "*";
$email_subject = $_POST['email_addr'];
$email_pattern = '/^\w+([\.-]?\w+)*#\w+([\.-]?\w+)*(\.\w{2,3})+$/';
preg_match($email_pattern, $email_subject, $email_matches);
if(!$email_matches[0])
$msg2_email = "Please enter a valid email address";
}
// validation complete
if(isset($_POST['submit'])){
if($msg_name=="" && $msg2_name=="" && $msg_email=="" && $msg2_email=="" && $msg2_Message=="")
$msg_success = "Thankyou for your enquiry";
//send mail
$to = "someone#email.com";
$subject = "Data collected thorugh from";
$message = "<p>".$_POST["full_name"]."</p><p>".$_POST["Phone_Num"]." </p><p>".$_POST["email_addr"]."</p><p>".$_POST["Message"]."</p>";
$from = "someone#email.com";
mail($to,$subject,$message);
}
?>
The PHP mail() function does not allow for sending email through SMTP with authentification. If you want do this, there is a PEAR package.
This article gives some explanation.