created a simple little php code to populate email with email address and info from a textbox on the form. it originally worked when I was calling to the script from a html form, but once I converted my site to PHP it stopped working. Eventually I would like to put this same info into a Database table but right now I would be content just getting the email to work.
When the form is submitted I get the email address from the customer, but I don't get the info from the textbox. here is my code.
<?php
$email = $_POST['email'];
$message = $_POST['message'];
mail( "sales#sixtoed-design.com", "Service Request", "From: $email",
$message );
header( "Location: http://www.sixtoed-design.com/thankyou.php" );
?>
Like I said it is a very simple code and worked fine before I converted my site completely to PHP.
Below is my code for the form if you need it.
<form method="POST" action="sendmail.php" enctype="multipart/form-data">
Email: <input name="email" type="text" /><br />
Message:<br />
<textarea name="message" rows="15" cols="40">
</textarea><br />
<input name="" type="submit" value="Send Email">
</form>
You're using the mail function wrong. See the docs.
The 3rd parameter should be the message and you send from as a header in the 4th parameter, so:
mail( "sales#sixtoed-design.com", "Service Request", $message, "From: $email" );
See example 2 in the docs
Related
So I wanted to make a contact form for a website that can actually send emails, but ended up with this error :
Here's my html code:
<div class="contact-fast">
<div class="contact-form">
<form id="contact-form" method="POST" action="contact-form-handler.php">
<input name="name" type="text" class="form-control" placeholder="Your Name" required> <br>
<input name="email" type="email" class="form-control" placeholder="Your Email" required> <br>
<textarea name="message" class="form-control" placeholder="Message" rows="8" required></textarea> <br>
<input type="submit" class="form-control submit" value="SEND MESSAGE">
</form>
</div>
</div>
And here's my PHP code:
<?php
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
$email_from = 'skeremobiel#gmail.com';
$email_subject = 'New Mail From Website';
$email_body = "Username: $name.\n".
"User Email: $visitor_email.\n".
"User Message: $message.\n";
$to = "said444b#gmail.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
if ($visitor_email!=NULL) {
mail($to,$email_subject,$email_body,$headers);
}
header("Location: http://127.0.0.1:5500/social.html");
die()
?>
Here's how my page looks like before and after submitting the form:
I think the problem is that when i click on the submit button i get directed to 127.0.0.1:5500/contact-form-handler.php (as you can see in the last image in my question). It doesn't execute the php file, but it just opens it.
Any help would be appreciated!
Many things can go wrong.
Make shure php error reporting is on. Google for that.
Try to find where the error code is coming from by reducing your problem.
For instance, comment out mail() and see if it works.
Or replace your php code by a simple text to see if the code runs, if the text appears on your browser screen.
Or Google for error 405. You will learn it is an http error, something in the data exchange between your browser and the server.
Do you have characters before <?php because if you have, the server will not be able to output the HTTP header any more.
Is your php file located at the webserver root and called contact-form-handler.php?
After manually submitting a form on my website, an email with the subject appears in my inbox. The email's subject is correctly sent as "Feedback Form Submission", but the email itself is blank. There is no body nor input from the email box on my form.
Form:
<form action="feedback-form.php" method="post" enctype="text/plain">
E-mail:<br>
<input type="text" class="textForm" name="email_address" size="35"><br>
Comment:<br>
<textarea name="feedback" class="textForm" rows="6" cols="35"></textarea><br><br>
<input type="submit" id="submit" class="button" value="Send">
</form>
PHP:
<?php
#Receive user input
$email_address = $_POST['email_address'];
$feedback = $_POST['feedback'];
#Filter user input for invalid characters
function filter_email_header($form_field)
{
return preg_replace('/[nr|!/<>^$%*&]+/', '', $form_field);
}
$email_address = filter_email_header($email_address);
#Send email
$headers = "From: $email_address";
$sent = mail('me#website.com', 'Feedback Form Submission', $feedback, $headers);
I believe my mail function is set up properly. My variables in the form match the variables my PHP script is using. My PHP and HTML are in the same file. I have read in other questions that having the two pieces of code in the same file can lead to issues when a blank form is submitted, but I am having issues while inputting valid information into my form. What am I doing incorrectly here?
I'm trying to create a PHP form that allows users to insert their email address and it automatically sends me an email with their email address. something like a subscription.
what I have so far is this:
<form action="" method="post">
<input type="email" name="email" placeholder="Enter your email address" /><br>
</form>
I found this PHP sample that I believe answers my problem, but I have no idea how to call it from my HTML.
<?php
if(isset($_POST['email'])){
$email = $_POST['email'];
$to = 'myemail#something.com';
$subject = 'new subscriber';
$body = '<html>
<body>
<p>Email:<br>'.$email.'</p>
</body>
</html>';
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset-utf-8";
$send = mail($to, $subject, $body, $headers);
if($send){
echo '<br>';
echo 'thanks';
}else{
echo 'error';
}
}
?>
There's insufficient code for me to be able to answer completely, but the one thing that comes immediately to my mind is not leaving action="" empty. Try $_SERVER['PHP_SELF'] variable, it should print the path to the file that is currently running so you'll be presented with the same page, but with data in $_POST you'll send. You can try it like this:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="email" name="email" placeholder="Enter your email address" /><br>
</form>
If you wish to send data to the same file like this, please make sure your PHP code is in the same file as the HTML structure of your form. It may make things easier if you put your PHP code first, so you can exit; from the file (not displaying the form anymore) telling the user that the message has been sent or that the error has occured.
I am using PHP to send data to an email address from a HTML form. It worked fine while the PHP file was a pure PHP file, displaying the confirmation text upon submitting the form. However, I needed the confirmation text to appear within our usual templates so I added the same PHP into the body of a page and set the form action to go to that page. When someone now submits the form, an email does get sent but it contains none of the information from the form. Can you help?
HTML:
<form method="post" action="thank-you-page.html">
Email: <input name="email" type="text"><br />
Name: <input name="name" type="text"><br />
<h3>Your message</h3>
Subject: <input name="subject" type="text"><br />
Message:<br /> <textarea name="message" rows="15" cols="40"></textarea><br />
<input type="submit" />
</form>
PHP within body of thank-you-page.html:
<?php
$to = "myemail#email.com";
$subject = 'Feedback from online form';
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$headers = "From: $email";
$sent = mail($to, $subject, $message, $headers) ;
if($sent)
{print 'Your mail was sent successfully. Thank you for your feedback.'; }
else
{print 'We encountered an error sending your mail.'; }
?>
Thank you!
Your thank you page needs to be a PHP page, not just an HTML page.
Change it to be thank-you-page.php
I've got a very simple PHP contact form, containing Email and Message.
I would like to add a functionality so that every time the contact form is sent I know the URL that it is being sent from, and I'd like to include it into the body of the message that gets sent to my email.
Here's the PHP code that runs the Contact form.
<?php
$to = "email#email.com" ;
$from = "Something Broke!" ;
$subject = "Something Broke!";
$fields = array();
$fields{"emailOptional"} = "Email:";
$fields{"message"} = "Message:";
$body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%s: %s\n",$b,$_REQUEST[$a]); }
if(mail($to, $subject, $body)){
echo 'sent';// we are sending this text to the ajax request telling it that the mail is sent..
}else{
echo 'failed';// ... or to tell it that it wasn't sent
}
?>
And here's the markup:
<form method="post" action="widgetScript.php" id="contactForm">
<input type="text" name="emailOptional" placeholder="Your Email (optional)" />
<textarea rows="5" type="text" name="message" id="message"></textarea><br />
<input type="submit" name="send" id="Submit" value="Send">
</form>
I've found that you can use [_post_url] to get the current URL if I understand correctly - but I'm unsure what to do with it. Would appreciate all the help I could get
In your php code, use $ _SERVER ['HTTP_REFERER'] to get the url the form was submitted from.
For more information on $_SERVER:
http://php.net/manual/en/reserved.variables.server.php