Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用php与pear mail.php和mime.php连接多个文件?_Php_Html_Email_Pear_Mime Mail - Fatal编程技术网

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

如何使用php与pear mail.php和mime.php连接多个文件?,php,html,email,pear,mime-mail,Php,Html,Email,Pear,Mime Mail,我无法附加多个文件。如果我的附件文件多于一个文件,电子邮件总是发送一个文件附件,但我的附件多于一个文件 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

我无法附加多个文件。如果我的附件文件多于一个文件,电子邮件总是发送一个文件附件,但我的附件多于一个文件

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 = "\n";
    $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>';
    }
    }
}
if(isset($\u POST['upload'])){
包括('config.php');
包括_once('Mail.php');
包括_once('Mail/mime.php');
$allowed_ext=array('jpg','bmp','gif','png','jpeg');
对于($i=0;$i$to,'Subject'=>$Subject);
$namafile[$i]=“$file\u path/$file\u name”;
$crlf=“\n”;
$mime=新邮件\u mime($crlf);
$mime->setTXTBody($body);
$mime->addAttachment($namafile[$i],'image/jpg');
$body=$mime->get();
$headers=$mime->headers($headers);
$smtp=Mail::工厂('smtp',阵列('host'=>$host,
'port'=>$port',auth'=>true,
'用户名'=>$useremail',密码'=>$password));
$mail=$smtp->send($to、$headers、$body);
如果($邮件){
echo‘成功:福托·特基里姆!’;
}否则{
echo“错误:Foto gagal dikirim!”;
}
}
}

我的代码怎么了?请帮帮我。给我决心。。。。谢谢你。。。。。我的英语是sry,我来自印度尼西亚。所以说英语就好了。sry….

我很快重新排列了代码,对此没有任何保证:

<?php

if(isset($_POST['upload'])){
    include ('config.php');
    include_once ('Mail.php');
    include_once ('Mail/mime.php');
    $allowed_ext = array('jpg','bmp','gif','png','jpeg');



    $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 = "\n";
    $mime = new Mail_mime($crlf);
    $mime->setTXTBody($body);


    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')");
        }

        $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>';
    }


}

我自己解决了。我能做到

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 = "\n";
    $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); }
}
if(isset($\u POST['username'])和&isset($\u POST['judul']))&&
($_POST['berita'])){
包括('config.php');
包括_once('Mail.php');
包括_once('Mail/mime.php');
$username=$_POST['username'];
$judul=$_POST['judul'];
$berita=$_POST['berita'];
$tanggal_kirim=日期(“Y-m-d H:i:s”);
$query=“选择电子邮件、密码\来自用户名为“$username”的用户的电子邮件;
$result=mysql\u query($query);
list($useremail,$password)=mysql\u fetch\u row($result);
mysql_free_result($result);
$from=$useremail;
$subject=$judul;
$body=$berita;
$to=”babagusandrian@gmail.com";
$host=”ssl://smtp.gmail.com";
$port=“465”;
$crlf=“\n”;
$headers=array('From'=>$From',To'=>$To',Subject'=>$Subject);
$mime=新邮件\u mime($crlf);
$mime->setTXTBody($body);
$arr_files=array();
$file_path=“foto”;
$file\u count=0;
对于($i=0;$iaddAttachment($file\u target,“image/”)$file\u ext,$file\u type);
}
}
$body=$mime->get();
$headers=$mime->headers($headers);
$smtp=Mail::工厂('smtp',阵列('host'=>$host,
'port'=>$port',auth'=>true,
'用户名'=>$useremail',密码'=>$password));
$mail=$smtp->send($to、$headers、$body);
如果($邮件){
mysql_查询(“插入到foto值中(“'$username'、'$tanggal_kirim'、'$judul'、'$berita'、'$file_count');
echo‘成功:福托·特基里姆!’;
}否则{
echo“错误:Foto gagal dikirim!”;
}
//sukses atopun gagal,tetap删除文件
foreach($arr_文件为$namafile){unlink($namafile);}
}

您正在for循环中发送,因此每个文件发送一封电子邮件。要将每个文件发送到一封电子邮件,您需要重新排列代码,以便在使用addAttachment方法添加文件的循环之后进行发送hmm…for循环在哪里更改?