dops57958 2017-03-29 06:18 采纳率: 0%
浏览 91

如何在PHP中将文件从一封电子邮件发送到另一封电子邮件ID?

I have PHP code where i code for sending attached file via email from one email id to another email id, But the name ,message ,email_id all send but not attached file sended.

<?php

   $name=$_REQUEST['name'];
   $email_id=$_REQUEST['email_id'];
   $message=$_REQUEST['message'];
   $attached_file=$_REQUEST['attached_file'];
   $name= mysql_real_escape_string($name);
   $email_id = mysql_real_escape_string($email_id);
   if(isset($_REQUEST['submit']))
   {
    $sql="select * from user where BINARY name = BINARY '$name' AND
     BINARY email_id = BINARY '$email_id' and status='1'";  
   $qex=mysql_query($sql);
   $numrow=mysql_num_rows($qex);
   $res=mysql_fetch_array($qex);    
   if($numrow==1)
  {
    $_SESSION['name']=$res['name'];
   $_SESSION['email_id']=$res['email_id'];


        $email_to .= "nehamitta919@gmail.com";
        $email_subject="A user send Tariffs file to their Client";
        $email_from="neha@studioscue.in";

        $email_message = "User Attached File Successfully send     ".$time."

";


        $email_message .= "User Name  : ".$name."

";
        $email_message .= "User Email: ".$email_id."

";
        $email_message .= "User Message: ".$message."

";
        $email_message .= "User Send File: ".$attached_file."

";


        $headers = 'From: '.$email_from."
".
        'Reply-To: '.$email_from."
" .
        'X-Mailer: PHP/' . phpversion();
        @mail($email_to, $email_subject, $email_message, $headers);

        ?>

html code :

<form class="form-horizontal" method="post" enctype="multipart/form-data">
                    <div class="row">
                        <div class="col-md-12">
                            <div class="form-group">
                                <label for="inputEmail3" class="col-sm-4 control-label">User name</font></label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" id="inputEmail3" placeholder="Name" required
                                name="name" value="<?Php echo $uvw['name']; ?>">
                            </div>
                            </div>
                        </div>
                    </div><!----row-------->
                    <br>
                    <br>
                <div class="row">
                    <div class="col-md-12">
                        <div class="form-group">
                            <label for="inputEmail3" class="col-sm-4 control-label">User Email Id<br><strong>(Send From)</strong></font></label>
                        <div class="col-sm-4">
                            <input type="text" class="form-control" id="inputEmail3" ng-model="email_id" placeholder="email" required
                            name="email_id" value="<?Php echo $uvw['email_id']; ?>">
                        </div>
                        </div>
                    </div>
                </div><!----row-------->
                <br>
                <br>                                              
            <div class="row">
                <div class="col-md-12">
                    <div class="form-group">
                        <label for="inputEmail3" class="col-sm-4 control-label">Send To</font></label>
                    <div class="col-sm-4">
                        <select required class="form-control" id="client_id" name="client_id" ng-model="client_id">
                                          <option required="required" selected="" hidden="" value="">Select Your Client Email Id</option>
                                           <?php
                        $id= $_SESSION['id'];
                        $qex = mysql_query("select * from client where status!='4' and status!='0' and client.user_id='$id'");
                                            while ($res = mysql_fetch_array($qex)) {
                                                ?>
                                                <option value="<?php echo trim($res['group_id']); ?>">
                                                    <?php
                                                    echo trim($res['group_id']);
                                                    ?>
                                                </option>
                                            <?php }
                                            ?>

                    </select>
                </div>
                </div>
            </div>
        </div><!----row-------->
        <br />
        <br />
        <div class="row">
            <div class="col-md-12">
                <div class="form-group">
                    <label for="inputEmail3" class="col-sm-4 control-label">Message</font></label>
                <div class="col-sm-4">
                    <textarea class="form-control" id="inputEmail3" placeholder="Message" required style="height: 50px;"
                    name="message"></textarea>
                </div>
                </div>
            </div>
        </div><!----row-------->
        <br>
        <br>
    <div class="row"
        <div class="col-md-12">
            <div class="form-group">
                <label for="inputEmail3" class="img-rounded col-sm-4 control-label animate-css-css" >Attach File</font></label>
            <div class="col-sm-3">
                <input type="file"name="attached_file" id="attached_file" required>
            </div>
            </div>
        </div><!----row-------->
        <br>
        <br />  
    <div class="form-group">
        <div class="col-sm-offset-5 col-sm-5">
            <button type="submit" class="btn btn-primary col-md-5" name="submit">Send File</button>
        </div>

    </div>
            </form>

Please check & reply me. what am i missing. Thanks & Regards Ankit

  • 写回答

3条回答 默认 最新

  • dongzhi4470 2017-03-29 06:25
    关注

    try this code,mention your file name as attachment...

     <?php
            ini_set('max_execution_time', 300);
    
            $to = "example@gmail.com";
    $subject= "Contact Mail";
    
    $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
    
             $headers = "From:<developer@gmail.com>"."
    " .
             "MIME-Version: 1.0
    " .
                "Content-Type: multipart/mixed;
    " .
                " boundary=\"{$mime_boundary}\"";
    
             $message = "This is a multi-part message in MIME format.
    
    " .
                "--{$mime_boundary}
    " .
                "Content-Type: text/plain; charset=\"iso-8859-1\"
    " .
                "Content-Transfer-Encoding: 7bit
    
    " .
                "Contact Details"."
    ".
             "Name :". $_POST['name']."
    ".
              "email :". $_POST['email']."
    ";
    
     $message.="comments :". $_POST['comments']."
    "        
     ."
    
    ";
    
    
    
             foreach($_FILES as $userfile)
             {
                $tmp_name = $userfile['tmp_name'];
                $type = $userfile['application/vnd.openxmlformats-officedocument.wordprocessingml.document'];
                $name = $userfile['name'];
                $size = $userfile['size'];
    
                if (file_exists($tmp_name))
                {
                   if(is_uploaded_file($tmp_name))
                   {
                      $file = fopen($tmp_name,'rb');
    
                      $data = fread($file,filesize($tmp_name));
    
                      fclose($file);
    
    
                      $data = chunk_split(base64_encode($data));
                   }
    
                   $message .= "--{$mime_boundary}
    " .
                      "Content-Type: {$type};
    " .
                      " name=\"{$name}\"
    " .
                      "Content-Disposition: attachment;
    " .
                      " filename=\"{$fileatt_name}\"
    " .
                      "Content-Transfer-Encoding: base64
    
    " .
                   $data . "
    
    ";
                }
             }
    
             $message.="--{$mime_boundary}--
    ";
    
    
    
             if (mail($to, $subject, $message, $headers)){
             echo "<script>alert('Mail sent Successfully');</script>";
                echo "<script>window.location = 'contact.php';</script>";
    
             } else {
                echo "<script>alert('Mail Not Send');</script>";
                echo "<script>window.location = 'contact.php';</script>";
    
    
            } 
            ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM