douzhuo8312 2015-02-18 01:18
浏览 56
已采纳

如何使用php与pear mail.php和mime.php连接多个文件?

i can't attachment multiple file. If i attachment file more than a files, email send always 1 file attachment, but i attachment more than a file.

if(isset($_POST['upload'])){
    include('config.php');
    include_once('Mail.php');
    include_once('Mail/mime.php');
    $allowed_ext    = array('jpg','bmp','gif','png','jpeg');
    for($i=0; $i<count($_FILES['file']['name']); $i++) {
    $file_name      = $_FILES['file']['name'][$i];
    $file_ext       = strtolower(end(explode('.', $file_name)));
    $file_size      = $_FILES['file']['size'][$i];
    $file_tmp       = $_FILES['file']['tmp_name'][$i];
    $username       = $_POST['username'];
    $judul          = $_POST['judul'];
    $berita         = $_POST['berita'];
    $max_size       = 25*1024*1024;
    $tanggal_kirim  = date("Y-m-d H:i:s");
    if(in_array($file_ext, $allowed_ext) === true){
        if($file_size <= $max_size){
            $file_path = "foto/". $_FILES['file']['name'][$i];
        move_uploaded_file($file_tmp, "$file_path");}
    mysql_query("INSERT INTO foto VALUES('', '$username', '$tanggal_kirim', '$judul', '$berita','0')");
    }
    $query = "SELECT email,password_email FROM users where username='$username'";
    $sql = mysql_query($query);
    $data = mysql_fetch_assoc($sql);

    $subject = $judul;
    $body = $berita;
    //mail($to, $subject, $body,$headers);
    //email tujuan
    $to = "babagusandrian@gmail.com";
    $host = "ssl://smtp.gmail.com";
    $port = "465";
    $from = $useremail = $data['email'];
    $password = $data['password_email'];

    $headers = array('From' => $from, 'To' => $to, 'Subject' => $subject);

    $namafile[$i] = "$file_path/$file_name";
    $crlf = "
";
    $mime = new Mail_mime($crlf);
    $mime->setTXTBody($body);
    $mime->addAttachment($namafile[$i],'image/jpg');

    $body = $mime->get();
    $headers = $mime->headers($headers);

    $smtp = Mail::factory('smtp', array('host' => $host,
            'port' => $port, 'auth' => true,
            'username' => $useremail, 'password' => $password));

    $mail = $smtp->send($to, $headers, $body);
    if($mail){
        echo '<div class="ok">SUCCESS: Foto terkirim!</div>';
    }else{
        echo '<div class="error">ERROR: Foto gagal dikirim!</div>';
    }
    }
}

what's wrong with my code? please help me. give me resolve.... thanks you..... sry for my english, im from indonesia. so just litle speak english. sry....

  • 写回答

2条回答 默认 最新

  • douhui4831 2015-02-18 02:38
    关注

    i was resolved by my self. i can do it....

    if(isset($_POST['username']) && isset($_POST['judul']) &&
        ($_POST['berita']) ) {
        include('config.php');
        include_once('Mail.php');
        include_once('Mail/mime.php');
    
        $username       = $_POST['username'];
        $judul          = $_POST['judul'];
        $berita         = $_POST['berita'];
        $tanggal_kirim  = date("Y-m-d H:i:s");
    
        $query = "SELECT email,password_email FROM users where username='$username'";
        $result = mysql_query($query);
        list($useremail,$password) = mysql_fetch_row($result);
        mysql_free_result($result);
    
        $from = $useremail;
        $subject = $judul;
        $body = $berita;
    
        $to = "babagusandrian@gmail.com";
        $host = "ssl://smtp.gmail.com";
        $port = "465";
    
        $crlf = "
    ";
        $headers = array('From' => $from, 'To' => $to, 'Subject' => $subject);
    
        $mime = new Mail_mime($crlf);
        $mime->setTXTBody($body);
    
        $arr_files = array();
        $file_path = "foto";
        $file_count = 0;
        for($i=0; $i<count($_FILES['fileAttach']['name']); $i++) {
            $file_name = $_FILES['fileAttach']['name'][$i];
            if(!empty($file_name)) {
                $file_count++;
                $file_ext       = strtolower(end(explode('.', $file_name)));
                $file_tmp       = $_FILES['fileAttach']['tmp_name'][$i];
                $file_target    = $file_path.'/'.$file_name.'';
                $file_type      ="image/*";
                move_uploaded_file($file_tmp, $file_target);
                $arr_files[] = $file_target;
                $mime->addAttachment($file_target,"image/".$file_ext, $file_type);
            }
        }
        $body = $mime->get();
        $headers = $mime->headers($headers);
    
        $smtp = Mail::factory('smtp', array('host' => $host,
                'port' => $port, 'auth' => true,
                'username' => $useremail, 'password' => $password));
    
        $mail = $smtp->send($to, $headers, $body);
    
        if($mail){
            mysql_query("INSERT INTO foto VALUES('', '$username', '$tanggal_kirim', '$judul', '$berita','$file_count')");
            echo '<div class="ok">SUCCESS: Foto terkirim!</div>';
        }else{
            echo '<div class="error">ERROR: Foto gagal dikirim!</div>';
        }
        // sukses atopun gagal, tetap delete file
        foreach($arr_files as $namafile) { unlink($namafile); }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测