add identifier to each page on site - php

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;
?>

Related

After sending email page is not loaded properly in Php

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.

Why my return file in php after form send doesn't work?

<?php
// Check for empty fields
if(empty($_POST['name']) ||
empty($_POST['phone']) ||
empty($_POST['email']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}
$name = strip_tags(htmlspecialchars($_POST['name']));
$phone = strip_tags(htmlspecialchars($_POST['phone']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
// Create the email and send the message
$to = 'info#bpmo-solutions.kz'; // 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 = "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";
$headers = "From: noreply#yourdomain.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 readfile(polychor_file.pdf);
?>
I have a response and all work correct with status 200, so why did I cannot take my file back? I need to show pdf file after the form sent.
You need to change the Header:
header("Content-type: application/pdf");
header("Content-Disposition: inline; filename=filename.pdf");
#readfile('path\to\filename.pdf');
Take a look at the following example:
How to display PDF in PHP

Send e-mail with page name in it

I currently have a contact form, from which e-mails are composed and sent using php. I use a separate php file for that.
$errors = '';
$myemail = 'mail#gmail.com';//
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['phone']))
{
$errors .= "\n Будь ласка, заповніть усі поля";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['phone'];
$page = getRequestURI();
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "Некоректна адреса e-mail";
}
if( empty($errors))
{
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "Нове замовлення туру: $page".
"Деталі:\n Ім'я: $name \n Email: $email_address \n Телефон \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}
I need to put such forms on different pages. And there should be information about the page on which form was submitted in the message sent by e-mail.
Is there a possibility to do that without creating separate php files for every page?
In other words how can I get the name (or url) of the page, on which form was submitted?
You can get the current request URI by using $_SERVER['REQUEST_URI'].
Assuming your script is located at http://example.com/some/page-here/more, $_SERVER['REQUEST_URI'] would be /some/page-here/more.

PHP Email contact-form

I've recently started using HTML, Javascript and PHP to create my own website.
I have been trying to make a PHP script that takes inputs from a form, validates with javascript and php, then sends the email.
I know the script is executed and runs fine, as it sucessfully redirects me to the new page at the end. However, it does not actually send any emails for some reason.
Here is the code:
<?php
$errors = '';
$myemail = '<removed due to privacy>';
if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email_address))
{
$errors .= "\n Error: Invalid email address";
}
if( empty($errors))
{
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $email_address\n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}
?>
Now I did see a few similar questions, but as I am very incompetent at php, they didn't really help.
If you can identify the reason, I'd be very grateful.
$sent = mail($to,$email_subject,$email_body,$headers);
put a condition like this
if($sent){ header('Location: contact-form-thank-you.html'); } else { echo 'mail failed';}
if it show "mail failed" configure your localhost to sent mails
follow this tutorials
http://blog.techwheels.net/send-email-from-localhost-wamp-server-using-sendmail/

Trying to get PHP to send email

I'm trying to get PHP to send an email to my email account but it's giving me this error:
http://gyazo.com/5e7870a814be520f2ba2dec32627c7e5
This is my code on contact_me.php
<?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 = $_POST['name'];
$email_address = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
// Create the email and send the message
$to = 'yourname#yourdomain.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 = "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: noreply#yourdomain.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;
?>
Thanks guys!
If you are using ubuntu, you need to configure the sendmail or postfix to send emails.
Configure the postix by using the following link : http://crazybrainz.in/configauration-of-postfix-for-mail-sending-through-php-and-magento/
provide some more information to solve your problem.

Categories