在PHP邮件脚本上添加表单数据

时间:2013-10-15 17:29:50

标签: php html forms email phpmailer

我正在使用带有文件附件的PHP表单。由于PHP知识不足,我使用在线教程中的这个脚本。

我的问题是:

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

  2. 我希望用户只能在文件附件上发送.txt,.doc,.docx文件格式。

  3. 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]
    

2 个答案:

答案 0 :(得分:1)

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

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

First Name: $_POST[f_name]

因此,获取名称,您的输入应具有等于f_name的名称属性,如下所示:

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

答案 1 :(得分:1)

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

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

     First Name: $_POST[f_name]
     Middle Name: $_POST[m_name]
     Last Name: $_POST[l_name]
    
  2. 应该是(记住'$'标志告诉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”。 “Content-Type:text / plain; charset = \”iso-8859-1 \“\ n”。

      $ message。=“ - {$ boundary} \ n”;

    2. 添加:

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

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