在上传之前用phpmailer发送文件? [英] Send files with phpmailer before uploading them?

查看:109
本文介绍了在上传之前用phpmailer发送文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过phpmailer脚本发送带附件的电子邮件时遇到问题。如果我想将单个文件添加到邮件中,我有一个工作代码。但是当涉及到多个文件时,它们看起来好像还没有上传。



我的单个文件的代码:

  if(isset($ _ FILES ['file'])&& 
$ _FILES ['file'] ['error'] == UPLOAD_ERR_OK)
{
$ mail-> AddAttachment($ _ FILES ['file'] ['tmp_name'],
$ _FILES ['file'] ['name']); $(!$ mail-> Send())
{
header(Location:。$ returnErrorPage);


}
else
{
header(Location:。$ returnHomePage);
}

}

我试了几个代码,应该循环遍历$ _FILES中的所有文件,但没有成功。然后我测试了以下代码:

  $ count = count($ _ FILES ['file'] ['tmp_name']); 
echo $ count;

它返回0.我知道$ _FILES是空的,但我不知道原因。我需要缓冲文件或类似的东西?



编辑:
这里是我的HTML代码发送文件和其他数据到脚本: / p>

 < form id =form_907007class =appnitromethod =postaction =server / phpmailer.php 
enctype =multipart / form-data>
< p>选择资料(txt,html等):< br>
< input name =filetype =filesize =50maxlength =100000multiple>
< / p>
< / form>


解决方案

我的问题的解决方案基于同步,首先上传文件,然后发送电子邮件。



在我的html代码中,我必须改变这一行:

 < input name =filetype =filesize =50maxlength =100000multiple> 

< input name =file []type =filesize =50maxlength =100000multiple>

重要的是要做到这一点,以便在稍后使用php来访问每个要上传的文件。



第二步是循环遍历所有文件并将它们存储在服务器上。这是我做到这一点的方式:

$ $ $ $ $ $ $ $ $ $ $ foreach($ _FILES [file] [error] as $ key = > $ error){

if($ error == UPLOAD_ERR_OK){
$ tmp_name = $ _FILES [file] [tmp_name] [$ key];
$ name = $ _FILES [file] [name] [$ key];
$ b $ move_uploaded_file($ tmp_name,server / data / $ name}

在下一步中,我检查文件是否上传成功,如果返回= TRUE,我将它们添加为邮件附件:

  if(move_uploaded_file($ tmp_name,server / data / $ name))
{
$ mail-> AddAttachment(server / data / $ name);
}

如果一切顺利,我可以在 之后删除

  if($ mail-> Send()){

foreach($ _FILES [ file] [error] as $ key => $ error)
{
$ name = $ _FILES [file] [name] [$ key];
unlink($ name);
}

header(Location:。$ returnPage);
exit;}



感谢您的帮助!


I have a problem with sending emails with attachment by the phpmailer script. I have a working code if I want to add a single file to the mail. But when it comes to multiple files, it looks like they are not even uploaded.

My code for a single file:

if (isset($_FILES['file']) &&
$_FILES['file']['error'] == UPLOAD_ERR_OK)
{
$mail->AddAttachment($_FILES['file']['tmp_name'],
                     $_FILES['file']['name']);

            if(!$mail->Send())
          {
           header("Location: " . $returnErrorPage);
          }
          else
          {
             header("Location: " . $returnHomePage);
          }             

}

I tried a few codes that should loop through all files in $_FILES without success. Then I tested the following code:

$count = count($_FILES['file']['tmp_name']);
echo $count;

it returns 0. I know that $_FILES is empty, but I dont know the reason for that. Do I need to buffer the files or something like that?

EDIT: here is my html code which sent the files and other data to the script:

<form id="form_907007" class="appnitro" method="post" action="server/phpmailer.php"       
 enctype="multipart/form-data">
<p>Choose data (txt, html etc.):<br>
<input name="file" type="file" size="50" maxlength="100000" multiple>
</p>
</form>

解决方案

The solution of my problem is based on the idea from Synchro, upload the files first and then send the email.

In my html code I had to change this line:

<input name="file" type="file" size="50" maxlength="100000" multiple>

<input name="file[]" type="file" size="50" maxlength="100000" multiple>

it is important to do this little step to reach each file you want to upload later in php.

The second step is to loop through all files an store them on your server. This is the way I did that:

foreach ($_FILES["file"]["error"] as $key => $error){

if ($error == UPLOAD_ERR_OK) {
    $tmp_name = $_FILES["file"]["tmp_name"][$key];
    $name = $_FILES["file"]["name"][$key];

move_uploaded_file($tmp_name," server/data/$name"}

In the next step I check if the files are uploaded successful, if return = TRUE I add them as attachement to the mail:

if(move_uploaded_file($tmp_name,"server/data/$name" ))
            {
                $mail->AddAttachment("server/data/$name");
            }

If everything went well I can delete the files after I have send the mail:

if($mail->Send()){

    foreach ($_FILES["file"]["error"] as $key => $error)
    {
    $name = $_FILES["file"]["name"][$key];
    unlink("$name");
    }

header("Location: " . $returnPage);
exit;}

Thank you for all your help!

这篇关于在上传之前用phpmailer发送文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆