------THE CODE HAS BEEN MODIFIED SINCE FIRST POST------
For the life of me, I cannot get this to work. The page mentioned below will not recognise the email.php file in the root theme folder. I've tried everything!
Purpose: HTML contact form for a user to submit their details, PHP Script collects the users first name, last name and email address and in turn, redirects them to a thank you page.
Problem: HTML contact form displays fine, but when I click on "Okay, send me a voucher code!" it comes up with URL: http://charliesonlinestore.com/free-delivery-coupon-page/email.php with 'Nothing found', when it should be re-directing to a thank you page.
Here is the form code from HTML:
<div class="form">
<form method="post" name="landing-page" action="<?php echo get_template_directory_uri(); ?>/email.php">
First Name<br>
<input type="text" name="first_name"><br><br>
Last Name<br>
<input type="text" name="last_name"><br><br>
Email Address<br>
<input type="text" name="email"><br><br>
<input type="submit" name="submit" value="Okay, send me a voucher code!">
</form>
</div>
Here is my PHP Script code:
<?php
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email_from = $_POST['email'];
$to = 'charlie#charliesonlinestore.com';
$body = 'Here your email body text';
$subject = 'NEW Subscriber';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'From: Lead Generator <Charlie#charliesonlinestore.com>' .
"\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
if($_POST['submit']){
if(mail($to, $subject, $headers)){
echo "mail send"; // change this later to your header location
}
else{
echo 'error sending mail';
}
}
else{
echo 'no post data';
}
?>
Confirmed points:
The email.php is in the root of the theme folder, storefront.
All items in the example are provided in the root folder, storefront.
When I tested this on local host (separate server), it worked fine and redirects without issues.
If you have any ideas on why this is happening then I would greatly appreciate it. I've almost completed my very own custom built contact form, lead generation and thank you page!
Just to note, I have checked previous posts and tried things such as putting a '/' infront of the PHP file name and also tried putting the file server address infront of the file name but to no avail.
Please help!
It tooks a little time before i saw it. The problem is your mail function. The php mail function needs a message before headers. And you replace your $header variable instead of extending it.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$to = 'charlie#charliesonlinestore.com';
$body = 'Here your email body text';
$subject = 'NEW Subscriber';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'From: Your name <Charlie#charliesonlinestore.com>' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
if($_POST['submit']){
if(mail($to, $subject, $body, $headers)){
echo "mail send"; // change this later to your header location
}
else{
echo 'error sending mail';
}
}
else{
echo 'no post data';
}
?>
Related
I am working on a website where you can provide contact details and submit it to a PHP page. These contact details will be sent to an email address using the native PHP mail() function. However, everything seems fine and works with the following code:
<?php
$salutation = $_GET["salutation"];
$name = $_GET["name"];
$plan = $_GET["plan"];
$desc = $_GET["desc"];
$to = "example#email.com";
$subject = $desc;
$msg = "
<html>
<body>
<p>Test body</p>
</body>
</html>
";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to, $subject, $msg, $headers);
?>
When you submit the form, the end of the URL bar changes to:
page.php?salutation=dhr&name=Kees+van+Voorthuizen&plan=basic&desc=Test+Description
As I said, this works properly and no errors were thrown.
But when I add an <input name="email" id="email" type="email"> to the contact details page inside the <form> tag and I submit the form using my own email address, the server returns an HTTP error 503. PHP code using the email input field:
<?php
$salutation = $_GET["salutation"];
$name = $_GET["name"];
$email = $_GET["email"]; // <--- The only thing that has changed
$plan = $_GET["plan"];
$desc = $_GET["desc"];
$to = "example#email.com";
$subject = $desc;
$msg = "
<html>
<body>
<p>Test body</p>
</body>
</html>
";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to, $subject, $msg, $headers);
?>
The URL now ends in:
page.php?salutation=dhr&name=Kees+van+Voorthuizen&email=my%40email.com&plan=basic&desc=Test+Description
I suspect that the error is being thrown when I include a period ('.') inside the email field. I've tried manually changing the URL bar using my%40emailcom instead of my%40email.com and it worked. Notice that I removed the period between email and com.
Any ideas?
I am sending a confirmation mail to user to confirm his account. The confirmation mail is styled using HTML and has an href element which points the users to a PHP file where I do the confirmation process. This href link also needs to have a PHP randomstring attached, the same randomstring is saved in Database and is also sent to user so that the cross-checking can be done in PHP once the user clicks on it.
<td align="center" style="margin:0;text-align:center">
<a href="http://aliencreative.wehubs.com/new_alien/email.php?code=<?php echo $randomString; ?>"
style="font-size:21px;line-height:22px;text-decoration:none;color:#ffffff;font-weight:bold;
border-radius:2px;background-color:#0096d3;padding:14px 40px;display:block;
letter-spacing:1.2px" target="_blank">Confirm Alien account now!</a></td>
The PHP code includes the above HTML as follows.
<?php
$randomString=time();
//$random="http://aliencreative.wehubs.com/new_alien/email.php?code=".$randomString;
echo $random;
$to = 'sample#gmail.com';
$subject = "Confirmation mail";
// Get HTML contents from file
$htmlContent = file_get_contents("email_template.php");
// Set content-type for sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// Additional headers
$headers .= 'From: Alien creative control<alien#alien.com>' . "\r\n";
// Send email
if(mail($to,$subject,$htmlContent,$headers)):
$successMsg = 'Email has sent successfully.';
else:
$errorMsg = 'Some problem occurred, please try again.';
endif;
?>
However, the PHP variable is't getting available in the link.
Please try this. Remove file_get_contents and use include with output buffer function. I think this will insert your variable to your template. Please check the below updated code. Anyway your method is not a secure. Please create more complicated random code
<?php
$randomString=time();
//$random="http://aliencreative.wehubs.com/new_alien/email.php?code=".$randomString;
echo $random;
$to = 'sample#gmail.com';
$subject = "Confirmation mail";
// Get HTML contents from file
ob_start();
include "email_template.php";
$htmlContent=ob_get_contents();
ob_end_clean();
// Set content-type for sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// Additional headers
$headers .= 'From: Alien creative control<alien#alien.com>' . "\r\n";
// Send email
if(mail($to,$subject,$htmlContent,$headers)):
$successMsg = 'Email has sent successfully.';
else:
$errorMsg = 'Some problem occurred, please try again.';
endif;
?>
Forgive me, I am new at this and the different php I have tried hasn't really worked at all. I've found examples for Natural Language forms, but none with working PHP and I'm familiar with PHP when used with traditional forms, but I'm having problems putting the two together.
The general form layout goes like this:
My name is [your name].
Today, I am [whatchya gonna do?].
I'm doing this because [c'mon,why?].
It's important that I [what you said you'd do] because [the big picture].
Shoot me an email with my awesome new declaration at [your email]
Button 1 (sends email)
Button 2 (copies all text and input fields to clipboard -- not as important right now)
Button 1, I want to send a copy to myself with a hard coded email address and also send a copy to the user with the email address they've entered.
This is a little messy right now, as I am simply trying to get it to work... again, I have no included PHP because at this point -- I've confused myself so much that I don't know what to include.
<form method="post" action="todayiam.php">
<div class="naturallanguageform">
<div class="nlfinner">
<p class="line">
<span class="copy">My name is </span>
<span class="inputcontainer"><input class="textinput" name="name" value="" placeholder="[your name]">.</span>
<span class="copy">Today, I am </span>
<span class="inputcontainer"><input class="textinput" name="todayiam" value="" placeholder="[whatchya gonna do?]">.</span>
<span class="copy">I'm doing this because </span>
<span class="inputcontainer"><input class="textinput" name="why" value="" placeholder="[c'mon, why?]">.</span>
<span class="copy"> It's important that I </span>
<span class="inputcontainer"><input class="textinput" name="whatusaid" value="" placeholder="[what you said you'd do]"></span>
<span class="copy"> because </span>
<span class="inputcontainer"><input class="textinput" name="because" value="" placeholder="[the big picture]">.</span>
</p>
<span class="copy">Shoot me an email with my awesome new declaration at</span>
<span class="inputcontainer"><input class="textinput" name="email" value="" placeholder="[your email]"></span>
<p class="line">
<button class="button">Send to E-mail</button>
</p>
<p class="line">
<button class="button">Copy to post in comments</button>
</p>
</div>
</div>
</form>
Any assistance will be greatly appreciated.
Update:
<?php
// from the form
$name = trim(strip_tags($_POST['name']));
$email = trim(strip_tags($_POST['email']));
$message = htmlentities($_POST['todayiam']);
// set here
$subject = "Contact form submitted!";
$to = 'email#gmail.com';
$body = <<<HTML
$message
HTML;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
// send the email
mail($to, $subject, $body, $headers);
// redirect afterwords, if needed
header('Location: index.html');
?>
Another update
I have changed $headers = "From: $email\r\n"; to $headers = "From: email#gmail.com" . "\r\n" ; to set a static from address (my support email) and the email is still identifying as from CGI Mailer. I have verified that it's not a caching issue and the correct files are being used.
<?php
if (isset($_POST['name'], $_POST['todayiam'], $_POST['why'], $_POST['whatusaid'], $_POST['because'], $_POST['email']) {
// We enter this statement only if all the fields has been properly defined, this to avoid getting undefined index
$name = $_POST['name'];
$todayIam = $_POST['todayiam'];
$why = $_POST['why'];
$whatYouSaid = $_POST['whatusaid'];
$because = $_POST['because'];
$email = $_POST['email'];
// We define the variables for using in mail()
$to = 'email#gmail.com';
$to .= ', '.$email; // You wanted them to recieve a copy
$subject = "Contact form submitted!";
// You can put a lot more headers, check out the mail() documentation
$headers = "From: email#gmail.com" . "\r\n" ;
$headers .= "Content-type: text/html\r\n";
// Compose a $message from all the variables
$message = "My name is $name. ";
$message .= "Today, I am $todayIam.";
$message .= "I'm doing this because $why.";
$message .= "It's important that I $whatYouSaid";
$message .= "because $because.";
if (mail($to, $subject, $message, $header)) {
// Mail was successfully sent! Do something here
}
}
?>
Before you posted your answer, I was working on this script:
<?php
// from the form
$name = trim(strip_tags($_POST['name']));
$email = trim(strip_tags($_POST['email']));
$message = htmlentities($_POST['todayiam']);
// set here
$subject = "Contact form submitted!";
$to = 'email#gmail.com';
$body = <<<HTML
$message
HTML;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
// send the email
mail($to, $subject, $body, $headers);
// redirect afterwords, if needed
header('Location: index.html');
?>
The $email = trim(strip_tags($_POST['email'])); field was pulling the user's email and using it for the sent from as noted in the $header... so it was working fine. With the new script, that you posted, I can't get it to work with my hard coded email OR the user's email as the FROM. Initially, I really wanted to understand what the differences were between the script, why one worked and the other didn't, but now... I'd really just like my email to be hard coded as the FROM. I'll worry about the differences later. As I said before, I really have tried to get this to work in many different forms... I am sure it's something simple that I am over looking as a novice to PHP. Thanks.
Right, so after a bit of discussion from comments, I decided to post an answer instead, giving a bit more detail where it's more readable.
Your PHP code is missing the combination of all fields into $message. This can easily be done, as you can put a variable inside a string in PHP. I'll show you how. I'm also going to show you how you can avoid undefined indexes.
<?php
if (isset($_POST['name'], $_POST['todayiam'], $_POST['why'], $_POST['whatusaid'], $_POST['because'], $_POST['email']) {
// We enter this statement only if all the fields has been properly defined, this to avoid getting undefined index
$name = $_POST['name'];
$todayIam = $_POST['todayiam'];
$why = $_POST['why'];
$whatYouSaid = $_POST['whatusaid'];
$because = $_POST['because'];
$email = $_POST['email'];
// We define the variables for using in mail()
$to = 'email#gmail.com';
$to .= ', '.$email; // You wanted them to recieve a copy
$subject = "Contact form submitted!";
// You can put a lot more headers, check out the mail() documentation
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
// Compose a $message from all the variables
$message = "My name is $name. ";
$message .= "Today, I am $todayIam.";
$message .= "I'm doing this because $why.";
$message .= "It's important that I $whatYouSaid";
$message .= "because $because.";
if (mail($to, $subject, $message, $header)) {
// Mail was sucsessfullly sent! Do something here
}
}
?>
I have a simple message that I send using php mail().
The code used:
//recipient info
$to = "$bookernavn <$mail>";
$from = "Visens Venner Hillerød <booking#eksample.dk>";
$subject = "Kvittering - $a_titel - ". date("j/n - Y",$a_dato);
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=utf-8" . "\r\n";
$headers .= "From: $from" . "\r\n";
$headers .= "Reply-To: $from" . "\r\n";
$headers .= "Return-Path: $from" . "\r\n";
$headers .= "Bcc: $from" . "\r\n";
// now lets send the email.
mail($to, $subject, $mailmsg, $headers); }
For some strange reason two mails are sent each time...
Sometimes with several minutes in between...
Any ideas?
You don't check to see if the form has been submitted so a browser refresh will send the form data again and cause the mail to be sent again. This also happens when a user presses the back button.
After the email is sent you need to do a 303 redirect to prevent the re-submission. You can redirect to the same page if you'd like.
This is called the Post/Redirect/Get pattern.
mail(...);
header('Location: /some-page-php', true, 303);
exit;
an easy way to prevent this from happening is to use POST method instead of GET for the form.
<form method="post">
if (isset($_POST['submitted']))
and at the end of the mail code use a redirect that will send the browser to load using a GET method.
Not only you can then redirect your user to a OK page "mail was sent" or a error page "sorry there was a mistake, please try again", a refresh of that page open by the browser will only send a GET, not triggering the send mail function
if (empty($errors)) {
header('Location: http://www.example.com/mail_OK.html');
exit;
} else {
// passing data to the "error/retry" page
$info = array(
'msg' => $msg,
'email' => $_POST['email'],
'name' => $_POST['name']
// etc...
)
header('Location: http://www.example.com/mailform.php?'.http_build_query($info));
exit;
}
in your form you can retrieve those info
<input name="name" type="text" placeholder="Naam" class="form-control" value="<?php echo htmlspecialchars($_GET['name']); ?>">
i am working on one static website, in which there is a contact us page. Here what i want to do is when the contact form is submitted it should show the message that - Email has been sent successfully. But the problem is i am calling the html page and we cannot pass php message in html view. So is there any way to get it done.
conatctus.php
<?php
$error = '';
$mailTo = $_POST['email'];
$mailFrom = 'info#sample.com';
//$headers = 'MIME-Version: 1.0' . "\r\n";
//$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$fullname = $_POST['username'];
$phoneno = $_POST['mobile'];
$emailaddress = $_POST['email'];
$msgsubject =$_POST['message'];
$new = "\n";
$msg = $fullname.$new.$emailaddress.$new.$phoneno.$new.$msgsubject;
$to = $email;
$subject = 'Inquiry';
$messageclient = '<div>
<p>Thank you For Inquiry.</p>
<p> We will reach back to you shortly. Have a Nice Day!</p>
<p>Company © 2013</p>
</p></div>
';
$headers = 'From: info#email.com' . "\r\n" .
'Reply-To: info#email.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";
$headers .= "From: Company<info#email.com>\r\n";
//$res = mail($to, $subject, $message, $headers);
//$message ='Thank you For Inquiry. We will reach back to you shortly. Have a Nice Day';
mail( $mailTo , $subject, $messageclient, $headers);
$message .= "<p>Name: $fullname</p><br /><p>Contact Number : $phoneno</p><br /> <p>Email: $emailaddress</p><br /><p>Message: $msgsubject</p>";
mail( $mailFrom, $subject,$message, $headers);
header("location:home.html");
?>
Your help is much appreciated, thanks in advance.
There are a lot of problems here, but to answer your question, you can't redirect after content has been sent.
If you add ob_start() to the top of the page it will buffer the contents and allow the redirect.
Upon, further re-reading of your post, maybe I misinterpreted. It doesn't look like you are sending content which means that your redirect IS working, but what you want is to add a message after it's been redirected.
You have options.
Redirect to a static HTML page that reflects the message you want to convey.
Redirect to a PHP page that has the logic to give the user a message.
Use Ajax to send the email and don't redirect at all.
You might want to do either an ajax request on the submit to prevent changing the page or make a landing page to which you will redirect after processing the mailing function
Have this email sent function performed as an ajax call and on it's success show user the success message.
If you can configure the web server, you can change it in a way to treat html pages like php pages. For example in APACHE's httpd.conf:
AddType application/x-httpd-php .php .htm .html
Hope that works for you.