我正在使用带有文件附件的 PHP 表单。由于对 PHP 的了解不足,我使用在线教程中的此脚本。
我的问题是:
我想通过邮件获取所有用户提交的表单信息,但不知道如何在此脚本上添加代码(检查最后一个代码部分)。
我希望用户只能在文件附件上发送 .txt、.doc、.docx 文件格式。
HTML 代码:
<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 代码-
<?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");
}
?>
我想在电子邮件正文中添加的字段代码 -
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]