簡體   English   中英

使用PEAR發送帶附件的電子郵件表單

[英]Sending an email form with attachment using PEAR

我已經使用PEAR Mail_Mime創建了一個HTML。 一旦表單被提交,我得到的附件沒問題。 我遇到的問題是表單的輸入字段(姓名,電話,電子郵件)不包含在我收到的電子郵件中。 我怎樣才能獲得這些信息? 對於PHP來說,我是Noobie,所以要溫柔。

    <?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 = 'gradysapp@gmail.com';//<<--  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>
$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";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM