I'm trying to create contact form that would let me attach file. I'm very new to PHP. I have tried looking for youtube videos but was not able to find any ussefull information. I'm hoping that you could help me with my PHP.
Also if there is better alternataive or the way of doing it , please share it with me.
HTML
<form style="display:flex; flex-direction: column;" action="../mail/mail.php" method="post">
<input type="text" id="name" name="name" placeholder="Your full name.." required="required" />
<label for="email">Email</label>
<input type="email" id="email" name="email" placeholder="Your e-mail.." required="required" />
<label for="country">Country</label>
<select id="country" name="country" required="required">
<option value="Netherlands">Netherlands</option>
<option value="Saint Kitts and Nevis">Saint Kitts and Nevis</option <option value="Tajikistan">Tajikistan</option>
</select>
<label for="subject">Subject</label>
<select id="subject" name="subject" required="required">
<option value="Choose">Click here to select..</option>
<option value="Choose1">Click here to select1..</option>
<option value="Choose2">Click here to select.2.</option></select>
<div class="attachment-row">
<input id="attachment-file" type="file" class="input-field" name="attachment[]">
</div>
<label for="message">Message</label>
<textarea id="message" name="message" placeholder="Write your message here.."style="height:200px"></textarea>
<input type="submit" class="btn-send-message" name="submit" value="Send" /></form>
PHP
<?php
ob_start();
if(isset($_POST['submit'])){
$to = "info#example.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$name = $_POST['name'];
$country = $_POST['country'];
$subject = $_POST['subject'];
$message = "From: ". $from . "\n\n" . "Subject: ". $subject . "\n\n" . "Country: ". $country ."\n\n"."Name: ". $name ."\n\n". "Wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $name . "\n\n" . $_POST['message'];
mail($to,$subject,$message);
mail($from,$subject2,$message2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $name . ", we will contact you shortly.";
header("Location: ../thank-you-for-contacting.php");
}
ob_end_flush();
?>
First of all you don't need the output buffer at all. What you're doing here is simply loading a single echo line into memory and dumping it on the end.
Have a read on:
https://www.php.net/manual/en/function.ob-start.php
Your current code doesn't even look for attachments, you'd need something along the lines of:
if (count($_POST['attachment'])) {...}
Next thing you need to figure out is the structure of the email itself. For basics have a quick read on:
https://www.php.net/manual/en/function.mail
Keeping the headers and message itself separate is always a good idea, creating variables only used once is a bad one. SO you might try something like:
if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$headers[] = 'To: '.filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
}
// They're both really bad filters, for application security You should do your research and define your own. Character encoding is a big deal btw.
Once you have a email formatted properly you should look at the attachments mentioned earlier. To understand how those are sent have a read on:
https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html
TLDR; // If you want to code things, nothing is too long... but this should provide some clarity
header[] = "Content-Type: multipart/mixed; boundary=$UNIQUE_string";
$message = "--$UNIQUE_string".PHP_EOL;
$message .= "Content-type: text/plain;".PHP_EOL;
$message .= $content.PHP_EOL;
$message .= "--$UNIQUE_string".PHP_EOL;
$message .= 'Content-Disposition: form-data; name="myFile"; filename="foo.txt"'.PHP_EOL;
$message .= "Content-Type: text/plain".PHP_EOL;
$message .= file_get_contents('foo.txt').PHP_EOL;
$message .= "--$UNIQUE_string--".PHP_EOL;
Related
I've tried multiple different ways to set up a contact form and connect it to an email address using PHP. This is my first time using PHP, however I know I've written the .php form correctly.
Whenever I send the form, I get redirected to an "Error 405" page.
I've Googled for a few hours, and some websites give confusing instructions about downloading PHP (which I have tried about 5 times with no success), others say that VS Code on Mac should already be able to use PHP.
I'll leave my HTML and PHP forms below (I've tried two different PHP forms with no success).
It is implied that this should be a very easy process, simply connect the HTML to PHP via "action: mail.php" attribute in the form tag, and that should be everything. Yet I have scoured the internet and have had no luck so far... Any help would be appreciated!
(I'll leave the code below, first is HTML and second/third are the two different PHP forms I have tried).
The link to my Github is here as well: "https://github.com/cjmaret/abt-website"
<form class="contact-form__form-section" action="mail.php" method="POST" accept-charset="UTF-8">
<h2 class="contact-form__form-section__title">Get In Touch</h2>
<p class="contact-form__form-section__subtitle">Fill out the form or call ###-###-#### to request an
estimate or more information. We look forward to assisting you!</p>
<fieldset class="fieldset">
<select class="input input_select" id="dropdown" name="dropdown" required>
<option value>Subject*</option>
<option value="accounts-receivable">Accounts Receievable</option>
<option value="accounts-payable">Accounts Payable</option>
<option value="human-resources">Human Resources</option>
<option value="quotes-estimates">Quotes/Estimates</option>
<option value="w9-certificates">W9/Certificates of Insurance</option>
<option value="careers">Careers</option>
<option value="general-feedback">General Feedback</option>
<option value="request-service">Request Service</option>
</select>
<div class="input">
<input class="text-input" type="text" name="name" placeholder="Name*" required>
</div>
<div class="input">
<input class="text-input" type="text" name="company" placeholder="Company">
</div>
<div class="input">
<input class="text-input" type="email" name="email" placeholder="Email*" required>
</div>
<div class="input">
<input class="text-input" type="tel" name="phone" placeholder="Phone">
</div>
<div class="input">
<textarea class="textarea" type="text" name="message" placeholder="How Can We Help?"></textarea>
</div>
<div class="g-recaptcha" data-sitekey="#########################"></div>
<button type="submit" class="button-primary button-primary_place_contact-form">
<p class="button-primary__text button-primary__text_place_contact-form">Get Started</p>
</button>
</fieldset>
</form>
<?php
if (isset($_POST['Email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "cjmaret#gmail.com";
$email_subject = "New form submissions";
function problem($error)
{
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'])
) {
problem('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['Name']; // required
$email = $_POST['Email']; // required
$message = $_POST['Message']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if (!preg_match($email_exp, $email)) {
$error_message .= 'The Email address you entered does not appear to be valid.<br>';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if (!preg_match($string_exp, $name)) {
$error_message .= 'The Name you entered does not appear to be valid.<br>';
}
if (strlen($message) < 2) {
$error_message .= 'The Message you entered do not appear to be valid.<br>';
}
if (strlen($error_message) > 0) {
problem($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 .= "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);
?>
<!-- include your success message below -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
#!/usr/bin/php
<?php
$name = $_POST['name'];
$company = $_POST['company'];
$email = $_POST['email'];
$dropdown = $_POST['dropdown'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$formcontent="From: $name \n Phone: $phone \n Company: $company \n Email: $email \n Subject: $dropdown \n Message: $message";
$recipient = "cjmaret#gmail.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>
I recently made a website using HTML, CSS, and JS. Since I don't know PHP, I am stuck at building the contact form where it is vital on the website. I learned a bit from YouTube tutorials and have the following HTML & PHP code:
<div class="contact_form">
<form action="/action_page.php">
<input type="text" id="name" name="name" placeholder="Name*">
<input class="contact_even" type="text" id="email" name="email" placeholder="Email id*">
<input type="text" id="phone" name="phone" placeholder="Phone No.">
<input class="contact_even" type="text" id="city" name="city" placeholder="City">
<textarea id="subject" name="subject" placeholder="How Can We Help You?"></textarea>
<input type="submit" value="Submit">
</form>
</div>
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$mailFrom = $_POST['email'];
$phone = $_POST['phone'];
$city = $_POST['city'];
$message = $_POST['message'];
$mailTo = 'example#something.in';
$headers = 'From: '.$mailFrom;
$txt = $name.'('.$phone.') from '.$city.' says:\n\n'.$message;
$headers = "MIME-VERSION: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
mail($mailTo, $headers, $txt);
header("Location: index.html?mailsent");
}
?>
Why do I need the MIME and content-type headers at the bottom as that bit I added from another tutorial.
When I use the form and try sending the message, I get the "?mailsent" after the URL but I receive no email which is a professional plan by GoDaddy.
They are also hosting my website. I contacted them to know whether the server allows me to make my contact form with the plan I have and they said yes. So, I must be missing something important here.
refer to the documentation of mail function
there are 3 required parameter : email destination (to), subject and the message, and two additional options are: headers and parameters.
your code didnt respect that, because you missing to add the subjuct as parameter.
and you get ?mailsent because you use header("Location: index.html?mailsent") without any test if the email send successfully or not.
i suggest you to replace the last two lines of your php code with this
$subject = "some subject"; // you can replace it with $subject = $_POST["subject"]
$result = mail($mailTo, $subject , $txt,$headers);
if ($result){
// mail send successfully
header("Location: index.html?mailsent");
} else {
// error
}
EDIT:
you can get the error message with error_get_last() function.
thanks to https://stackoverflow.com/a/20203870/195835
$subject = "some subject"; // you can replace it with $subject = $_POST["subject"]
$result = mail($mailTo, $subject , $txt,$headers);
if ($result){
// mail send successfully
header("Location: index.html?mailsent");
} else {
print_r(error_get_last());
}
You are missing the form action, So PHP doesn't know what to do with your variable data.Try adding method="post" inside <form> tag. Like this
<div class="contact_form">
<form action="/action_page.php" method="post">
<input type="text" id="name" name="name" placeholder="Name*">
<input class="contact_even" type="text" id="email" name="email" placeholder="Email id*">
<input type="text" id="phone" name="phone" placeholder="Phone No.">
<input class="contact_even" type="text" id="city" name="city" placeholder="City">
<textarea id="subject" name="subject" placeholder="How Can We Help You?"></textarea>
<input type="submit" value="Submit">
</form>
</div>
And also. If you use your computer as localhost(using xampp , wamp, or something without a hosting service) You have to make some changes to the config files.
Also try this modified php code
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$mailFrom = $_POST['email'];
$phone = $_POST['phone'];
$city = $_POST['city'];
$message = $_POST['message'];
$title = "replace this";
$mailTo = 'support#udichi.in';
$txt = $name.'('.$phone.') from '.$city.' says:\n\n'.$message;
$headers = 'From: '.$mailFrom . PHP_EOL .'Reply-To:' .$mailFrom . PHP_EOL . 'X-Mailer: PHP/' . phpversion();
mail($mailTo,$title,$txt,$headers);
header("Location: index.html?mailsent");
}
?>
Getting a white screen when trying to submit my contact form which has these entries:
- Name
- Email
- Subject
- Message
Im attempting to recieve emails through my website. Ive checked all variable names and such and it seems that everything is correct. Im new to PHP so im a little cloudy on what to try next.
Thanks
<form method"POST" action="action/form-submit.php"> <!--NO FOR ATTRIBUTE, NOT ADDING FUNCTIONALITY-->
<h2>Contact Me:</h2>
<label>Your Name:</label>
<input name="name" type="text" placeholder="Your Name..." required/>
<label>Email:</label>
<input name="email" type="email" placeholder="Email..." required/>
<label>Query Type:</label>
<select id="qry" name="query" required>
<option value="" disabled selected>Please Select:</option>
<option value="jobs">Jobs</option>
<option value="website">Website Issues</option>
<option value="info">Information</option>
</select>
<label>Your Message:</label>
<textarea name="info" placeholder="Your Message..." required></textarea>
<input type="submit" value="Submit">
</form>
Then the PHP code:
<?php
$vname = $_POST['name'];
$vemail = $_POST['email'];
$vquery = $_POST['query'];
$vmessage = $_POST['info'];
$email_from = "test#gmail.com";
$email_subject = "New Website Submission";
$email_body = "Visitor Name: $vname.\n".
"Visitor Email: $vemail.\n".
"Visitor Subject: $vquery.\n".
"Visitor Message: $vmessage.\n";
$to = "bradleyarcher98#gmail.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $vemail \r\n";
mail($to,$email_subject,$email_body,$headers);
header("location: contact.html");
?>
In order to see what is wrong, you need to turn on the PHP error handling.
When this is on, you won't just see a white page anymore, an error message with filename, line number and a message describing the error is sent to the browser.
<?php
session_start();
$vname = $_POST['name'];
$vemail = $_POST['email'];
$vquery = $_POST['query'];
$vmessage = $_POST['info'];
$subject = " YOUR MESSAGE Title ";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "From: "YOUR SITE NAME" . "\r\n";
$message = "
<!-- you can add CSS Aswell -->
<div>
<p>Visitor Name: ".$_POST['name']."<p>
<p>Visitor Email: ".$vemail."<p>
<p>Subject: ".$vquery."<p>
<p>Message :".$vmessage."<p>
</div>
";
mail($to,$subject,$message,$headers);
?>
sorry but i didn't learn PHP yet so can any one help me . i want the out put of the form written in txt file and a messege like " thank you " be the answer if the visitor complete the form and press submit
<form role="form" method="post" id="contact-form">
<input type="text" placeholder="Name" name="Name" id="Name" required>
<input type="email" placeholder="Email" name="Email" id="Email" required>
<input type="text" placeholder="Phone" name="Phone" id="Phone">
<select name="test">
<option selected disabled="disabled"> Where do you live?</option>
<option value="test12"> Al Amirat</option>
<option value="test13"> Other</option>
</select>
<br>
<button type="submit" id="submit"> Submit</button>
<div id="success"></div>
</form>
the last PHP code was to make it to email
<?php
$name = $_POST['Name'];
$email = $_POST['Email'];
$phone = $_POST['Phone'];
$subject = $_POST['Subject'];
$message = $_POST['Message'];
$to = 'test#email.com';
$subject = 'the subject';
$message = 'FROM: '.$name.' Email: '.$email.' Phone: '.$phone.' Subject: '.$subject.' Message: '.$message;
$headers = 'From: youremail#domain.com' . "\r\n";
if (filter_var($email, FILTER_VALIDATE_EMAIL)) { // this line checks that we have a valid email address
mail($to, $subject, $message, $headers); //This method sends the mail.
echo "Your email was sent!"; // success message
}else{
echo "Invalid Email!";
}
?>
but after the client need to add the choose from place and making it in txt not email i didn't know how it solved
Here is one example,
$file = 'people.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= "John Smith\n";
// Write the contents back to the file
file_put_contents($file, $current);
Check more the example Here,
There are few things you might get stuck with.
Folder Permission:
PHP must have write permission to the folder you are going to save the file. Which 777 permission. So be very careful about security.
Path Separator
Path separators are different for linux and windows. So choose accordingly.
I am having trouble with the contact form that I've created for my website. I am not a PHP expert but I thought a proper contact form would be more professional than a simple href mailto link.
I managed to get the email, it tells me the sender but there is no subject and is all just blank without text. Also I keep receiving 2 emails everyday from no sender.
This is what I done in PHP in the page named contact.php.
I hope you can help:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$form1_services = $_POST['form1_services'];
$msg = $_POST['msg'];
$formcontent="From: $name \n Message: $message";
$recipient = "dandrea.alessandro81#gmail.com";
$subject = "Customer Inquiry";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You! Keep in touch soon!" . " -" . "<a href='index.html' style='text-decoration:none;font-family: 'gooddogregular';color:#009999;'> Return Home</a>";
?>
And this is the actual form in HTML:
<form action="contact.php" method="post">
<fieldset>
<legend></legend>
<div class="controlgroup">
<label for="form1_name">Name *</label>
<input type="text" placeholder="Enter your full name*" name="name" value>
</div>
<div class="controlgroup">
<label for="form1_email">Email *</label>
<input type="text" placeholder="Enter a valid email address*" name="email" value>
</div>
<div class="controlgroup">
<label for="form1_services">Services Required</label>
<select id="form1_services" name="services">
<option value="Website Design"> Website Design (from scratch) </option>
<option value="Resposive Design"> Responsive Design </option>
<option value="Customize a Site"> Customize a Site </option>
<option value="Quotation"> Quotation </option>
</select>
</div>
<div class="controlgroup">
<label>Project Info*</label>
<textarea placeholder="Ciao Alessandro, I am contacting you because...*" id="msg" name="msg" required aria-required="true"></textarea>
</div>
<input type="submit" name="submit_btn" id="send" value="Hit me up!" class="wow rubberBand animated" data-wow-delay="2s">
</fieldset>
</form>
Thanks a lot in advance.
Alessandro
Taking a look at your PHP, you're collecting a number of fields, and then not actually using them. Try this:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$form1_services = $_POST['services'];
$msg = $_POST['msg'];
$message = 'From: ' . $name . ' <' . $email . '>' . "\n";
$message .= 'Service: ' . $form1_services . "\n";
$message .= 'Message: ' . "\n";
$message .= $msg;
$recipient = "dandrea.alessandro81#gmail.com";
$subject = "Customer Inquiry";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $message, $mailheader) or die("Error!");
echo "Thank You! Keep in touch soon!" . " -" . "<a href='index.html' style='text-decoration:none;font-family: 'gooddogregular';color:#009999;'> Return Home</a>";
Keep in mind that at this point, you're not actually validating any of this information, so you can't be sure that the email address or the name is actually valid at all, but this should at least show you what is getting posted.
Try adding some headers:
$mailheader = "From: $email \r\n";
$mailheader .= "MIME-Version: 1.0\r\n";
$mailheader .= "Content-Type: text/html; charset=ISO-8859-1\r\n";