I want to send a email with input data from my html page. I am using next php script:
<?php
if((isset($_POST['budget']))&&(isset($_POST['type']))) {
$email_to = "design.er#icloud.com";
$email_subject = "Design";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['budget']) ||
!isset($_POST['type'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$email = $_POST['email']; // required
$budget = $_POST['budget']; // required
$type = $_POST['type']; // required
$comments = $_POST['comments']; // not required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
$string_exp = "/^[A-Za-z .'-]+$/";
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Email: ".clean_string($email)."\n";
$email_message .= "Budget: ".clean_string($budget)."\n";
$email_message .= "Type: ".clean_string($type)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- THERE MY PHP CODE, I REMOVED IT ESPECIALLY FOR STACKOVERFLOW, BUT IT EXIST -->
<?php
}
?>
But it doesn't work. When I am clicking on submit - it shows me a blank page.
I can't understand what the problem is.
Also, I tried next script and it worked perfectly:
<?php
$to = "xyz#somedomain.com";
$subject = "This is subject";
$message = "<b>This is HTML message.</b>";
$message .= "<h1>This is headline.</h1>";
$header = "From:abc#somedomain.com \r\n";
$header .= "Cc:afgh#somedomain.com \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$retval = mail ($to,$subject,$message,$header);
if( $retval == true ) {
echo "Message sent successfully...";
}else {
echo "Message could not be sent...";
}
?>
There is nothing showing because there is nothing to show. The only place in the script where there is an effort to display something is if the page is accessed without the proper post variables (in this case displaying the contents of the died() method). At the very bottom of this script there is an html comment with <!-- HTML CODE HERE --> and there's nothing there.
An absolutely blank page (i.e. even no output when you view the page's source in the browser) usually means that a PHP error occurred and error reporting is turned off. In this case you should take a look into the server's log files, if you can do that. (It might not be available on some hosted platforms.)
Common mistakes for such a "white screen of death" are syntax errors in the PHP script, but yours seems to be OK. However, some host providers disable some functions like the mail() function to avoid scripts that send spam emails. In this case, the mail() function could cause an internal error which stops the script execution at that point. Since this is before the point where any HTML code is printed in your script, this could be the cause of the error.
Related
Since a few days my server doesn't parse my php code (not only the attached file) anymore. If I call the php file via a html file it will just load forever.
The code is tested on a local apache2 server and there it works fine. I already tried to reinstall php5/apache2 on the server but still no progress.
The servers runs Ubuntu 14.04 LTS.
<?php
if(isset($_POST['email'])) {
$email_to = "placeholder#placeholder.com";
$email_subject = "Website Contact";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
// create email headers
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<html>
<p>Succesful!</p>
</body>
</html>
<?php
}
?>
ยด
Define your function outside any if statements. Not sure this is the reason for your error but it at least reads more logically. Also indent your code sensibly it helps a lot when looking for errors.
Also <p> tags belong on the <body and not in the <html> section of your HTML code.
Also if you have issues on a LIVE server, add some error reporting to your script, remember on Live servers errors are only written to logs, write then to the screen while you fox a bug its easier to see whats going wrong
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
if(isset($_POST['email'])) {
$email_to = "placeholder#placeholder.com";
$email_subject = "Website Contact";
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$email_message = "Form details below.\n\n";
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
// create email headers
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
}
?>
<html>
<body>
<p>Succesful!</p>
</body>
</html>
I took a standard form that I always use which included a comments are. For this website I don't need comments so I only left email and url in it. When I remove the everything related to comments in my PHP file it shows a blank page, not even an error.
Here's the code:
<?php
if(isset($_POST['email'])) {
// CHANGE THE TWO LINES BELOW
$email_to = "email#email.com";
$email_subject = "NEW CONTACT";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['email']) ||
!isset($_POST['url'])) || {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$email_from = $_POST['email']; // required
$url = $_POST['url']; // not required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "url: ".clean_string($url)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- place your own success html below -->
Success
<?php
}
die();
?>
You have extra || in your if condition. Remove that and it should work.
if(!isset($_POST['email']) ||
!isset($_POST['url'])) || {
^^^^
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
Hi Im trying to get a contact us page to get a email address and send it to an email. Everytime I click submit the php file just downloads. I have loaded php version file on the web server and its running php version 5.3.27 code below:
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "nishantrama#gmail.com";
$email_subject = "Sva Sva Spa Salon Coming Soon Notify Email";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if !isset($_POST['email']) ||
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$email_from = $_POST['email']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Email: ".clean_string($email_from)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
Any everytime I click submit on my webpage it just downloads the file.
Currently I have the code below which sends the contents of the html input field (email) to the welcome#domain.com address. There are two things which I am now struggling with. I have used the following bits of code to no avail -
$to = "welcome#domain.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thankyou.html');
How can I automatically send an email response from welcome#domain.com?
Instead of the page saying 'Thank you for contacting us, we will be in touch very soon' is there a way that they are redirected to domain.com/thankyou.html instead?
This is the rest of the code which seems to be working fine! If there are any suggestions for this though they are always more than welcome!
if(isset($_POST['email'])) {
$email_to = "welcome#domain.com";
$email_subject = "Your spot has been saved!";
function died($error) {
echo "We are very sorry, but there were error(s) found with the form your submitted";
echo "These errors appear below<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors<br /><br />";
die();
}
if(
!isset($_POST['email'])
) {
died("We are sorry, but there appears to be a problem with the form your submitted!");
}
$email_from = $_POST['email'];
$error_message = "";
$email_exp = "^[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$";
if(!eregi($email_exp,$email_from)) {
$error_message .= "The Email Address you entered does not appear to be valid!<br />";
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= 'Email: '.clean_string($email_from)."\n";
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
Thank you for contacting us. We will be in touch with you very soon.
<?
}
?>
Regarding page being not redirected refer this post stackoverflow.com/questions/6396813/php-header-not-redirecting
It basically says you must not output any value to stream before header call.
For mail issue please elaborate more what error you are getting.
i have a script that i have modified to meet my requirements however i now need to send the email to more than one person, could someone point me in the right direction as to how i could modify the script to send to more than one person.
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "emailremoved#sample.com";
$email_subject = "Kro Catering Website Enquiry";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['your_name']) ||
!isset($_POST['type']) ||
!isset($_POST['guests']) ||
!isset($_POST['date']) ||
!isset($_POST['phone']) ||
!isset($_POST['email'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$your_name = $_POST['your_name']; // required
$type = $_POST['type']; // required
$guests = $_POST['guests']; // required
$date = $_POST['date']; // not required
$phone = $_POST['phone']; // required
$email_from = $_POST['email']; // required
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Your Name: ".clean_string($your_name)."\n";
$email_message .= "Type: ".clean_string($type)."\n";
$email_message .= "Guests: ".clean_string($guests)."\n";
$email_message .= "Date: ".clean_string($date)."\n";
$email_message .= "Phone: ".clean_string($phone)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
<?php
header( 'Location: /thanks.aspx' ) ;
?>
<?php
}
?>
Search for the line:
$email_to = "emailremoved#sample.com";
And keep adding e-mails with a comma separating them:
$email_to = "emailremoved#sample.com,emailremoved#sample.com,emailremoved#sample.com";
PHP's mail() function is quite versatile when it comes to the "to" field. See the documentation here. Any one of the listed examples would be fine:
user#example.com
user#example.com, anotheruser#example.com
User <user#example.com>
User <user#example.com>, Another User <anotheruser#example.com>
So since your $email_to variable is not cleaned or otherwise modified after you set it on line 5, you should be able to just put 2 there separated by a comma (as in the examples above that I copied from the documentation I linked to.)
Try this!
It was the only code that worked for me.
$header .= 'Bcc: someaddress#email.com';