16 PHPExcel使用

这是用来读取上传的excel文件的,至于如何生成我还没细细研究,大家可以baidu
下载地址http://phpexcel.codeplex.com/

<?php
		if ($_FILES["uploadFile"]["size"] < 20000000){
	  		if ($_FILES["uploadFile"]["error"] > 0){
	    		echo "Return Code: " . $_FILES["uploadFile"]["error"] . "<br />";
	    	}else{
					$uploadFileTime	=	time();
		      		move_uploaded_file($_FILES["uploadFile"]["tmp_name"],"temp_excel/".$uploadFileTime.".xls");
	    	}
	   }
 
		require_once 'Classes/PHPExcel.php';
		$fileName 	= $uploadFileTime.".xls";
		$path 		= 'temp_excel/';
		$filePath 	= $path . $fileName;
		$PHPExcel 	= new PHPExcel ();
		$PHPReader 	= new PHPExcel_Reader_Excel2007 ();
		PHPExcel_CachedObjectStorageFactory::cache_to_discISAM;  
		if (! $PHPReader->canRead ( $filePath )) {
			$PHPReader = new PHPExcel_Reader_Excel5 ();
			if (! $PHPReader->canRead ( $filePath )) {
				echo 'Read Excel Failed! Please Contact Admin';
			}
		}
		$PHPExcel 		= $PHPReader->load ( $filePath );
		$currentSheet 	= $PHPExcel->getSheet ( 0 );
		// get cols
		$allColumn 		= $currentSheet->getHighestColumn ();
		// get rows
		$allRow 		= $currentSheet->getHighestRow () ;	
		for($currentRow = 1; $currentRow <= $allRow; $currentRow ++) {
			for($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn ++) {
				$address = $currentColumn . $currentRow;
				$destArr[]	=	$currentSheet->getCell ($address)->getValue ();
			}
		}
?>

15 PHPmailer使用

这个例子也是从官方网站上面找来的,大家有空可以看看官网的例子
http://phpmailer.worxware.com/index.php?pg=examples

require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
 
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
 
$mail->IsSMTP(); // telling the class to use SMTP
 
try {
  $mail->Host       = "mail.yourdomain.com"; // SMTP server
  $mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
  $mail->SMTPAuth   = true;                  // enable SMTP authentication
  $mail->Host       = "mail.yourdomain.com"; // sets the SMTP server
  $mail->Port       = 26;                    // set the SMTP port for the GMAIL server
  $mail->Username   = "[email protected]"; // SMTP account username
  $mail->Password   = "yourpassword";        // SMTP account password
  $mail->AddReplyTo('[email protected]', 'First Last');
  $mail->AddAddress('[email protected]', 'John Doe');
  $mail->SetFrom('[email protected]', 'First Last');
  $mail->AddReplyTo('[email protected]', 'First Last');
  $mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
  $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
  $mail->MsgHTML(file_get_contents('contents.html'));
  $mail->AddAttachment('images/phpmailer.gif');      // attachment
  $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
  $mail->Send();
  echo "Message Sent OK<p></p>\n";
} catch (phpmailerException $e) {
  echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
  echo $e->getMessage(); //Boring error messages from anything else!
}

另外最近在公司用mail函数发送邮件时,配置相关参数,不让其乱码.

//php的mail函数发邮件时,配置相关参数,不让其乱码
$headers       = 'MIME-Version: 1.0' . "\r\n";
$headers     .= 'Content-type: text/html; charset=utf-8' . "\r\n";
// Additional headers
//$headers     .= 'To: sms-grp <[email protected]>' . "\r\n";
$headers     .= 'From: sms-grp <[email protected]>' . "\r\n";
$headers     .= 'Reply-To: sms-grp <[email protected]>' . "\r\n"; 
$subject     =     "=?UTF-8?B?".base64_encode('你好')."?=";
$message    =    "你好,<br />今晚有空吗?";

另外一种,改装版

function sendEmail($subject,$content,$toEmailArr,$attachArr='',$isHtml=true,$from='[email protected]',$fromName='testName',$smtpUserName="",$smtpPass="")
{
        //include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
        $mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
        $mail->IsSMTP(); // telling the class to use SMTP
 
        try
        {
            $mail->SMTPDebug  =     2;                             // enables SMTP debug information (for testing)
            $mail->SMTPAuth   =     true;                          // enable SMTP authentication
            $mail->Host       =     "smtp.qq.com";                 // sets the SMTP server
            $mail->Port       =     25;                            // set the SMTP port for the GMAIL server
            $mail->Username   =     $smtpUserName;                 // SMTP account username
            $mail->Password   =     $smtpPass;                    // SMTP account password
            $mail->Subject       =     $subject;
 
            $mail->AddReplyTo    ($from, $fromName);
            $mail->SetFrom        ($from, $fromName);
            if($isHtml==true)
            {
                $mail->MsgHTML($content);    
            }
            else
            {
                $mail->Body  =         $content;
            }
 
            if(!empty($toEmailArr) && is_array($toEmailArr))
            {
                foreach ($toEmailArr as $toEmail)
                {
                    $mail->AddAddress    ($toEmail,'');
                }
            }
            if(!empty($attachArr) && is_array($attachArr))
            {
                foreach ($attachArr as $attach)
                {
                    $mail->AddAttachment($attach);
                }
            }
            $mail->Send();
            return true;
        }
        catch (phpmailerException $e)
        {
            echo $e->errorMessage(); //Pretty error messages from PHPMailer
        }
        catch (Exception $e)
        {
            echo $e->getMessage(); //Boring error messages from anything else!
        }
        return false;
}