在PHP中发送带图像附件的邮件的问题

时间:2014-07-04 15:20:46

标签: php email

我正在做一个应用程序,它有一个带有三个文件输入的联系表单...当我发送电子邮件时,根本不显示文件,简单地显示一个大的编码文本。我做错了什么?谢谢' S。这是我的功能(处理电子邮件的代码的一部分。

function send_mail() {

    $to = "me@me.com";
    $fromEmail = $_POST['email']; 
    $fromName = $_POST['first_name']; 
    $subject = 'mail web'; 


    /* Encoding file message_data */
    $message_data = chunk_split(base64_encode($message_data)); 

    $file_Name = $_FILES['fileToUpload']['tmp_name']; 
    $file_Type = $_FILES['fileToUpload']['type']; 
    $file_tmp_Name = $_FILES['fileToUpload']['name'];

    /* a boundary string */
    $randomVal = md5(time()); 
    $mimeBoundary = "==Multipart_Boundary_x{$randomVal}x"; 

    /* Header for File Attachment */
    $headers .= "\nMIME-Version: 1.0\n"; 
    $headers .= "Content-Type: multipart/mixed;\n" ;
    $headers .= " boundary=\"{$mimeBoundary}\""; 
    /* Multipart Boundary above message */
    $message = "This is a multi-part message in MIME format.\n\n" . 
    "--{$mimeBoundary}\n" . 
    "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . 
    "Content-Transfer-Encoding: 7bit\n\n" . 
    $message . "\n\n"; 

    for($x=0;$x<3;$x++){
      $tmpName = $file_Name[$x];
      $fileType = $file_Type[$x];
      $fileName = $file_tmp_Name[$x];

      if (isset($tmpName)) { 

        $file = fopen($tmpName,'rb'); 
        $msg_data = fread($file,filesize($tmpName)); 
        fclose($file); 

        /* Encoding file msg_data */
        $msg_data = chunk_split(base64_encode($msg_data)); 

        /* Adding attchment-file to message*/
        $message .= "--{$mimeBoundary}\n" . 
        "Content-Type: {$fileType};\n" . 
        " name=\"{$fileName}\"\n" . 
        "Content-Transfer-Encoding: base64\n\n" . 
        $msg_data . "\n\n" . 
        "--{$mimeBoundary}--\n"; 
      } 
    }

    $ok = @mail($to, $subject, $message, $headers); 
    if ($ok) { 
        echo "<p>mail sent to $to!</p>"; 
    } else { 
        echo "<p>mail could not be sent!</p>"; 
    } 

} //end function

HTML表格

<div id="formulario" class="col-sm-10 col-sm-offset-1 col-lg-offset-0">
    <form method="POST"  role="form" class="form-horizontal" id="iwantForm"
        action="iwant-process.php"  enctype="multipart/form-data" >

        <div class="col-sm-6">
            <div class="form-group">
                <label for="gender" class="col-md-5 control-label">Select Gender: </label>                    
                <div class="col-md-7">
                    <select class="form-control" name="Gender">
                        <option class="selectBoxTextColor" value="female">Female</option>
                        <option class="selectBoxTextColor" value="men">Men</option></select>               
                </div>
            </div>
            <div class="form-group">
                    <label class='col-md-5 control-label' for='age'>Age:</label>
                    <div class="col-md-7">
                        <input class='form-control' type='text' name='age' id='age' required /></div>
                </div>  
            <div class="form-group">
                <label class='col-md-5 control-label' for='first_name'>First Name:</label>
                <div class="col-md-7"><input class='form-control' type='text' name='first_name' id='first_name' required />
                </div></div>           
            <div class="form-group">
                <label class='col-md-5 control-label' for='last_name'>Last Name:</label>
                <div class="col-md-7">
                    <input class='form-control' type='text' name='last_name' id='last_name' required />
                </div>
            </div>            
            <div class="form-group">
                <label class='col-md-5 control-label' for='telephone'>Telephone:</label>
                <div class="col-md-7"><input class='form-control' type='text' name='telephone' id='telephone' required /></div>
            </div>            
            <div class="form-group">
                <label class='col-md-5 control-label' for='email'>Email:</label>
                <div class="col-md-7"><input class='form-control' type='text' name='email' id='email' required /></div>
            </div>            
            <div class="form-group">
                <label class='col-md-5 control-label' for='address'>Address:</label>
                <div class="col-md-7"><input class='form-control' type='text' name='address' id='address' required />
                </div>
            </div>            
            <div class="form-group">
                <label class='col-md-5 control-label' for='city'>City:</label>
                <div class="col-md-7"><input class='form-control' type='text' name='city' id='city' required />
                </div>
            </div>
            <div class="form-group">
                <label class='col-md-5 control-label' for='nationality'>Nationality:</label>
                <div class="col-md-7"><input class='form-control' type='text' name='nationality' id='nationality' required />
                </div>
            </div>   
              <div class="form-group">
                <label class='col-md-5 control-label' for='facebook'>Facebook Link:</label>
                <div class="col-md-7"><input class='form-control' type='text' name='facebook' id='facebook' required />
                </div>
            </div>  
            <div class="form-group">
                <label class='col-md-5 control-label' for='nationality'>Comment:</label>
                <div class="col-md-7">
                    <textarea name="message" id="" cols="30" rows="10"></textarea>
                </div>
            </div>          
        </div> <!-- /.col-sm-6  -->
        <div class="col-sm-6">

            <div class="col-sm-6">
                <div class="form-group">
                    <label class='col-md-5 control-label' for='height'>Height:</label>
                    <div class="col-md-7">
                        <input class='form-control' type='text' name='height' id='height' required /></div>
                </div>          
                <div class="form-group">

                    <label class='col-md-5 control-label' for='size'>Size:</label>
                        <div class="col-md-7"><input class='form-control' type='text' name='size' id='size'  /></div>
                </div>            
                <div class="form-group">
                    <label class='col-md-5 control-label' for='waist'>Waist:</label>
                    <div class="col-md-7"><input class='form-control' type='text' name='waist' id='waist' required /></div>
                </div>            
                <div class="form-group">
                    <label class='col-md-5 control-label' for='bust'>Bust:</label>
                    <div class="col-md-7"><input class='form-control' type='text' name='bust' id='bust' required /></div>
                </div>            
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class='col-md-5 control-label' for='hips'>Hips:</label>
                    <div class="col-md-7"><input class='form-control' type='text' name='hips' id='hips' required /></div>
                </div>            
                <div class="form-group">
                    <label class='col-md-5 control-label' for='eyes'>Eyes:</label>
                    <div class="col-md-7"><input class='form-control' type='text' name='eyes' id='eyes'  /></div>
                </div>            
                <div class="form-group">
                    <label class='col-md-5 control-label' for='hair'>Hair:</label>
                    <div class="col-md-7"><input class='form-control' type='text' name='hair' id='hair'  /></div>
                </div>            
                <div class="form-group">
                    <label class='col-md-5 control-label' for='shoes'>Shoes:</label>
                    <div class="col-md-7"><input class='form-control' type='text' name='shoes' id='shoes'  /></div>
                </div>
            </div>
            <div class="col-sm-12">


                <div class="form-group file">
                    <label class='control-label' for="file1">photo 1: (Face)</label>
                     <input name="fileToUpload[]" id="fileToUpload1" type="file"  />
                </div>
                <div class="form-group file">
                    <label class='control-label' for="file2">photo 2: (Body complete)</label>
                     <input name="fileToUpload[]" id="fileToUpload2" type="file"  />
                </div>
                <div class="form-group file">
                    <label class='control-label' for="file3">photo 3: (Body complete)</label>
                     <input name="fileToUpload[]" id="fileToUpload3" type="file"  />
                </div>

            </div>
        </div> <!-- /.col-sm-6  -->
        <hr>
    <input id="sendBtn" type="submit" value="Send">
    </form>
    </div> <!-- ./col-sm-8 -->

0 个答案:

没有答案