2015年11月10日 星期二

PHPMail 寄 html

#!/usr/bin/php

<?php
    $cnt = 0;
    $text;
    function getData() {
        require "/root/php/setEnv.php";

        //EIP Connect
        $con = new mysqli("$hostEIP","$usernameEIP","$passwordEIP","$databaseEIP");
        if ($con->connect_errno)
          {
            die('Could not connect: ' . $con->connect_error());
          }
        mysqli_set_charset($con,"utf8");

        $stmt = $con->prepare("
SELECT account_lastname, vfs_directory, vfs_name,
       concat( 'http://xxx.xxx.xxx.xxx/eipplus/index.php?menuaction=filemanager.uifilemanager.index&path_id=', vfs_file_id ) link
  FROM hplus_vfs, hplus_accounts
 WHERE vfs_directory LIKE '%共用文件區%'
   AND vfs_mime_type LIKE 'application%'
   AND vfs_createdby_id = account_id
   AND vfs_created >= sysdate( ) - 144000
");
        $stmt->bind_result($name,$dir,$file,$link);
        $stmt->execute();

        $GLOBALS['text'] = "EIP 共用文件區新增檔案通知,有需要同仁可上EIP自行下載:<br> <br>\n";
        $GLOBALS['text'] .= " <table style=' border: 1px solid #aaa; border-collapse: collapse; '> \n";
        $GLOBALS['text'] .= " <tr> <th style='border: 1px solid #c3c3c3; background-color: #efefef;'> 上傳人員 </th> <th style='border: 1px solid #c3c3c3; background-color: #efefef;'> 上傳目錄 </th> <th style='border: 1px solid #c3c3c3; background-color: #efefef;'> 檔案名稱 </th> <th style='border: 1px solid #c3c3c3; background-color: #efefef;'> EIP連結 </th> </tr> \n";
        while($stmt->fetch())
        {
          $GLOBALS['text'] .= " <tr> <td style='border: 1px solid #c3c3c3;'> $name </td> <td style='border: 1px solid #c3c3c3;'> $dir </td> <td style='border: 1px solid #c3c3c3;'> $file </td> <td style='border: 1px solid #c3c3c3;'> $link </td> </tr>\n";
          $GLOBALS['cnt'] += 1;
        }
        $GLOBALS['text'] .= " </table> \n";

        #$GLOBALS['text'] = "EIP 共用文件區新增檔案通知,有需要同仁可上EIP自行下載:<br> <br>";
        #while($stmt->fetch()) {
        #   $GLOBALS['text'] .= "上傳人員 : $name<br>上傳目錄 : $dir<br>檔案名稱 : $file<br>EIP連結 : $link<br> <br>";
        #   $GLOBALS['cnt'] += 1;
        #}

        #echo $text;
        #echo $cnt;
        #echo $GLOBALS['cnt'];

        $stmt->close();
        $con->close();
    }

    function sendMail() {
        require_once('/root/php/PHPMailer/class.phpmailer.php');
        require_once('/root/php/PHPMailer/class.smtp.php');
        require "/root/php/setEnv.php";

        $phpmail = new PHPMailer();
        $phpmail->IsSMTP(true);
        #$phpmail->IsHTML(true);
        $phpmail->Host = 'xxx.xxx.xxx.xxx';
        $phpmail->CharSet = "utf-8";
        $phpmail->From = 'mis@xxx.xxx.xxx.xxx';
        $phpmail->FromName = 'EIP 自動寄信通知';
        $phpmail->Subject = 'EIP 共用文件區新增檔案通知';

        $html = $GLOBALS['text'];
        #echo $html;
        #$phpmail->Body = "<h1>Test 1 of PHPMailer html</h1><p>This is a test</p>";
        #$phpmail->Body = "<html><table style='border: 1px solid #ccc;'></table><html>";
        $phpmail->MsgHTML($html);
        #$phpmail->MsgHTML("test");
        #$phpmail->AddAttachment( "/tmp/MailNoPrice.csv" , '有庫存無訂價.csv' );

        #$phpmail->AddAddress( $mailNoPriceList,'商品行銷' );
        #$phpmail->AddAddress( $mailNoPriceListVGM,'副總' );
        #$phpmail->AddAddress( $mailNoPriceListFIN1,'財務主管1' );
        #$phpmail->AddAddress( $mailNoPriceListFIN2,'財務主管2' );
        $phpmail->AddAddress( "xxx@xxx.xxx.xxx.xxx",'全公司' );
        if(! $phpmail->Send())
        {
           echo "通知信件寄出失敗";
           echo "Mailer Error : " . $mail->ErrorInfo;
           exit;
        }
           echo "通知信件已寄出";
    }

    function main() {
        getData();
        if ($GLOBALS['cnt'] > 0) {
            sendMail();
        }
    }

    main();
?>

沒有留言:

張貼留言