An Excel file is uploaded from a html form the regno and email information are read from the excel file.Each user is then sent an email along with a message.The regno and email are inserted in the database. gmail is used for sending emails $frmid is the from email address and $password is the password for it. Here is the code.
<?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");
}
}
?>
The functions used are
<?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);
}
?>
The problem is I`m not being redirected after the operation completes.The mails are sent successfully and the content is also added but the page does not change.I get same page as before
Found the Problem.The phpmailer,phpexcel are fine the problem was with connection.php
After I added buildinsert function I left a newline,The page was not redirecting because of the newline space between
Thanks Jon!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.