文件上传后发送电子邮件

时间:2018-08-29 10:08:46

标签: php ajax email

我正在处理多个文件的上载脚本,当用户上载一些文件时,我想得到警告。 这是我的上传代码:

if(isset($_POST['submit_image'])){
   for($i=0;$i<count($_FILES["upload_file"]["name"]);$i++){
    $uploadfile=$_FILES["upload_file"]["tmp_name"][$i];
    $folder="galerie/evg/";

    move_uploaded_file($_FILES["upload_file"]["tmp_name"][$i], "$folder".$_FILES["upload_file"]["name"][$i]);

    $to = "benoit@lalilou.com";
    $subject = "Nouvelles photos uploadées";
    $message = "
        <html>
            <head>
                <title>".$userRow['user_firstname']." a uploadé de nouvelles photos.</title>
            </head>
            <body>
                ".$userRow['user_firstname']." a uploadé de nouvelles photos.
                ".$_FILES["upload_file"]["name"][$i]."
                <br/>
                <br/>
            </body>
        </html>
    ";
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
    $headers .= 'From: <webmaster@lalilou.com>' . "\r\n";

}
mail($to,$subject,$message,$headers);
exit();}

我的问题:我收到了邮件,但在邮件中,我仅上传了最后一个文件。而且,如果我将邮件功能放入for循环中,我会收到2或3封邮件,具体取决于上传的图片数量。

我的问题:什么是正确的方法?

预先感谢:)

**** EDIT ****

感谢@menaka 这是正在寻找的人的最终工作代码:

if(isset($_POST['submit_image'])){
$to = "benoit@lalilou.com";
$subject = "Nouvelles photos uploadées";
$message =  "
    <html>
        <head>
            <title>".$userRow['user_firstname']." a uploadé de nouvelles photos.</title>
        </head>
        <body>".$userRow['user_firstname']." a uploadé de nouvelles photos.<br/>";
for($i=0;$i<count($_FILES["upload_file"]["name"]);$i++){
    $uploadfile=$_FILES["upload_file"]["tmp_name"][$i];
    $folder="galerie/evg/";
    move_uploaded_file($_FILES["upload_file"]["tmp_name"][$i], "$folder".$_FILES["upload_file"]["name"][$i]);
    $message .= "<img style='width:200px;' src='http://exemple.com/galerie/".$_FILES["upload_file"]["name"][$i]."' />";
}
$message .= "<br/>
            <br/>
        </body>
    </html>
";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <webmaster@lalilou.com>' . "\r\n";
mail($to,$subject,$message,$headers);
exit();}

2 个答案:

答案 0 :(得分:0)

我认为问题是,您只需要替换循环中的最后一条消息,就可以像这样尝试。

if(isset($_POST['submit_image'])){

    $to = "benoit@lalilou.com";
    $subject = "Nouvelles photos uploadées";
    $message =  "
        <html>
            <head>
                <title>".$userRow['user_firstname']." a uploadé de nouvelles photos.</title>
            </head>
            <body>";

   for($i=0;$i<count($_FILES["upload_file"]["name"]);$i++){
    $uploadfile=$_FILES["upload_file"]["tmp_name"][$i];
    $folder="galerie/evg/";

    move_uploaded_file($_FILES["upload_file"]["tmp_name"][$i], "$folder".$_FILES["upload_file"]["name"][$i]);


    $message .= $userRow['user_firstname']." a uploadé de nouvelles photos.
                ".$_FILES["upload_file"]["name"][$i];


}

$message .= "<br/>
                <br/>
            </body>
        </html>
    ";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <webmaster@lalilou.com>' . "\r\n";

mail($to,$subject,$message,$headers);
exit();}

答案 1 :(得分:-1)

您必须加载每个文件并将其添加到文件名标头中。 我认为这将回答您的问题: How to attach two or multiple files and send mail in PHP