i am using PHP form with File attachment. Due to low knowledge in PHP i use this script from a online tutorial.
My problem is:
I want to get all user submitted form info by mail but don't know how to add code on this script(check last code section).
I want user can only send only .txt, .doc, .docx file format on file attachment.
HTML Code:
<form name="sendmail" action="form.php" method="post" enctype="multipart/form-data">
<ul>
<li>
<span class="left">
<label for="name">First Name:</label>
<input type="text" size="40" id="f_name" />
</span>
<span class="left" style="position:relative;left:30px;">
<label for="name">Middle Name:</label>
<input type="text" size="40" id="m_name" />
</span>
<span class="right">
<label for="name">Last Name:</label>
<input type="text" size="40" id="l_name" />
</span>
</li>
<li>
<span class="left">
<label for="email">Email Address:</label>
<input type="email" size="40" id="email" style="width:250px;" />
</span>
<span class="right">
<label for="email" style="width:200px;">Confirm Email Address:</label>
<input type="email" size="40" id="c_email" style="width:250px;" />
</span>
</li>
<li>
<span class="left">
<label for="name">Primary Phone: </label>
<input type="text" size="40" id="p_phone" style="width:250px;" />
</span>
<span class="right">
<label for="name" style="width:200px;">Secondary Phone:</label>
<input type="text" size="40" id="s_phone" style="width:250px;" />
</span>
</li>
<li>
<span class="left">
<label for="name">Mailing Address:</label>
<input type="text" size="40" id="m_address" style="width:250px;" />
</span>
<span class="right">
<label for="name" style="width:200px;">Mailing City:</label>
<input type="text" size="40" id="m_city" style="width:250px;" />
</span>
</li>
<li>
<span class="left">
<label for="name">State/Province:</label>
<input type="text" size="30" id="state_province" />
</span>
<span class="left" style="position:relative;left:30px;">
<label for="name">Zip/Postal Code:</label>
<input type="text" size="30" id="zip_postal" />
</span>
<span class="right">
<label for="name">Country:</label>
<input type="text" size="30" id="country" />
</span>
</li>
<li>
<span class="left">
<label for="name">Job Information:</label>
<input type="text" size="40" id="job_info" style="width:250px;" />
</span>
<span class="right">
<label for="name" style="width:200px;">Desired Job Title: </label>
<input type="text" size="40" id="job_title" style="width:250px;" />
</span>
</li>
<li>
<span class="left">
<label for="name" style="width:200px;">Desired Geographic Region:</label>
<input type="text" size="40" id="job_info" style="width:200px;" />
</span>
<span class="right">
<label for="name" style="width:200px;">Years of Related Experience:</label>
<input type="text" size="40" id="job_title" style="width:250px;" />
</span>
</li>
<li>
<span class="left">
<label>Resume:</label>
<input type="file" name="resume" />
</span>
<span class="right">
<label style="width:180px;">Cover Letter:</label>
<input type="file" name="cover_letter" />
</span>
</li>
</ul>
<p>
<button type="submit" class="action" name="submit">Submit</button>
</p>
form.php code-
<?php
$from = "info#arif-khan.net";
$to = "arifkpi#gmail.com";
$subject ="JobSeeker Registration Request";
$message = $_POST['body'];
// Temporary paths of selected files
$file1 = $_FILES['resume']['tmp_name'];
$file2 = $_FILES['cover_letter']['tmp_name'];
// File names of selected files
$filename1 = $_FILES['resume']['name'];
$filename2 = $_FILES['cover_letter']['name'];
// array of filenames to be as attachments
$files = array($file1, $file2);
$filenames = array($filename1, $filename2);
// include the from email in the headers
$headers = "From: $from";
// boundary
$time = md5(time());
$boundary = "==Multipart_Boundary_x{$time}x";
// headers used for send attachment with email
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$boundary}\"";
// multipart boundary
$message = "--{$boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$boundary}\n";
// attach the attachments to the message
for($x=0; $x<count($files); $x++){
$file = fopen($files[$x],"r");
$content = fread($file,filesize($files[$x]));
fclose($file);
$content = chunk_split(base64_encode($content));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$filenames[$x]\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $content . "\n\n";
$message .= "--{$boundary}\n";
}
// sending mail
$sendmail = mail($to, $subject, $message, $headers);
// verify if mail is sent or not
if ($sendmail) {
header("location:index.html");
}
?>
Field code that i want to add on email body-
First Name: $_POST[f_name]
Middle Name: $_POST[m_name]
Last Name: $_POST[l_name]
Email Address: $_POST[email]
Primary Phone: $_POST[p_phone]
Secondary Phone: $_POST[s_phone]
Mailing Address: $_POST[m_address]
Mailing City: $_POST[m_city]
State/Province: $_POST[state_province]
Zip/Postal Code: $_POST[zip_postal]
Country: $_POST[country]
Job Information: $_POST[job_info]
Desired Job Title: $_POST[job_title]
Desired Geographic Region: $_POST[desired_region]
Years of Related Experience: $_POST[year_of_experience]
To add the captured data to the sendmail script:
name your variable correctly (that is no space in between), for example,
First Name: $_POST[f_name]
Middle Name: $_POST[m_name]
Last Name: $_POST[l_name]
Should be (remember the '$' sign to tell php it is a variable:
$First_Name: $_POST['f_name'];
$Middle_Name: $_POST['m_name'];
$Last_Name: $_POST['l_name'];
Do the same for all, and place this code before this script:
// Temporary paths of selected files
$file1 = $_FILES['resume']['tmp_name'];
$file2 = $_FILES['cover_letter']['tmp_name'];
In your code where you have:
// multipart boundary
$message = "--{$boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
$message .= "--{$boundary}\n";
Add this:
$message .= "First name: ".clean_string($First_Name)."\r\n";
$message .= "Last name: ".clean_string($Last_Name)."\r\n";
Do this for all the variable you want to add from (1) above, and your script should now work fine.
Your inputs are missing the name attribute. For PHP to capture form values you need to add the name attribute to the inputs and then capture the input values by referring to these names. For example:
You are capturing the First Name from the from like so:
First Name: $_POST[f_name]
So get the name your input should have a name attribute equal to f_name like so:
<input type="text"name="f_name"size="40" id="job_title" style="width:250px;" />
Related
I have this PHP code to send emails.
<?php
if (isset($_POST['ajax'])) {
$to = $_POST['to'];
$subject = $_POST['sub'];
$msg = $_POST['msg'];
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "From: ".$_POST['name']."<".$_POST['from'].">";
$send = mail($to,$subject,$msg,$headers);
if ($send) {
echo "<p id='success'>✔️ $to</p>";
}else{
echo "<p id='error'>❌ $to</p>";
}
exit();
}
?>
I want to integrate it with the following form.
<form>
<input id="input-1" type="text" placeholder="John Doe" required autofocus />
<label for="input-1">
<span class="label-text">Full Name</span>
<span class="nav-dot"></span>
<div class="signup-button-trigger">Sign Up</div>
</label>
<input id="input-2" type="text" placeholder="john" required />
<label for="input-2">
<span class="label-text">Username</span>
<span class="nav-dot"></span>
</label>
<input id="input-3" type="email" placeholder="email#address.com" required />
<label for="input-3">
<span class="label-text">Email</span>
<span class="nav-dot"></span>
</label>
<input id="input-4" type="text" placeholder="●●●●●●" required />
<label for="input-4">
<span class="label-text">Password</span>
<span class="nav-dot"></span>
</label>
<input id="input-5" type="text" placeholder="●●●●●●" required />
<label for="input-5">
<span class="label-text">Confirm Password</span>
<span class="nav-dot"></span>
</label>
<button type="submit">Create Your Account</button>
<p class="tip">Press Tab</p>
<div class="signup-button">Sign Up</div>
</form>
Is there a way to make it work. I want to use the PHP GET method. Is this possible? How would I get around doing that?
I want to send a PHP GET request, which would then send the email. Do I have to change anything in the form? In the PHP?
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
HTML Codes
<h3>online consultation</h3>
<h5>We have provided contact information down below. You can contact us either by mobile, via email or via contact form. We will at once contact you to discuss your problems! If you have special requirments a consultation can be decided.</h5>
</div>
<div class="col-md-7 col-lg-offset-3 contact-grid-right">
<form method="post" action="mail.php">
<input type="text" placeholder="Your Name" name="name" id="name" required="required">
<input type="text" placeholder="Email" name="Email" id="Email" required="required">
<input type="text" placeholder="Contact No" name="contact" id="contact" required="required">
<input type="text" placeholder="Subject" name="subject" id="subject" required="required">
<textarea rows="2" cols="70" type="text" placeholder="MESSAGE" name="message" id="message" required="required">
</textarea>
<button type="submit" class="slide-btn"> Send your message <i class="fa fa-paper-plane" aria-hidden="true"></i> <br></button>
</form>
</div>
PHP Script
<?php
$n=$_POST['name'];
$mail=$_POST['email'];
$ph=$_POST['contact'];
$ln=$_POST['subject'];
$msg=$_POST['message'];
$to="email#yahoo.com";
$sub="message";
$body="
<html>
<body>
<p><b></b></p><h4></h4>
<table border='0'>
<tr>
<b>Name : </b> $n <br><br>
<b>Email : </b> $mail <br><br>
<b>Contact :</b> $ph <br><br>
<b>Subject :</b> $ln <br><br>
<hr>
<b>Contact Form Message:</b> <br><br>$msg
</tr>
</table>
</body>
</html>";
$headers = "MIME-Version: 1.0". "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8". "\r\n";
$headers .= 'From:'.$mail . "\r\n";
//$headers .= 'Cc: email#yahoo.com.au'; "\r\n";
//$headers .= 'Bcc: '; "\r\n";
mail($to,$sub,$body,$headers);
//echo "<script>alert('Message Send Sucessfully')</script>";
echo "<script>window.location.href='index.html'</script>";
?>
here the solution change the $mail=$_POST['email']; in your php script to $mail=$_POST['Email'];
For keeping your code standard replace the email html input for:
<input type="text" placeholder="Email" name="email" id="email" required="required">
HTML:
<form class="contact_form" action="php/mail.php" method="post" name="contact_form">
<ul>
<li>
<label for="name">Name*</label>
<input type="text" placeholder="Your Name" required />
</li>
<li>
<label for="name">Email*</label>
<input type="email" name="email" placeholder="Your email address" required />
<span class="form_hint">Proper format "name#something.com"</span>
</li>
<li>
<label for="name">Website</label>
<input type="url" name="website" placeholder="Your website" required pattern="(http|https)://.+"/>
<span class="form_hint">Proper format "http://someaddress.com"</span>
</li>
<li>
<label for="message">Message:</label>
<textarea rows="6" cols="40" name="message" ></textarea>
</li>
<li>
<button class="submit" type="submit">Send</button>
</li>
</ul>
</form>
HTML
/* Subject and Email Variables */
$emailSubject = 'Contact Form';
$webMaster = 'email#address.com';
/* Gathering Data Variables */
$nameField = $_POST['Name'];
$emailField = $_POST['Email'];
$website = $_POST['Website'];
$messageField = $_POST['Message'];
$body = <<<EOD
<br><hr><br>
Name: $nameField <br>
Email: $emailField <br>
Message: $messageField <br>
EOD;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);
/* Results Rendered as HTML */
There are more problems, so:
you missed name attribute at the name text input
you used incorect names, PHP is case-sensitive in keys ($_POST['name'] !== $_POST['Name']). This is the point why you received blank meesages
for attribute in labels has to correspond with input ID
Updated code:
<form class="contact_form" action="php/mail.php" method="post" name="contact_form">
<ul>
<li>
<label for="Name">Name*</label>
<input type="text" id="Name" name="Name" placeholder="Your Name" required />
</li>
<li>
<label for="Email">Email*</label>
<input type="email" name="Email" id="Email" placeholder="Your email address" required />
<span class="form_hint">Proper format "name#something.com"</span>
</li>
<li>
<label for="Website">Website</label>
<input type="url" id="Website" name="Website" placeholder="Your website" required pattern="(http|https)://.+"/>
<span class="form_hint">Proper format "http://someaddress.com"</span>
</li>
<li>
<label for="Message">Message:</label>
<textarea rows="6" id="Message" cols="40" name="Message" ></textarea>
</li>
<li>
<button class="submit" type="submit">Send</button>
</li>
</ul>
</form>
I think you got typo there, change
$headers = "From: $email\r\n";
to
$headers = "From: $emailField\r\n";
Remember to validate the $emailField to avoid injections!
Update: Now that I can see the actual HTML for the form, there is some problems.
I think for requires and ID field, so you should add one to every field in the form.
You also forgot to set name in the name text field.
Lets add the name attribute in the name field, and add the ID field (which should be done for the other fields also):
<label for="name">Name*</label>
<input type="text" placeholder="Your Name" required />
to
<label for="name">Name*</label>
<input type="text" id="name" name="name" placeholder="Your Name" required />
$_POST['Name'] is case-sensetive, so you should change your code so the variables match the names in the html.
example:
$nameField = $_POST['Name'];
should be
$nameField = $_POST['name'];
put $nameField = $_POST['Name'];
$emailField = $_POST['Email'];
$website = $_POST['Website'];
$messageField = $_POST['Message']; within a post function. After verifying $_post is not empty. on form submit.
I have a contact form that is only passing over some of the form fields and i wondered if anyone can help as to why?
The html form is as follows:
<form enctype="multipart/form-data" method="post" action="referral.php" onsubmit="return submitForm()">
<fieldset>
<div class="field">
<input name="name" id="name" type="text" class="validate" placeholder="Your Name" />
<span class="form-msg" id="name-error">Please add your name</span>
</div>
<div class="field here-for-next-until">
<input name="email" id="email" type="text" class="validate" placeholder="Your E-mail" class="text-input validate" />
<span class="form-msg" id="email-error">Please add your e-mail address</span>
</div>
<div class="field">
<input name="phone" id="phone" type="text" placeholder="Your Phone Number" class="text-input validate" />
<span id="phone-error" class="form-msg" >Please add your phone number</span>
</div>
<div class="area here-for-next-until">
<textarea name="message" id="message" class="text-input validate" placeholder="Introduce Yourself"></textarea><br/>
<span class="form-msg" id="email-error">Please add an introduction to yourself</span>
<input type="text" id="cap_val" name="cap_val" class="code validate" placeholder="Enter Code From Right"/>
<img src="bin/captcha.php" id="cap"/><img src="images/refresh.jpg" id="cap-refresh" onclick="change_captcha()"/><br/>
<span id="captcha-error" class="form-msg" >Please add the above code</span>
<div class="clear here-for-next-until"></div>
</div>
</div>
<div class="flt-lt w420">
<h2 class="subheader">Client</h2>
<div class="field">
<input name="referralsname" id="name" type="text" class="validate" placeholder="Name" />
<span class="form-msg" id="name-error">Please add your referrals name</span>
</div>
<div class="field here-for-next-until">
<input name="referralsemail" id="email" type="text" class="validate" placeholder="E-mail address" class="text-input validate" />
<span class="form-msg" id="email-error">Please add your referrals e-mail address</span>
</div>
<div class="field">
<input name="referralsphone" id="phone" type="text" placeholder="Phone Number" class="text-input validate" />
<span id="phone-error" class="form-msg" >Please add your referrals phone number</span>
</div>
<div class="area here-for-next-until">
<textarea name="referralsmessage" id="message" class="text-input validate" placeholder="Introduce Yourself"></textarea><br/>
<span class="form-msg" id="email-error">Please add an introduction to yourself</span>
<div class="clear here-for-next-until"></div>
<div id="submit-holder">
<input class="submit" type="submit" value="Submit"/>
<input id="reset" class="submit" type="reset" value="Reset"/>
</div>
</div>
</fieldset>
</form>
Basically this is two very similar contact forms side by side that share a submit button
When posted it goes to this:
$owner_email = 'myemail.co.uk';
$subject = A message from your site visitor ' . $_POST["name"];
$messageBody = "\nVisitor: " . $_POST["name"] . "\n";
$messageBody .= "Email Address: " . $_POST['email'] . "\n";
$messageBody .= "Phone Number: " . $_POST['phone'] . "\n";
$messageBody .= "Message: " . $_POST['message'] . "\n";
$messageBody = "\nVisitor: " . $_POST["referralsname"] . "\n";
$messageBody .= "Email Address: " . $_POST['referralsemail'] . "\n";
$messageBody .= "Phone Number: " . $_POST['referralsphone'] . "\n";
$messageBody .= "Message: " . $_POST['referralsmessage'] . "\n";
However when I submit the form I onyl get one set of details (the first 4 $messageBody)
Any help anyone could offer would be greatly appreciated.
Thanks again
David
Change:
$messageBody = "\nVisitor: " . $_POST["referralsname"] . "\n";
To:
$messageBody .= "\nVisitor: " . $_POST["referralsname"] . "\n";
(.= instead of =, you are re-assigning value to $messageBody instead of chaining it to $messageBody)
if i had to guess, i would say it is because you are repeating the IDs. In HTML element IDs have to be unique. make all IDs match your "name" values. im referring to
<input name="phone" id="phone"....
<input name="referralsphone" id="phone"....
change all these cases to:
<input name="phone" id="phone"....
<input name="referralsphone" id="referralsphone"....
I would also suggest checking the contents of POST. this question shows how Echo an $_POST in PHP
I have a form on my site. When the user submits the form I get the email response but there is no form data returned. All I see is
Company:
Name:
Email:
Phone:
The form html is below:
<form method="post" action="send_survey.php" class="survey" >
<header><img src="../img/PTS_Survey_logo.jpg" alt="Patterson Tubular Services Logo">
Customer Survey</header>
<fieldset>
<section>
<label class="input">
<i class="icon-append icon-group"></i>
<input type="company" id="company" required placeholder="Company name">
</label>
</section>
<section>
<label class="input">
<i class="icon-append icon-user"></i>
<input type="name" id="name" required placeholder="Your name" >
</label>
</section>
<section>
<label class="input">
<i class="icon-append icon-envelope-alt"></i>
<input type="email" id="email" required placeholder="Your e-mail">
</label>
</section>
<section>
<label class="input">
<i class="icon-append icon-phone"></i>
<input type="phone" id="phone" placeholder="Your phone number">
</label>
</section>
</fieldset>
<footer>
<input id="submit" name="submit" type="submit" value="submit" class="button">Submit the survey</button>
</footer>
</form>
The php code is below:
<?php
/*Subject and email variables */
$emailSubject = 'PTS Site Test';
$emailTo = 'testemail#testemail.com';
/* Gather form information */
$company = $_POST['company'];
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$body = <<<EOD
<br><hr><br>
Company: $company <br>
Name: $name <br>
Email: $email <br>
Phone: $phone <br>
EOD;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail ($emailTo, $emailSubject, $body, $headers);
/* Results rendered */
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL=http://www.cudd.com/PTS-2014/index.html">';
?>
Thank you in advance.
HTML form input is set in POST by the element's name attribute, not its id. You should set the name attribute to the same as the id on each of your input elements.
You have no name attribute on any of your inputs
Change:
<input type="company" id="company" required placeholder="Company name">
To:
<input type="company" name="company" id="company" required placeholder="Company name">