0

我正在使用带有文件附件的 PHP 表单。由于对 PHP 的了解不足,我使用在线教程中的此脚本。

我的问题是:

  1. 我想通过邮件获取所有用户提交的表单信息,但不知道如何在此脚本上添加代码(检查最后一个代码部分)。

  2. 我希望用户只能在文件附件上发送 .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]
4

2 回答 2

1

您的输入缺少该name属性。为了让 PHP 捕获表单值,您需要将name属性添加到输入中,然后通过引用这些名称来捕获输入值。例如:

您正在First Name从 from 中捕获,如下所示:

First Name: $_POST[f_name]

因此,获取您输入的名称应该有一个 name 属性,f_name如下所示:

<input type="text"name="f_name"size="40" id="job_title" style="width:250px;" />

于 2013-10-15T17:49:04.497 回答
1

要将捕获的数据添加到 sendmail 脚本:

  1. 正确命名您的变量(中间没有空格),例如,

     First Name: $_POST[f_name]
     Middle Name: $_POST[m_name]
     Last Name: $_POST[l_name]
    

应该是(记住'$'符号告诉php它是一个变量:

     $First_Name: $_POST['f_name'];
     $Middle_Name: $_POST['m_name'];
     $Last_Name: $_POST['l_name'];

对所有人都这样做,并将此代码放在此脚本之前://选定文件的临时路径

     $file1 = $_FILES['resume']['tmp_name'];
     $file2 = $_FILES['cover_letter']['tmp_name'];
  1. 在您拥有的代码中:

    // 多部分边界 $message = "--{$boundary}\n" . “内容类型:文本/纯文本;字符集=\”iso-8859-1\“\n”。

    $message .= "--{$boundary}\n";

添加这个:

$message .= "First name: ".clean_string($First_Name)."\r\n";

$message .= "Last name: ".clean_string($Last_Name)."\r\n";

对您想要从上面的 (1) 添加的所有变量执行此操作,您的脚本现在应该可以正常工作了。

于 2014-01-24T06:59:26.233 回答