从数据库字符串打开并将文件附加到邮件程序

时间:2015-10-27 15:06:59

标签: php sql-server phpmailer

所以我正在开发一个项目,我有一个文件存储在数据库中,其中包含名称,数据和mime类型的字段。我不能控制为什么文件存储就像那样,它就是这样。

接下来,我有一个系统,其中有一个批准或拒绝设置。如果获得批准,我必须从数据库中取出该文件并将其附加到邮件程序并发送。我可以让邮件发送,但它从来没有附加它。所以我需要弄清楚我做错了什么以及如何让文件附加。

为简单起见,我使用的是PHPMailer,我的数据库是SQL Server 2012.我的代码也非常冗长,所以我只在下面放置了所需的部分。如果您需要更多信息,请询问!感谢您提前帮助。

PHP

try{    
    $mail = $MAIL;
    $decision = $approve == 1 ? "<span style='color:#00dd00;'><b>Approved</b></span>" : "<span style='color:#dd0000;'><b>Denied</b></span>";
    $complete_time = date('m/d/Y');
    $name = explode(" ", LDAPGetName($underwriter, $LDAP));
    $name = $name[0];
    $to = LDAPGetEmail($underwriter, $LDAP);
    $mail->setFrom('no-reply@example.com');
    $mail->addAddress($to);
    if($approve == 1){
        $q = "SELECT file_data, file_name, file_type FROM doc_file WHERE doc__id ='$id'";
        $file_data = "";
        $file_name = "";
        $file_type = "";
        foreach($CONN->query($q) as $row){
            $file_data = $row['file_data'];
            $file_name = $row['file_name'];
            $file_type = $row['file_type'];
        }
        $file = hextobin($file_data);
        $mail->AddAttachment($file);
    }
    $mail->isHTML(true);
    $mail->Subject = "Lease Application Update for Lease #$lease";
    $message = "<html>";
        $message .= "<body>";
            $message .= "<h2>Lease Application Update</h2>";
            $message .= "<hr />";
            $message .= "<p>Hello $name,</p>";
            $message .= "<p>This message is to inform you that there has been a decision made on application for
            lease number $lease. Please see below for details.</p>";
            $message .= "<table rules='all' style='border-color: #666; cellpadding='10'>";
                $message .= "<tr><td>Lease #:</td><td>$lease</td></tr>";
                $message .= "<tr><td>Date:</td><td>$complete_time</td></tr>";
                $message .= "<tr><td>Completed By:</td><td>$USER</td></tr>";
                $message .= "<tr><td>Decision:</td><td>$decision</td></tr>";
                $message .= "<tr><td colspan='2'><center><b>Decision Notes</b></center></td></tr>";
                $message .= "<tr><td colspan='2'>$notes</td></tr>";
            $message .= "</table>";
        $message .= "</body>";
    $message .= "</html>";
    $mail->Body = $message;
    $mail->send();
    $response['errors'] = false;
    $response['success_message'] = "Lease application successfully updated and all necessary parties notified";
}catch(phpmailerException $e){
    $response['errors'] = true;
    $response['error_message'] = "The lease application status has been updated however there was an error when trying to send a message to the saleperson: <br>".$e->getMessage();
}

0 个答案:

没有答案