2012-08-15 91 views
0

我已經使用PEAR Mail_Mime創建了一個HTML。一旦表單被提交,我會得到附件沒有問題。我遇到的問題是表單的輸入字段(名稱,電話,電子郵件)不包含在我收到的電子郵件中。我怎樣才能得到這個信息呢?當談到PHP時,我是一個Noobie,所以要溫和。使用PEAR發送附件的電子郵件表格

<?php 
    include('PEAR/Mail.php'); 
    include('PEAR/Mail/mime.php'); 
    $max_allowed_file_size = 900; // size in KB 
    $allowed_extensions = array("pdf", "doc", "docx", "txt"); 
    $upload_folder = './uploads/'; //<-- this folder must be writeable by the script 
    $your_email = '[email protected]';//<<-- update this to your email address 

    $errors =''; 

    if(isset($_POST['submit'])) 
    { 
//Get the uploaded file information 
$name_of_uploaded_file = basename($_FILES['uploaded_file']['name']); 

//get the file extension of the file 
$type_of_uploaded_file = substr($name_of_uploaded_file, 
         strrpos($name_of_uploaded_file, '.') + 1); 

$size_of_uploaded_file = $_FILES["uploaded_file"]["size"]/1024; 

///------------Do Validations------------- 
if(empty($_POST['name'])||empty($_POST['email'])) 
{ 
    $errors .= "\n Name and Email are required fields. "; 
} 
if(IsInjected($visitor_email)) 
{ 
    $errors .= "\n Bad email value!"; 
} 

if($size_of_uploaded_file > $max_allowed_file_size) 
{ 
    $errors .= "\n Size of file should be less than $max_allowed_file_size"; 
} 

//------ Validate the file extension ----- 
$allowed_ext = false; 
for($i=0; $i<sizeof($allowed_extensions); $i++) 
{ 
    if(strcasecmp($allowed_extensions[$i],$type_of_uploaded_file) == 0) 
    { 
     $allowed_ext = true;   
    } 
} 

if(!$allowed_ext) 
{ 
    $errors .= "\n The uploaded file is not supported file type. ". 
    " Only the following file types are supported: ".implode(',',$allowed_extensions); 
} 

//send the email 
if(empty($errors)) 
{ 
    //copy the temp. uploaded file to uploads folder 
    $path_of_uploaded_file = $upload_folder . $name_of_uploaded_file; 
    $tmp_path = $_FILES["uploaded_file"]["tmp_name"]; 

    if(is_uploaded_file($tmp_path)) 
    { 
     if(!copy($tmp_path,$path_of_uploaded_file)) 
     { 
      $errors .= '\n error while copying the uploaded file'; 
     } 
    } 

    //send the email 
    $name = $_POST['name']; 
    $visitor_email = $_POST['email']; 
    $user_message = $_POST['message']; 
    $to = $your_email; 
    $subject="New form submission"; 
    $from = $your_email; 
    $text = "A user $name has sent you this message:\n $user_message"; 

    $message = new Mail_mime(); 
    $message->setTXTBody($text); 
    $message->addAttachment($path_of_uploaded_file); 
    $body = $message->get(); 
    $extraheaders = array("From"=>$from, "Subject"=>$subject,"Reply-To"=>$visitor_email); 
    $headers = $message->headers($extraheaders); 
    $mail = Mail::factory("mail"); 
    $mail->send($to, $headers, $body); 
    //redirect to 'thank-you page 
    header('Location: careers_thank-you.html'); 
} 
    } 
    ///////////////////////////Functions///////////////// 
    // Function to validate against any email injection attempts 
    function IsInjected($str) 
    { 
    $injections = array('(\n+)', 
      '(\r+)', 
      '(\t+)', 
      '(%0A+)', 
      '(%0D+)', 
      '(%08+)', 
      '(%09+)' 
     ); 
    $inject = join('|', $injections); 
    $inject = "/$inject/i"; 
    if(preg_match($inject,$str)) 
    { 
    return true; 
    } 
    else 
    { 
    return false; 
    } 
    } 
    ?> 

這裏是表格,如果在這裏的代碼有任何錯誤。

<?php 
if(!empty($errors)) 
{ 
echo nl2br($errors); 
} 
?> 
<form method="POST" name="email_form_with_php" 
action="php-form-action.php" enctype="multipart/form-data"> 
<p> 
<label for='name'>Name: </label><br> 
<input type="text" name="name" > 
</p> 
<p> 
<label for='email'>Email: </label><br> 
<input type="text" name="email" > 
</p> 
<p> 
<label for='phone'>Phone: </label><br> 
<input type="text" name="phone" >   
<p> 
<label for='position'>Which position are you applying for? </label><br> 
<input type="text" name="position" >   
    <p> 
<label for='resume_uploaded_file'>Please attach your resume or work history.</label><br> 
<input type="file" name="uploaded_file">  
<span class="smallNote">(PDF or Word Document)</span> 
    <p> 
    <label for='letter_uploaded_file'>Please attach your cover letter.</label><br> 
<input type="file" name="letter_uploaded_file">  
    <span class="smallNote">(PDF or Word Document)</span> 
    <p> 
    <input type="submit" value="Submit" name='submit'> 
</form> 

回答

2
$name = $_POST['name']; 
$visitor_email = $_POST['email']; 
$user_message = $_POST['message']; 
$to = $your_email; 
$subject="New form submission"; 
$from = $your_email; 
$text = "A user $name has sent you this message:\n $user_message"; 

我想你只需要添加一對夫婦行:

$phone = $_POST["phone"]; 

$text = "A user $name has sent you this message:\n $user_message"; 
$text .= "Phone: " . $phone . "\n"; 
$text .= "Email: " . $visitor_email . "\n"; 
+0

這是它!非常感謝Andrewsi – 2012-08-15 19:52:34