So I am working on a project where I have a file that has been stored in a database with the fields of name, data, and mime-type. It is not under my control why the file is stored like that, it just is.
Next, I have a system where there is an approve or deny setting. If approved I have to take that file from the database and attach it to a mailer and send it. I can get the mailer to send but it never has the attachment with it. So I need to figure out what I am doing wrong and how to get the file to attach.
For simplicity I am using PHPMailer, and my database is SQL Server 2012. My code is also very lengthy, so I placed only the needed part below. if there is more information you require, please ask! Thank you for all help in advance.
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();
}