how to add an attachment to a php contact form - php

I have made a contact form that allows users to send me messages, but what I couldn't add is the ability for them to send me a file with it. Here is my code:
<form action="mail.php" method="post" enctype='multipart/form-data'>
<input type="text" class="feedback-input" id="firstname" name="firstname" placeholder="First Name" size="30" required="">
<br/>
<input type="text" class="feedback-input" id="lastname" name="lastname" placeholder="Last Name" size="30" required="">
<br/>
<input type="email" class="feedback-input" id="title" name="title" placeholder="E-mail" size="30" required="">
<br/>
<textarea name="message" class="feedback-input" placeholder="What can I help you with?" style="height: 150px;" required=""></textarea><br/>
<br/>
<input type="file" name="file" id="file" placeholder=" " tabindex="1" required/><br/>
<input type="submit" name="submit" id="Submit" value="Send">
and this is the mail.phph file:
<?php
$firstname = $_POST['firstname'];
$title = $_POST['title'];
$lastname = $_POST['lastname'];
$message = $_POST['message'];
$ip = $_SERVER['REMOTE_ADDR'];
$formcontent=" From: $firstname $lastname \n Email Address: $title \n IP: $ip \n\n Description : $message";
$recipient = "myemail#yahoo.com";
$subject = "New Message!!";
$mailheader = "From: $title \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header("Location: www.mythankyoupage.com");
die();
?>

Related

Php Contact Form issue

I'm having problems with this code. After submitting the filled form the display message (Email sent!) is not displaying next to the submit button. I need to display the Email sent message next to the submit button on the contact form. After submitting the form, I also need to clear the form fields even after a refresh or a back action. Kindly help me out with this code
Form Action Images:
Before Submitting the Form
After Submitting the form
<?php
$action=$_REQUEST['action'];
if ($action=="") /* display the contact form */
{
?>
<form name="contactform" method="post" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
<label for="your_name">Your Name <font color="red">*</font></label>
<input type="text" id="reset" name="your_name" placeholder="Enter Your Name" maxlength="20" size="40" >
<label for="email">Email Address <font color="red">*</font></label>
<input type="email" id="reset" name="email" placeholder=" Enter Your E-mail Address" maxlength="20" size="40" pattern="[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,3}$" required>
<label for="mobile_number">Mobile Number</label>
<input type="tel" id="reset" name="mobile_number" pattern="[0-9]{1}[0-9]{9}" placeholder="Enter Your Phone Number by Adding Country Code (eg: +91.,)" maxlength="30" size="40" >
<label for="message">Message <font color="red">*</font></label>
<textarea name="message" id="reset" placeholder="Your Message Goes Here" maxlength="1000" cols="62" rows="10" required></textarea>
<input type="submit" value="Submit">
</form>
<?php
}
else /* send the submitted data */
{
$your_name = $_REQUEST['your_name'];
$email = $_REQUEST['email'];
$mobile_number = $_REQUEST['mobile_number'];
$message = $_REQUEST['message'];
$formcontent="From: $your_name \n Email: $email \n Phone Number: $mobile_number \n Message: $message";
$recipient = "name#email.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
if (($your_name=="")||($email=="")||($message==""))
{
echo "All fields are required, please fill the form again.";
}
else{
#mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Email sent!";
}
}
?>
Your code should be like this. Notice isset($_POST['submit'], $msg , value="".
<?php
if(isset($_POST['submit']))
{
$your_name = $_REQUEST['your_name'];
$email = $_REQUEST['email'];
$mobile_number = $_REQUEST['mobile_number'];
$message = $_REQUEST['message'];
$formcontent="From: $your_name \n Email: $email \n Phone Number: $mobile_number \n Message: $message";
$recipient = "name#email.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
if (($your_name=="")||($email=="")||($message==""))
{
$msg = "All fields are required";
}
else{
#mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
$msg = "Email Sent";
}
}
?>
<form name="contactform" method="post" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
<label for="your_name">Your Name <font color="red">*</font></label>
<input type="text" id="reset" name="your_name" placeholder="Enter Your Name" maxlength="20" size="40" value="">
<label for="email">Email Address <font color="red">*</font></label>
<input type="email" id="reset" name="email" placeholder=" Enter Your E-mail Address" maxlength="20" size="40" pattern="[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,3}$" required value="">
<label for="mobile_number">Mobile Number</label>
<input type="tel" id="reset" name="mobile_number" pattern="[0-9]{1}[0-9]{9}" placeholder="Enter Your Phone Number by Adding Country Code (eg: +91.,)" maxlength="30" size="40" value="">
<label for="message">Message <font color="red">*</font></label>
<textarea name="message" id="reset" placeholder="Your Message Goes Here" maxlength="1000" cols="62" rows="10" required></textarea>
<input type="submit" name="submit" value="Submit">
<?php echo $msg; ?>
</form>
You can redirect the page after sent email or also can set a variable like $msg='email sent'; and call this variable before your submit button.
$action=$_REQUEST['action'];
if ($action!="") /* display the contact form */
{
$your_name = $_REQUEST['your_name'];
$email = $_REQUEST['email'];
$mobile_number = $_REQUEST['mobile_number'];
$message = $_REQUEST['message'];
$formcontent="From: $your_name \n Email: $email \n Phone Number: $mobile_number \n Message: $message";
$recipient = "name#email.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
if (($your_name=="")||($email=="")||($message==""))
{
header('location: yoururl?msg=error');
//$msg = "All fields are required, please fill the form again.";
}
else{
#mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header('location: yoururl?msg=succss');
//$msg = 'Email Sent!';
}
}
?>
<form name="contactform" method="post" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
<label for="your_name">Your Name <font color="red">*</font></label>
<input type="text" id="reset" name="your_name" placeholder="Enter Your Name" maxlength="20" size="40" >
<label for="email">Email Address <font color="red">*</font></label>
<input type="email" id="reset" name="email" placeholder=" Enter Your E-mail Address" maxlength="20" size="40" pattern="[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,3}$" required>
<label for="mobile_number">Mobile Number</label>
<input type="tel" id="reset" name="mobile_number" pattern="[0-9]{1}[0-9]{9}" placeholder="Enter Your Phone Number by Adding Country Code (eg: +91.,)" maxlength="30" size="40" >
<label for="message">Message <font color="red">*</font></label>
<textarea name="message" id="reset" placeholder="Your Message Goes Here" maxlength="1000" cols="62" rows="10" required></textarea>
<?php if(isset($_REQUEST['msg']) and $_REQUEST['msg']=='success'){echo "Email sent!";}if(isset($_REQUEST['msg']) and $_REQUEST['msg']=='error'){echo "All fields are required, please fill the form again.";} ?>
<?php echo $msg; ?>
<input type="submit" value="Submit">
</form>

PHP form redirecting to PHP file itself

I'm trying to get a contact form working, and I'm being redirected to the PHP file itself, and I'm not sure why. I'm fairly new to PHP and I'm not entirely sure what I'm doing wrong. I'd love to be pointed in the right direction. The code is below.
Thanks
HTML:
<p id='feedback'><?php echo $feedback; ?></p>
<form id="contact_us" enctype="text/plain" method="post" action="form.php">
<input class="form_field" type="text" name="Name" placeholder="Full Name">
<br>
<input class="form_field" type="text" name="Company" placeholder="Company Name">
<br>
<input class="form_field" type="email" name="Email_Address" placeholder="Email Address">
<br>
<textarea class="form_field" rows="10" cols="20" name="Description" wrap="hard" placeholder="Project Description"></textarea>
<br>
<p id="required"><i>Please fill in all the fields*</i></p>
<input class="submit" type="submit" value="SUBMIT">
</form>
PHP:
<?php
$to = 'zack#zfisch.com';
$subject ='Dropset Work Request';
$name = $_POST['Name'];
$company = $_POST['Company'];
$email = $_POST['Email_Address'];
$message = $_POST['Description'];
$message = <<<EMAIL
From: $name
$message
Email: $email
EMAIL;
$header = $subject;
if($_POST) {
mail($to, $subject, $message, $header);
$feedback = 'Email sent!';
}
?>
This line tells the form what PHP file to send the data to:
<form id="contact_us" enctype="text/plain" method="post" action="form.php">
In particular, it is this part that sends the data to a particular form:
action="form.php"
In fact, this is fine. You can process a form in the same PHP file that contains the form. Just put the PHP form processing at the top:
<?php
if (isset($_POST['Email_Address'] && $_POST['Email_Address'] != ''){
$to = 'zack#zfisch.com';
$subject ='Dropset Work Request';
$name = $_POST['Name'];
$company = $_POST['Company'];
$email = $_POST['Email_Address'];
$message = $_POST['Description'];
$message = <<<EMAIL
From: $name
$message
Email: $email
EMAIL;
$header = $subject;
if($_POST) {
mail($to, $subject, $message, $header);
$feedback = 'Email sent!';
}
}else{
>?
<p id='feedback'><?php echo $feedback; ?></p>
<form id="contact_us" enctype="text/plain" method="post" action="form.php">
<input class="form_field" type="text" name="Name" placeholder="Full Name">
<br>
<input class="form_field" type="text" name="Company" placeholder="Company Name">
<br>
<input class="form_field" type="email" name="Email_Address" placeholder="Email Address">
<br>
<textarea class="form_field" rows="10" cols="20" name="Description" wrap="hard" placeholder="Project Description"></textarea>
<br>
<p id="required"><i>Please fill in all the fields*</i></p>
<input class="submit" type="submit" value="SUBMIT">
</form>
<?php
}
?>
Just remove enctype="text/plain" otherwise your code is correct in HTML and try to echo feedback variable other way is to write all your code above the HTML like below:
<?php
if (isset($_POST['Email_Address'] && !empty['Email_Address']){
$to = 'zack#zfisch.com';
$subject ='Dropset Work Request';
$name = $_POST['Name'];
$company = $_POST['Company'];
$email = $_POST['Email_Address'];
$message = $_POST['Description'];
$message = <<<EMAIL
From: $name
$message
Email: $email
$header = $subject;
if($_POST) {
mail($to, $subject, $message, $header);
$feedback = 'Email sent!';
}
}else{
>?
<p id='feedback'><?php echo $feedback; ?></p>
<form id="contact_us" enctype="text/plain" method="post" action="form.php">
<input class="form_field" type="text" name="Name" placeholder="Full Name">
<br>
<input class="form_field" type="text" name="Company" placeholder="Company Name">
<br>
<input class="form_field" type="email" name="Email_Address" placeholder="Email Address">
<br>
<textarea class="form_field" rows="10" cols="20" name="Description" wrap="hard" placeholder="Project Description"></textarea>
<br>
<p id="required"><i>Please fill in all the fields*</i></p>
<input class="submit" type="submit" value="SUBMIT">
</form>
<?php
}
?>

PHP form validation

So I have created a form inside html the code -
<form method="post" action="hi.php">
<fieldset>
<label class="labelone" type="text" />
<input name="name" placeholder="Your Name"/>
<label class="email" for="email" />
<input name="email" placeholder="Email"/>
<label class="phonenumber" for="phonenumber" />
<input name="phonenumber" placeholder="Phone Number"/>
<label class="comments" for="comments" />
<textarea name="comments" placeholder="Comments"></textarea>
</fieldset>
<fieldset>
<input type="submit" value="Submit" class="btn" >
<input class="btn" type="reset" value="Reset">
</fieldset>
</form>
and this is the php -
<?php
$emailSubject = 'Test';
$webMaster = 'Test';
$name = $_GET['firstname'];
$email = $_GET['lastname'];
$message = $_GET['comment'];
$body = <<<EOD
<br><hr><br>
First Name: $firstname <br>
Last Name: $lastname <br>
Message: $comment <br>
EOD;
$headers = "From: $lastname\r\n";
$headers = "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);
$theResults = <<<EOD
<?php
?>
<?php
$emailSubject = 'Kent Pest Control Services';
$webMaster = 'xxxxxxxxxEDITED#gmail.com';
$name = $_GET['firstname'];
$email = $_GET['email'];
$phonenumber = $_GET['phonenumber']
$message = $_GET['comments'];
$body = <<<EOD
<br><hr><br>
First Name: $firstname <br>
Email: $email <br>
Phone Number: $phonenumber <br>
Message: $comments <br>
EOD;
When I upload it to my hosting and fill in the php form, and click submit, it goes to a blank screen, and I dont get an email, I have used this before like a year ago, I dont know if the code has changed since then, but I cant get it to work, I hope someone can help. :) I changed the email so people cant email me ;)
you are missing a <form> tag in your HTML.
try this:
<form action="yourPHPfile.php" method="post">
<fieldset>
<label class="labelone" type="text" />
<input name="name" placeholder="Your Name"/>
<label class="email" for="email" />
<input name="email" placeholder="Email"/>
<label class="phonenumber" for="phonenumber" />
<input name="phonenumber" placeholder="Phone Number"/>
<label class="comments" for="comments" />
<textarea name="comments" placeholder="Comments"></textarea>
</fieldset>
<fieldset>
<input type="submit" value="Submit" class="btn" >
<input class="btn" type="reset" value="Reset">
</fieldset>
</form>
I would use POST, and not GET as you are passing potentially a lot of data through and it would create a very ugly url.
your php file then..
<?php
$emailSubject = 'Test';
$webMaster = 'Test';
$name = $_POST['firstname'];
$email = $_POST['lastname'];
$message = $_POST['comments']; // you have the wrong key in your original file (comment)
$body = <<<EOD
<br><hr><br>
First Name: $firstname <br>
Last Name: $lastname <br>
Message: $comment <br>
EOD;
$headers = "From: $lastname\r\n";
$headers = "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);
$theResults = <<<EOD
?>
other points:
you should be validating the email address server side if youre going to do anything else with it, like send back a confirmation etc
I think this code can resolve you query
<form action="actionname.php" method="post">
<fieldset>
<label class="labelone" type="text" />
<input name="firstname" placeholder="Your firstName"/>
<label class="labeltwo" type="text" />
<input name="lastname" placeholder="Your LastName"/>
<label class="email" for="email" />
<input name="email" placeholder="Email"/>
<label class="phonenumber" for="phonenumber" />
<input name="phonenumber" placeholder="Phone Number"/>
<label class="comments" for="comments" />
<textarea name="comments" placeholder="Comments"></textarea>
</fieldset>
<fieldset>
<input type="submit" value="Submit" class="btn" >
<input class="btn" type="reset" value="Reset">
</fieldset>
</form>
Now this php code
<?php
$emailSubject = 'Test';
$webMaster = $_POST['email']; /* Pass your form email address here */
$firstname = $_POST['firstname']; /* Firstname from form */
$lastname = $_POST['lastname']; /* Lastname from form */
$message = $_POST['comments']; /* Comment from form */
$body = <<<EOD
<br><hr><br>
First Name: $firstname <br>
Last Name: $lastname <br>
Message: $message <br>
EOD;
$headers = "From: $lastname\r\n";
$headers = "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);
$theResults = <<<EOD
?>

Send a message and upload a file in PHP [duplicate]

This question already has answers here:
How to upload & Save Files with Desired name
(9 answers)
Closed 9 years ago.
I have form that allows users to send me a message on my email, but I want it that when a user fills the form and chooses a file the file gets uploaded to the server and the message gets sent to me. I did do the message part but I couldn't get the file uploaded. here is my code:
Index page:
<form action="mail.php" method="post" enctype='multipart/form-data'>
<input type="text" class="feedback-input" id="firstname" name="firstname" placeholder="First Name" size="30" required="">
<br/>
<input type="text" class="feedback-input" id="lastname" name="lastname" placeholder="Last Name" size="30" required="">
<br/>
<input type="email" class="feedback-input" id="title" name="title" placeholder="E-mail" size="30" required="">
<br/>
<textarea name="message" class="feedback-input" placeholder="What can I help you with?" style="height: 150px;" required=""></textarea><br/>
<br/>
<input type="file" name="file" id="file" placeholder=" " tabindex="1" required/><br/>
<input type="submit" name="submit" id="Submit" value="Send">
and this is the mail.php file:
<?php
$firstname = $_POST['firstname'];
$title = $_POST['title'];
$lastname = $_POST['lastname'];
$message = $_POST['message'];
$ip = $_SERVER['REMOTE_ADDR'];
$formcontent=" From: $firstname $lastname \n Email Address: $title \n IP: $ip \n\n Description : $message";
$recipient = "myemail#yahoo.com";
$subject = "New Message!!";
$mailheader = "From: $title \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header("Location: www.mythankyoupage.com");
die();
?>
I think your file upload should be based on this
http://www.w3schools.com/php/php_file_upload.asp
Please comment if you get stuck somewhere and I'll gladly help! :)

Basic contact form php not working, getting empty messages from my form [duplicate]

I’m new to PHP so bear with me. My form is sending empty emails when I press submit. Any help would be great.
This is my form:
<form class="contact_form" action="kontakt.php" method="post">
<p><input type="text" required="required" id="name" name="name" class="text_input" value="" size="22" />
<label for="name">Namn *</label></p>
<p><input type="text" required="required" id="company" name="company" class="text_input" value="" size="22" />
<label for="company">Företag *</label></p>
<p><input type="email" required="required" id="email" name="email" class="text_input" value="" size="22" />
<label for="email">Epost *</label></p>
<p><textarea required="required" name="content" class="textarea" cols="30" rows="5"></textarea></p>
<p><button type="submit" class="button white"><span>Skicka</span></button></p>
<input type="hidden" value="info#web.se" name="contact_to"/>
</form>
And my PHP code so far is:
<?php
$name = $_POST['name'];
$company = $_POST['company'];
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
$content = $_POST['content'];
$mail_to = 'info#web.se';
$subject = 'Lilla form'.$name;
$body_message = 'From: '.$name."\n";
$body_message .= 'E-mail: '.$email."\n";
$body_message .= 'Message: '.$content;
$headers = 'From: '.$email."\r\n";
$headers .= 'Reply-To: '.$email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
?>
Please help me, I’m really stuck.
Thank you all!
Try this
<form class="contact_form" action="kontakt.php" method="post">
<p><input type="text" required="required" id="name" name="name" class="text_input" size="22" />
<label for="name">Namn *</label></p>
<p><input type="text" required="required" id="company" name="company" class="text_input" size="22" />
<label for="company">Företag *</label></p>
<p><input type="email" required="required" id="email" name="email" class="text_input" size="22" />
<label for="email">Epost *</label></p>
<p><textarea required="required" name="content" class="textarea" cols="30" rows="5"></textarea></p>
<p><button type="submit" class="button white"><span>Skicka</span></button></p>
<input type="hidden" value="info#web.se" name="contact_to"/>
</form>
Edit:(Debug)
$name = $_POST['name'];
$company = $_POST['company'];
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
$content = $_POST['content'];
echo $name.$comapny.$email.$content;exit; // check whether the values are posted successfully or not

Categories