通过phpmailer实现文件上传并发送邮件

      我的工作 2007-5-29 22:34:00

研究了一周多的php,终于实现了通过php发送附件邮件的功能,以下为代码,供有兴趣的兄弟们参考

或者可以访问:http://php.ezpower.cn查看此演示

以下为upfile.html,即客户端内容

=========================================================

<html>
<head>
<title>phpmailer 发信系统</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">
<!--
td {  font-size: 9pt}
a {  color: #000000; text-decoration: none}
a:hover {  text-decoration: underline}
.tx {  height: 16px; width: 30px; border-color: black black #000000; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 1px; border-left-width: 0px; font-size: 9pt; background-color: #eeeeee; color: #0000FF}
.bt {  font-size: 9pt; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; height: 16px; width: 80px; background-color: #eeeeee; cursor: hand}
.tx1 { height: 20px; width: 30px; font-size: 9pt; border: 1px solid; border-color: black black #000000; color: #0000FF}
-->
</style>
</head>

<body bgcolor="#FFFFFF" text="#000000">
<form name="form1" method="post" action="ups.php" enctype="multipart/form-data" >
  <table border="1" cellspacing="0" cellpadding="0" bordercolorlight="#000000" bordercolordark="#CCCCCC" width="91" height="23">
    <tr>
      <td align="left" valign="middle" height="18" width="18">&nbsp;<input type="hidden" name="MAX_FILE_SIZE" value="10240000"></td>
      <td bgcolor="#CCCCCC" align="left" valign="middle" height="18" width="67"> 文件上传</td>
    </tr>
  </table>
  <br>
 
  <br>
  <table width="450" border="1" cellspacing="0" cellpadding="5" align="center" bordercolordark="#CCCCCC" bordercolorlight="#000000">
    <tr bgcolor="#CCCCCC">
      <td height="22" align="left" valign="middle" bgcolor="#CCCCCC">&nbsp;phpmailer 发信系统</td>
    </tr>
    <tr align="left" valign="middle" bgcolor="#eeeeee">
      <td bgcolor="#eeeeee" height="92">
      <li>Mail To:
          <input type="text" name="mto" class="tx" style="width:350" value="">
      </li>
      <li>Mail Cc:
          <input type="text" name="mcc" class="tx" style="width:350" value="">
      </li>
            <li> Subject:
          <input type="text" name="msubject" class="tx" style="width:350" value="">
      </li>
            <li> Mail Body: <br>&nbsp;&nbsp;
            <textarea  name="mbody" cols="55" rows="10"  wrap="on"></textarea>
         
      </li>
           
        <script language="javascript">
   function setid()
   {
   str='<br>';
   if(!window.form1.upcount.value)
    window.form1.upcount.value=3;
    for(i=1;i<=window.form1.upcount.value;i++)
      str+='附件'+i+':<input type="file" name="userfile[]" style="width:400" class="tx1"><br><br>';
   window.upid.innerHTML=str+'<br>';
   }
   </script>
        <li> 指定附件数量
          <input type="text" name="upcount" class="tx" value="3">
          <input type="button" name="Button" class="bt" onclick="setid();" value="· 设定 ·">
        </li>
        <br>
       
      </td>
    </tr>
    <tr align="center" valign="middle">
      <td align="left" id="upid" height="122"> 附件1:
        <input type="file" name="userfile[]" style="width:400" class="tx1" value="">
      </td>
    </tr>
    <tr align="center" valign="middle" bgcolor="#eeeeee">
      <td bgcolor="#eeeeee" height="24">
        <input type="submit" name="Submit" value="· 提交 ·" class="bt">
        <input type="reset" name="Submit2" value="· 重执 ·" class="bt">
      </td>
    </tr>
  </table>
</form>
</body>
</html>
<script language="javascript">

setid();
</script>

以下为服务端内容,即ups.php

=========================================================================================

<?php


//设置发件者初始信息从这开始

require("class.phpmailer.php");


$ mail = new PHPMailer();
$ uniq_id = md5(uniqid(time()));
$ mail->SetLanguage('cn', "language/");

$ mail->IsSMTP();                                      // set mailer to use SMTP
$ mail->Host = "smtp.mail.sogou.com;smtp.163.com";  // specify main and backup server
$ mail->SMTPAuth = true;     // turn on SMTP authentication
$ mail->Username = "temp@ezpower.cn";  // SMTP username
$ mail->Password = "temp"; // SMTP password

$ mail->From = "temp@ezpower.cn";
$ mail->FromName = "EzPower";

//设置发件者初始信息到这结束;


// 在 4.1.0 以前的 PHP 中,需要用 $ HTTP_POST_FILES 代替 $ _FILES。
// 在 4.0.3 以前的 PHP 中,需要用 copy() 和 is_uploaded_file() 来代替 move_uploaded_file()。
$ mto=$ _REQUEST["mto"];
$ mailcc=$ _REQUEST["mcc"];
$ mailsubject=$ _REQUEST["msubject"];
$ mailbody=$ _REQUEST["mbody"];
//echo ($ mailto.$ mailcc.$ mailbody.$ mailsubject);
$ mto=explode(";",$ mto);
foreach($ mto as $ key=>$ mto1){
if (strpos($ mto1, '@') !== false) {
 $ mail->AddAddress($ mto[$ key]);}
};
$ mcc=explode(";",$ mcc);
foreach($ mcc as $ key=>$ mcc1){
if (strpos($ mcc1, '@') !== false) {
 $ mail->AddCC($ mcc[$ key]);}
};


$ uploaddir = 'ups/'.date("omdH").'/';//建立ups/当前时间/的文件夹来存放文件
if (@opendir($ uploaddir)){
 echo($ uploaddir.' already exist!</br>');
 @closedir ($ uploaddir);
 }
 else
{echo 'Now mkdir will make "'.$ uploaddir.'"for you </br>';
@mkdir($ uploaddir);
}


$ uploadfile = $ uploaddir. $ _FILES['userfile']['name'];
//echo($ uploadfile);
//foreach ($ _FILES['userfile']['name']) {

print "<pre>\n";


//返回一个URL连接
function url($ a,$ b)
     { 
    $ url= "<a href='".$ a.$ b."' title='".$ b."' >".$ b."</a></br>";
       return "$ url\n"; 
 
          }
 
foreach ($ _FILES["userfile"]["size"] as $ key => $ size) {
    if ($ size != 0 ) {
        $ tmp_name = $ _FILES["userfile"]["tmp_name"][$ key];
        $ name = $ _FILES["userfile"]["name"][$ key];
        move_uploaded_file($ tmp_name, $ uploaddir . $ name);
        //$ url= "<a href='".$ uploaddir.$ name."' title='".$ name."' >".$ name."</a></br>";
       
       
        //把附件内容添加到邮件中开始:
        $ mail->AddAttachment($ uploaddir . $ name);         // add attachments
        //把附件内容添加到邮件中结束:
     
        print url($ uploaddir,$ name);
        }
  } 
 
 
  //从下面开始进行mail发送动作;

//$ mail->AddAddress($ mailto);

//$ mail->AddAddress("temp@ezpower.cn");                  // name is optional
$ mail->AddReplyTo("temp@ezpower.cn", "Reply to me");
//$ mail->AddEmbeddedImage("ups/drunk.gif",$ uniq_id,"drunk.gif","base64","image/gif");
//访问嵌入式图片方法为:<img src=cid:$ uniq_id>

$ mail->WordWrap = 50;                                 // set word wrap to 50 characters
//$ mail->AddAttachment("ups/2007050623/Uploader V6 文件上传中文版.rar");         // add attachments
//$ mail->AddAttachment("phpmailer-1.73.zip");    // optional name
$ mail->IsHTML(true);                                  // set email format to HTML

$ mail->Subject = $ mailsubject;
$ mail->Body    = $ mailbody;
$ mail->AltBody = "This is the body in plain text for non-HTML mail clients";
set_time_limit(0);//设定最长执行时间,单位为秒,以免因为发送文件而出现TIME_OUT执行错误
if(!$ mail->Send())
{
   echo "Message could not be sent. <p>";
   echo "Mailer Error: " . $ mail->ErrorInfo;
   exit;
}

echo "Message has been sent";

//邮件发送结束

 


/*

foreach ($ _FILES["userfile"]["error"] as $ key => $ error) {
    if ($ error == UPLOAD_ERR_OK ) {
        $ tmp_name = $ _FILES["userfile"]["tmp_name"][$ key];
        $ name = $ _FILES["userfile"]["name"][$ key];
        move_uploaded_file($ tmp_name, $ uploaddir . $ name);
        //$ url= "<a href='".$ uploaddir.$ name."' title='".$ name."' >".$ name."</a></br>";
        print url($ uploaddir,$ name);
      
             
        }
     
 
  
  }
  */
    print "\nFile is valid, and was successfully uploaded.  You can access to your files by clicking below links:\n";
  //  echo()
        print_r($ _FILES);
       
        if ($ handle = opendir($ uploaddir)) {
    while (false !== ($ file = readdir($ handle))) {
        if ($ file != "." && $ file != "..") {
            echo url($ uploaddir,$ file)." \n";
        }
    }
    closedir($ handle);
}


print "</pre>";

 


?>

===================================================================================

标签集:TAGS:phpmailer 文件上传 发送附件
回复Comments() 点击Count()
喜欢就顶一下

回复Comments

{commentauthor}
{commentauthor}
{commenttime}
{commentnum}
{commentcontent}
作者:
{commentrecontent}