从html表单上传Excel文件,从excel文件中读取regno和电子邮件信息。然后每个用户都会收到一封电子邮件和一条消息.regno和email都插入到数据库中。 gmail用于发送电子邮件$ frmid是来自电子邮件地址,$ password是密码。 这是代码。
<?php
require_once("function/PHPExcel/Classes/PHPExcel/IOFactory.php");
require_once("function/Mail/class.phpmailer.php");
require_once("function/connection.php");
if($_SERVER['REQUEST_METHOD']=='POST' && is_uploaded_file($_FILES['uploaded_file']['tmp_name'])){
$msg=$_POST['msg'];
$arr=array();
$frmid=htmlspecialchars(trim($_POST['frm_id']),ENT_QUOTES,'UTF-8');
$password=htmlspecialchars(trim($_POST['password']),ENT_QUOTES,'UTF-8');
$subject=htmlspecialchars(trim($_POST['subject']),ENT_QUOTES,'UTF-8');
$name=$_FILES['uploaded_file']['tmp_name'];
try {
$inputFileType = PHPExcel_IOFactory::identify($name);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($name);
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
for ($row = 1; $row <= $highestRow; $row++){
$email = $sheet->getCell('B'.$row )->getValue();
$regno = strtoupper($sheet->getCell('A'.$row )->getValue());
$arr[]=array($regno,$email);
$message=$msg;
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->IsHTML(true);
$mail->Username = $frmid;
$mail->Password = $password;
$mail->SetFrom($frmid);
$mail->Subject = $subject;
$mail->MsgHTML($msg);
$mail->AddAddress($email);
if(!$mail->Send()){
die("Failed to send to Email Id ".$email."\n<br/>");
}
}
} catch(Exception $e) {
die('Server Error');
}
$db=connection();
$query="INSERT INTO table(regno,email) VALUES ";
$query=buildinsert($query,$highestRow);
$stmt=$db->prepare($query);
$result=$stmt->execute($arr);
if($stmt->rowCount()==$highestRow){
redirect("page.php?result=1");
}else{
redirect("page.php?result=0");
}
}
?>
使用的功能是
<?php
function buildinsert($query,$rows){
$placeholder=array();
for($i=0;$i<$rows;$i++){
$placeholder[]="(?,?)";
}
return($query.implode(",",$placeholder));
}
function connection(){
try{
return (new PDO("mysql:host=localhost;dbname=dbms,","root","passwd"));
}catch(Exception $e){
die( "An error occured".$e->getMessage());
}
}
function redirect($link){
header("Location:".$link);
}
?>
问题是我在操作完成后没有被重定向。邮件发送成功,内容也被添加,但页面没有变化。我得到的页面和以前一样
答案 0 :(得分:0)
发现问题.phpmailer,phpexcel很好,问题在于connection.php
添加buildinsert函数后,我留下了换行符,由于
之间的换行符空间,页面没有重定向谢谢Jon!