将数据从csv文件上载到数据库时,向用户发送邮件

时间:2017-03-16 04:52:54

标签: php csv mysqli

我有一个import.php文件,它将csv文件中的数据导入数据库。 import.php的代码如下所示

<?php
include('db.php');
include_once "auth.php";
include 'common.php';
if(($_SESSION['SESS_CATEGORY']=='requestor') AND ($_SESSION['SESS_LOGIN_DEPT'] != 'TXN')){
    header("location: menu.php");
        exit();
}
echo '<ul style = "margin-top: -9px;"">
 <li style="margin-left: 550px; list-style: none;" align ="center"><a href="menu.php"><font size = "2" face="verdana">GO BACK TO MENU</font></a></li></ul>';
if(!$link){
    die( "Sorry! There seems to be a problem connecting to our database.");
}
function get_file_extension($file_name) {
    return end(explode('.',$file_name));
}
function errors($error){
    if (!empty($error))
    {
            $i = 0;
            while ($i < count($error)){
            $showError= '<div class="msg-error">'.$error[$i].'</div>';
            $i ++;}
            return $showError;
    }
}
$to ="*TXN_PlanningTNC@gmail.com";
$subject = "TND uploaded";
$message = "New TND(s) uploaded,please check TXN portal for details.";
$user_name = $_SESSION['SESS_FIRST_NAME'];
if (isset($_POST['upfile'])) {
    if(get_file_extension($_FILES["uploaded"]["name"])!= 'csv')
    {
        echo '<p align="center">Only CSV files accepted!';
        return false;
    }
    $tot = 0;
    $handle = fopen($_FILES["uploaded"]["tmp_name"], "r");
    while (($data = fgetcsv($handle, 10000, ",")) !== FALSE) {
        for ($c=0; $c < 1; $c++) {
            if($data[0] !=''){
                mysqli_query($link,"INSERT INTO new_tnds(`TND_ID`,`Site_name_S1`,`Site_name_S2`,`Call_sign_S1`,`Idea_ID_S1`,`Call_sign_S2`,`Idea_ID_S2`,`Site_to_integrate`,`True_azimuth_S1`,`True_azimuth_S2`,`Path_length_(km)`,`TR_Antenna_diameter_(m)_S1`,`TR_Antenna_diameter_(m)_S2`,`TR_Antenna_height_(m)_S1`,`TR_Antenna_height_(m)_S2`,`#1_Channel_ID_S1`,`#1_Channel_ID_S2`,`#1_Design_frequency_S1`,`#1_Design_frequency_S2`,`Polarization`,`Radio_model_S1`,`Radio_model_S2`,`TX_power_(dBm)_S1`,`TX_power_(dBm)_S2`,`Receive_signal_(dBm)_S1`,`Receive_signal_(dBm)_S2`,`cluster`)VALUES('".mysqli_real_escape_string($link,$data[0])."','".mysqli_real_escape_string($link,$data[1])."','".mysqli_real_escape_string($link,$data[2])."','".mysqli_real_escape_string($link,$data[3])."','".mysqli_real_escape_string($link,$data[4])."','".mysqli_real_escape_string($link,$data[5])."','".mysqli_real_escape_string($link,$data[6])."','".mysqli_real_escape_string($link,$data[7])."','".mysqli_real_escape_string($link,$data[8])."','".mysqli_real_escape_string($link,$data[9])."','".mysqli_real_escape_string($link,$data[10])."','".mysqli_real_escape_string($link,$data[11])."','".mysqli_real_escape_string($link,$data[12])."','".mysqli_real_escape_string($link,$data[13])."','".mysqli_real_escape_string($link,$data[14])."','".mysqli_real_escape_string($link,$data[15])."','".mysqli_real_escape_string($link,$data[16])."','".mysqli_real_escape_string($link,$data[17])."','".mysqli_real_escape_string($link,$data[18])."','".mysqli_real_escape_string($link,$data[19])."','".mysqli_real_escape_string($link,$data[20])."','".mysqli_real_escape_string($link,$data[21])."','".mysqli_real_escape_string($link,$data[22])."','".mysqli_real_escape_string($link,$data[23])."','".mysqli_real_escape_string($link,$data[24])."','".mysqli_real_escape_string($link,$data[25])."','".mysqli_real_escape_string($link,$data[26])."')")or die(mysqli_connect_errno());
                    $sql ="UPDATE new_tnds SET  TND_uploadedtime = now() where TND_ID = '$data[0]'";
                    $sql1 ="UPDATE new_tnds SET TND_uploaded_user_name = '$user_name' where TND_ID = '$data[0]'";
                    if (mysqli_query($link,$sql))
                    {
                        if (mysqli_query($link,$sql1))
                        {
                            echo '';
                        }
                    }
            }
            else{
                    echo '<p align="center">check the data in the csv file!';
                }

            $tot++;
        }
    }
    fclose($handle);
    $content= '<div class="success" id="message"> CSV File Imported, $tot records added </div>';
}
if(TND_uploadedtime = )
echo "<h3 align = 'center'>Import CSV Data</h3>
<form enctype='multipart/form-data' action='' method='post' align = 'center'>
    File:<input name='uploaded' type='file' maxlength='20' accept='.csv'/><input type='submit' name='upfile' value='Upload File'>
    <p>Don't upload more than 500 TNDs at a time.</p>
</form>";
?>

我希望在数据上传时触发邮件。所以我有邮件功能,将邮件发送给相关人员。我已将时间作为导入文件本身的一个列上传。任何人都可以建议我如何捕获上传的事件并触发该邮件功能。

<?php
function mail_attachment($to,$subject,$message, $files)
{

//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: txnportal.tnc@idea.adityabirla.com\r\nReply-To: txnportal.tnc@idea.adityabirla.com";

if($files[0]!="NULL")
{
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks

$attachment = chunk_split(base64_encode(file_get_contents($files[0])));
$name = basename($files[0]);


//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit


--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<?php echo $message; ?>

--PHP-alt-<?php echo $random_hash; ?>--

--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: application/zip; name=<?php echo $name; ?> 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--


<?php
$message.= ob_get_clean();

}
else
{

  $headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
}
//copy current buffer contents into $message variable and delete current output buffer

//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"

return $mail_sent;

}
?>

0 个答案:

没有答案