My php file gets passed image/png data and needs to attach it to an e-mail and send said e-mail.
Here is working code from a test file:
<?php
include("include.php");
$subject = "Laptop Collection " . date("Y");
//create a boundary string
$random_hash = md5(date('r', time()));
//define the headers we want passed
$headers[] = "From: Do Not Reply <email@example.com>";
//add boundary string and mime type specification
$headers[] = "Content-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
ob_start();
$image = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAArwAAADICAYAAAAKljK9AAALF0lEQVR4nO3dfYylV10H8G277W5bSympte+vY2k7zJ15zvc8d3o7sb1IAvYVrIxBNJE/uq2gqYIGoolEI6KhkaSQEN/gL4pJtUiVgBHBIEpQwca3NsagxDcSLd12u253d7Zb/5hz6e3S7m53F+/u9vNJftlJ5s6dc5+5m/u7537PORs2AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAwFFhPB5vnJ+fP2VS4/F44+rq6kmrq6snzXpsx6gTJtdvUtPXt13jzUlOm66VlZUzDlST287NzW2an58/JcnJ7e904qwfNADArJwwGo1OTXLmYDA4Z3l5+cK+7y9PclWSm4fD4Ru6rntNrfW2SSV5ba113GqYpNRab+v7/tYk5dtV/x+/40j/rue7n67rRlPXb1xrHZdSbp2+xrXWHyil/PB01VrvSvLW/dXU7W+vtd5WSnldu//vSVL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H1DVheOX3Nzc5uSXDzJD5dSfrzlh387yR8m+cusH9v8jTa7+6lZjxkAXrTJaVa11htrrX8xNZOzazQaXTXr8QEAwCFrK7FfXUp5IFOnpNVanxqNRhfMenwAAHBIxuPxxr7vvy/J7yZZa3t7PlNr3ZPkHntzAgBwzGrN7vXTC1ZKKXtLKQ/0fX/5rMcHAACHLMnJpZRb2kzupNn9TK31lbMeGwAAHJY2s3vn1GlNa0tLSzfNelwAAHDY2qlpt5VSnm6zuo91XXfJrMcFAACHbRJjmNpbd2cpZW7W4wIAgMOW5OQkb5iKMexeWlq6dNbjAgCAw5bktHZq0uSI4EcXFxftrQsAwDHvhMXFxQuS3D3ZX1dmFwCA48WJXdfdmORPp44IltkFAOD4cO2115ZSys6pZneHGAMAAMeF4XB42dTitGdKKR8ej8ebZz0uAAA4bIPB4PRSysOt2d1bSrl91mMCAIAjptb64KTZTfK2WY8HAACOmFLKtVOZ3U/OejwAAHBETWV2dyY5bdbjAQCAI2rS8K6srJwx67EAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADHnS1btnxiy5YtzyillFJKvcTr92fdl/FtsmXLlj86Cp5gSimllFIzrTvuuOPTs+7LAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgOPS/wF0o6u7NRb1FQAAAABJRU5ErkJggg==";
$imgPng = imageCreateFromPng($image);
imageAlphaBlending($imgPng, true);
imageSaveAlpha($imgPng, true);
header("Content-type: image/png");
imagePng($imgPng);
$i = ob_get_contents();
$attachment = chunk_split(base64_encode($i));
// clear the buffer, but don't destroy it
ob_clean();
// Define Body
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
This is the information for laptop problems. Your signature is attached to this e-mail.
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>
<img src="cid:signature.png" />
--PHP-alt-<?php echo $random_hash; ?>--
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/zip; name="signature.png"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--
<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
$mail = new \helpdesk\core\mail("email@example.com", $subject, $message);
$mail->setHeaders($headers);
$mail->send();
unset($mail);
?>
Here is my code from a function in my hd (help desk) object that is being called. It is copied and pasted from test.php. I have even set the the image data to the same string on both and it works great in test.php but fails by sending plaintext of all of the multipart php stuff.
public static function createWorkflowData($signatureData, $signatureImage, $reviewData){
//print_r($signatureData);
// print_r($signatureImage);
//print_r($reviewData);
//exit;
$stages = $_SESSION['student']->workflow->getAllStages();
$updateAssignment = array();
$assignmentID = "";
$workflowID = null;
$mysqli = \helpdesk\db\DB::connect();
$stmt = $mysqli->prepare("INSERT INTO `check_workflow_data`
(`workflowID`, `key`, `section`, `questionDate`, `answer`, `checkIn`, `WO`, `internalWO`)
VALUES (?,?,?,?,?,?,?,?)");
foreach( $stages as $stage){
// Skip main stages
if($stage->getSection() == "main")
continue;
// Create Workflow in DB if it is the 1st one
if($stage->getAssignmentGroup() != $assignmentID){
$assignmentID = $stage->getAssignmentGroup();
$workflowID = \helpdesk\db\hd::createWorkflow($assignmentID, $signatureData);
}
//Create Any WO's
if($stage->getPreparedWO() != null){
$stage->setWOID( \helpdesk\db\hd::createWO( $stage->getPreparedWO() ) );
}
if($stage->getPreparedInternalWO() != null){
$stage->setInternalWOID( \helpdesk\db\hd::createInternalWO( $stage->getPreparedInternalWO() ) );
}
$key = $stage->getKey();
$section = $stage->getSection();
$questionDate = (($stage->getAnswerTime())?$stage->getAnswerTime():time());
$answer = (($stage->getAnswer())?$stage->getAnswer():"Not Answered");
$checkIn = (($stage->getAnswerCheckIn()=="true")?1:0);
$WO = $stage->getWOID();
$internalWO = $stage->getInternalWOID();
$stmt->bind_param("issisiss",$workflowID, $key, $section, $questionDate,$answer,$checkIn,$WO,$internalWO);
$stmt->execute();
if($stmt->error != ""){
printf("Error: %s.\n", $stmt->error);
}
if(($CheckIn = $stage->getAnswerCheckIn()) != null){
if($CheckIn == "true"){
//echo "Will update ".$assignmentID." by setting ".$stage->getSection()." to 1";
$updateAssignment[$assignmentID][$stage->getSection()] = 1;
}else if($CheckIn == "false"){
$updateAssignment[$assignmentID][$stage->getSection()] = 0;
}
}
//Prepare this item for this assignment for turn in
}
foreach($updateAssignment as $assignmentID => $assignmentUpdate){
\helpdesk\db\hd::updateAssignment($assignmentID, $assignmentUpdate);
}
$stmt->close();
$subject = "Laptop Collection " . date("Y");
//create a boundary string
$random_hash = md5(date('r', time()));
//define the headers we want passed
$headers[] = "From: Do Not Reply <email@example.com>";
//add boundary string and mime type specification
$headers[] = "Content-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
ob_start();
$image = $signatureImage;
$imgPng = imageCreateFromPng($image);
imageAlphaBlending($imgPng, true);
imageSaveAlpha($imgPng, true);
header("Content-type: image/png");
imagePng($imgPng);
$i = ob_get_contents();
$attachment = chunk_split(base64_encode($i));
// clear the buffer, but don't destroy it
ob_clean();
// Define Body
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
This is the information for laptop problems. Your signature is attached to this e-mail.
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>
<img src="cid:signature.png" />
--PHP-alt-<?php echo $random_hash; ?>--
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/zip; name="signature.png"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--
<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
$mail = new \helpdesk\core\mail("email@example.com", $subject, $message);
$mail->setHeaders($headers);
$mail->send();
unset($mail);
}
Can someone tell me what I am doing wrong here?