PHP/Wordpress - Trying to mail an order form - php

I have a rather large form built, and unfortunately my friend is using WordPress. After much research of how to get PHP to run on an individual page, I created a custom template for the page and inserted my PHP which is to read that it was a post, and send a wp_email accordingly.
My form starts out:
<form name="healthyOrderForm" method="POST" action="/order-sent" enctype="text/plain">
the /order-sent does send it to the correct page as I can see my php response when it gets there.
Form submit button is:
<input type="submit" name="send" value="Submit Order" />
PHP is:
<?php
if (isset($_POST['submit'])) {
$to = 'testemail#gmail.com';
$subject = 'New Healthy Order';
$message .= 'Location: ' . $_POST['location'] ;
$headers = "From: randomemail#gmail.com\r\n";
$headers .= 'Content-Type: text/plain; charset=utf-8';
$success = wp_mail( $to, $subject, $message, $headers);
}
?>
Any help would be greatly appreciated as I have been stuck on this for over 24 hours now.

You have no $_POST['submit']. The name of your submit is $_POST["send"]. Check on that.
EDIT
And, as Fred-ii- pointed me to that, remove the enctype="text/plain" from your form tag.

Related

Trying to get an Email to send on button click, but it keeps displaying an Error [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 2 years ago.
I'm working on a project that requires a specific button on a page to send an email out. It works as a sort of reminder function for the client, so when it's clicked they will receive an email to let them know that they need to check something.
I think I almost have it working, but for some reason it keeps failing instead of actually sending the email out. It could be the way that I have it set out, but I have tried using the "action" on the form to run the php through a seperate file, but it still does the same. I currently have it like this:
<form method='post'>
<button type='submit' name='reminder_btn'>
Send Reminder
</button>
</form>
<?php
if(isset($_POST['reminder_btn'])){
$email = "hello#email.com";
$subject = "Company Reminder";
$message = "
<html>
<head>
<title>Company</title>
</head>
<body>
<p>Hello!</p>
<p>You have a reminder. Please check the website.</p>
<p>Thanks!</p>
</body>
</html>
";
$mailheaders[] = "From: Company Name";
$mailheaders[] = "Reply-To: hello#email.com";
$mailheaders[] = "MIME-Version: 1.0";
$mailheaders[] = "Content-type: text/html; charset=iso-8859-1";
if(mail($email, $subject, $message, implode("\r\n", $mailheaders))) {
echo "Reminder sent.";
} else {
echo "Something has gone wrong. Try again.";
}
}
I'm sure I've had it working like this on something else before, but all I'm getting is the "Something has gone wrong" message each time.
Obviously I've got something wrong but I don't understand what it is, so any assistance here would be much appreciated.
You forgot the action on the form. I have used PHP SELF but you can replace it with your page. PHP SELF sends the POST data to the same page.
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method='post'>
<button type='submit' name='reminder_btn'>
Send Reminder
</button>

PHP mail() form doesn't complete sending e-mail

<?php
ini_set("mail.log", "/tmp/mail.log");
ini_set("mail.add_x_header", TRUE);
define("TITLE", "Contact Us | MicroUrb");
include('includes/header.php');
/*
NOTE:
In the form in contact.php, the name text field has the name "name".
If the user submits the form, the $_POST['name'] variable will be automatically created and will contain the text they typed into the field. The $_POST['email'] variable will contain whatever they typed into the email field.
PHP used in this script:
pre_match()
- Perform a regular expression match
- http://ca2.php.net/preg_match
$_POST
- An associative array of variables passed to the current script via the HTTP POST method.
- http://www.php.net/manual/en/reserved.variables.post.php
trim()
- Strip whitespace (or other characters) from the beginning and end of a string
- http://www.php.net/manual/en/function.trim.php
exit
- output a message and terminate the current script
- http://www.php.net/manual/en/function.exit.php
die()
- Equivalent to exit
- http://ca1.php.net/manual/en/function.die.php
wordwrap()
- Wraps a string to a given number of characters
- http://ca1.php.net/manual/en/function.wordwrap.php
mail()
- Send mail
- http://ca1.php.net/manual/en/function.mail.php
*/
?>
<div id="contact">
<hr>
<h1 class="contact">Get in touch with us!</h1>
<?php
// Check for header injections
function has_header_injection($str) {
return preg_match("/[\r\n]/", $str);
}
if (isset($_POST['contact_submit'])) {
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$msg = $_POST['message'];
// Check to see if $name or $email have header injections
if (has_header_injection($name) || has_header_injection($email)) {
die(); // If true, kill the script
}
if (!$name || !$email || !$msg) {
echo '<h4 class="error">All fields required.</h4>Go back and try again';
exit;
}
// Add the recipient email to a variable
$to = "renaissance.scholar2012#gmail.com";
// Create a subject
$subject = "$name sent you a message via your contact form";
// Construct message
$message = "Name: $name\r\n";
$message .= "Email: $email\r\n";
$message .= "Message:\r\n$msg";
// If the subscribed checkbox was checked
if (isset($_POST['subscribe']) && $_POST['subscribe'] == 'Subscribe') {
// Add a new line to message variable
$message .= "\r\n\r\nPlease add $email to the mailing list.\r\n";
}
$message = wordwrap($message, 72);
// Set mail headers into a variable
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: $name <$email> \r\n";
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: High\r\n";
// Send the email
mail($to, $subject, $message, $headers);
?>
<!-- Show success message afte email has been sent -->
<h5>Thanks for contacting MicroUrb!</h5>
<p>Please allow 24 hours for a response.</p>
<p>« Go to Home Page</p>
<?php } else { ?>
<form method="post" action="" id="contact-form">
<label for="name">Your name</label>
<input type="text" id="name" name="name">
<label for="email">Your email</label>
<input type="email" id="email" name="email">
<label for="message">and your message</label>
<textarea id="message" name="message"></textarea>
<input type="checkbox" id="subscribe" name="name" value="Subscribe">
<label for="">Subscribe to newsletter</label>
<input type="submit" class="button next" name="contact_submit" value="Send Message">
</form>
<?php } ?>
<hr>
</div>
<?php include('includes/footer.php'); ?>
I've tried creating a simple mail form. The form itself is on my contact.php page. I would like to test that the code submits perfectly and sends the email.
I knew that there would be a chance that via MAMP or MAMP Pro the sending of the email would not work, but I have found documentation that says you can make it work. I followed this documentation:
https://gist.github.com/zulhfreelancer/4663d11e413c76c6393fc135f72a52ce
and it did not work for me. I have tried reaching out to the author to no avail.
This is what I have tried to do thus far:
I have made sure error reporting is enabled and set to report all errors and I have that code in my index.php file here:
<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 'On');
define("TITLE", "Home | MicroUrb");
include('includes/header.php');
?>
<div id="philosophy">
<hr>
<h1>MicroUrb's Philosophy</h1>
<p>Here at MicroUrb we guarantee you organically grown produce, because we grew it ourselves and we are local. We're not pompous, we're proud. We're proud of our work, our quality, our environment and our love for fresh local produce and family.</p>
<p>Thank you, from your local urban family farm.</p>
<hr>
</div><!-- philosophy -->
<?php
include('includes/footer.php');
?>
If you look at my code in contact.php, you will see that the mail() function is being called.
I checked MAMP Pro logs and for Postfix I had nothing because Postfix appears to not be running (could this be the problem?) For the Apache logs I had this error:
unknown: warning: /etc/postfix/main.cf, line 696: overriding earlier entry: inet_protocols=all
sendmail: warning: /etc/postfix/main.cf, line 676: overriding earlier entry: inet_protocols=ipv4
sendmail: warning: /etc/postfix/main.cf, line 696: overriding earlier entry: inet_protocols=all
postdrop: warning: /etc/postfix/main.cf, line 696: overriding earlier entry: inet_protocols=all
but which was caused by following Step 2 of the link I shared above, where it directed me to add this:
myorigin = live.com
myhostname = smtp.live.com
relayhost = smtp.live.com:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_sasl_mechanism_filter = plain
inet_protocols = ipv4
smtp_use_tls = yes
smtp_tls_security_level=encrypt
tls_random_source=dev:/dev/urandom
Line eight above was conflicting with inet_protocols = all, so I deleted line 8 above and the problem went away, but the original problem of not being able to send mail is still there.
I am not using an error suppression operator.
I may need to check to see if mail() returns true or false.
I checked spam folders of the email accounts I tried sending mail to.
I believe I have supplied all mail headers:
// Set mail headers into a variable
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: $name <$email> \r\n";
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: High\r\n";
I believe the recipient value is correct:
// Send the email
mail($to, $subject, $message, $headers);
I tried multiple email accounts, same issue.
I believe the form method matches the code.
Is Postfix on my MAMP Pro not working the problem?
I enabled PHP custom mail.log.
Should I just call it quits and use a different mailer or different way of developing my contact form in PHP that will be less of a headache?
So I believe my question is not a duplicate of the other posting in question is because as outline above, I have gone through the troubleshooting steps of that very SO posting and it has not solved my problem.
I can only guess my problem is either more specific to getting Postfix working on my MAMP Pro. I am not checking if mail() returns true or false correctly or I just may need to go with PHPMailer or some alternative.

html contact form doesn't recognise php file

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

Html/php email send [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I am about to write a little html/php script. But I am not able to send data from the HTML form input to any email address or into any text file. I was already searching for possible solutions but nothing worked for me. The Browser replayed the php script. But no mail has been send. Any help will be appreciated. Thank you.
I recommend you use PHPMailer his use is very easy
Do not forget the action and method attributes in the <form> tag.
contents of html file
<form action="send.php" method="POST">
<input type="text" name="name" placeholder="Typ your name..." />
<input type="email" name="from" placeholder="Typ your e-mailaddress..." />
<textarea name="message" placeholder="Typ your message..."></textarea>
<button type="submit">Send E-mail</button>
</form>
contents of send.php
<?
if(isset($_POST)) {
$name = $_POST['name'];
$message = $_POST['message'];
$from = $_POSST['from'];
if(!empty($name) && !empty($message) {
$subject = 'message from '.$name;
$headers = "From: " . strip_tags($from) . "\r\n";
$headers .= "Reply-To: ". strip_tags($from) . "\r\n";
//$headers .= "CC: susan#example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
// #SEE: http://php.net/manual/en/function.mail.php
if(mail('[YOUR-ADDRESS]', $subject, $message, $headers)) {
echo 'Thx 4 msg!';
}
else {
echo 'Oh nooos, The msg was not send.';
}
}
else {
echo 'You should provide the fields with some data..';
}
}
?>
One should first sanitize the user input obviously.
I think your issue is how to handle form data with some code, so you may be able to send an email or write form data to a file. This is where you see the difference between client-side and server-side. HTML is a language to describe documents, here your form: the text input name is going to describe a name, the form will send its data within the POST method, and so on. The file describing HTML is processed in your browser. And your browser don't send an email or write data... That's why you should use a server-side language such as PHP to get things done. PHP is great to help you process data and behave on different event... In your case, great for receiving data, analyze data and then send data through mails or save data into a file.
So now you may want to understand how to do so... Mail are a little tricky since you may need to configure things such as mail server, authentification and so one. Maybe a good solution is to try mails with a Google account or something like that... When it will be done, you may simply send an email like so:
<?php
$to = 'your#email.here';
$subject = 'Mail test';
$data = $_POST['name']; // if a `name` field exist and your form send its data through POST method
mail($to, $subject, $data);
Writing things to a file is simpler, it only request permissions to read and/or write a file.
<?php
$file = 'path/to/file';
file_put_contents($file, $_POST['name'] . ' for example');
So this is globally everything:
index.html HTML file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Form</title>
</head>
<body>
<form action="process.php" method="post">
<input name="name" type="text" placeholder="Name" />
<input type="submit" value="Process">
</form>
</body>
</html>
and process.php PHP file
<?php
/**
* Testing data
*/
if (!isset($_POST['name'])) {
die('No value `name` found');
}
/**
* Configuring process
*/
$to = 'your#email.here';
$subject = 'Mail test';
$data = $_POST['name'];
/**
* Saving data
*/
$res = file_put_contents(
'data.txt',
$data."\r\n"
);
if ($res !== false) {
echo 'data saved'.PHP_EOL;
} else {
echo 'error while saving data'.PHP_EOL;
}
/**
* Sending email
*/
$res = mail(
$to,
$subject,
$data
);
if ($res === true) {
echo 'mail sent';
} else {
echo 'error while sending mail'.PHP_EOL;
}
I suggest you to read mail() and file_put_contents() documentations to understand their behavior in case there are errors... :)

How To Make 'Submit' Button Refresh Page?

On my website I have a 'Contact Us' form.
Currently, I am using a php form to have the information that has been filled out on the form to be e-mailed to my e-mail.
I wanted an echo message to come up once someone clicks on submit, but instead it's going to another page with the message there.
I just want the page to refresh.
This is the PHP Code:
<?php
//echo $emailBody;
sendEmail();
function sendEmail(){
global $emailBody, $name, $from, $fromName, $templateFile, $to, $subject;
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers .= "Reply-To: ". $from . "\r\n";
$headers .= "From: ". $from . "\r\n";
$headers .= "Return-Path: ".$from;
if (!mail($to, $subject, $emailBody, $headers)) {
} else {
echo "Your Message has been sent!we will contact back you in a short moment!";
}
}
?>
PHP will always send you a new page because it is a server side language. In order for this code to be executed when a user clicks the button and then the results shown to the user, the request must be sent to the server, executed, and then sent back to the user. The only way you can 'refresh' a page like you are requesting is to use a client side language such as javascript.
It is like this: When user clicks the submit button they are requesting that PHP process the request. This can only take place on the server. Because the request goes from user to server and then back to user, you will always get a new page.
Need a little more code, but typically when doing something like this, you would have the form submit back to iself and at the top of the php, do some isset(-form variable -) checking, if they are filled out then you can enter a function to process from there and subsequently echo a message to the user indicating success/failure.
I'd use:
<script type="text/javascript">
setTimeout(function(){location.reload();},1000);
</script>
<noscript>
<meta http-equiv="refresh" content="1" />
</noscript>
You can also specify this with a header (the meta-refresh only emulates this header):
header('Refresh: 1');
It is also nice to leave the user a message such as:
<div>If this page doesn't automatically refresh in one second click here.</div>
before you echo you can redirect to the source page with the message that you want to show up there
for example
if (!mail($to, $subject, $emailBody, $headers)) {
} else {
$myMsg="Your Message has been sent!we will contact back you in a short moment!";
header('location: yourpage');
}
and in your page you can check out if there is any message like this
if(isset($myMsg))
{
echo $myMsg; // in the place & style that you want
}
change your action
<form action="This is where your webpage goes" method="POST">
so this will send your page to google:
<form action="http://google.com" method="POST">
if you remove the action, it will reload the current page, like this:
<form method="POST">
just put your php in the same page as your <form>.
like the following
<?php
if($_POST['submit'] == 'send'){
sendEmail();
function sendEmail(){
global $emailBody, $name, $from, $fromName, $templateFile, $to, $subject;
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers .= "Reply-To: ". $from . "\r\n";
$headers .= "From: ". $from . "\r\n";
$headers .= "Return-Path: ".$from;
if (!mail($to, $subject, $emailBody, $headers)) {
} else {
echo "Your Message has been sent!we will contact back you in a short moment!";
}
}
}
//For that to work your button needs to be like this
?>
<form method="POST">
<!-- other fields here -->
<input type="submit" name="submit" value="send" />
</form>

Categories