Jump to content

mail() attachments on the fly


Muddy_Funster

Recommended Posts

Hi, is there a way to generate attachemtns for php's mail() function on the fly, without first saving a file.  I have an html page stored in a variable and would like to send it as both the body of the mail and as an attachment in it (well actualy I would like to just send it in the body....).  Can I push the variable as an attachement or do I need to generate a temp file from it first?

 

Cheers

Link to comment
https://forums.phpfreaks.com/topic/254900-mail-attachments-on-the-fly/
Share on other sites

OK, I got the attachment working on the fly.  Now however my message boddy has completly vanished from the mail.  It's in the variable when I print_r($mail), which is the return from the following function, everything is in there, looking how I expect it should.  Can anyone see what I've done wrong here?

function buildMessage($fieldArray, $attachment, $bound){

$fname = $fieldArray[0][0].'.xml';
$at_con = chunk_split(base64_encode($attachment));
$mailBody  = "";
$mailBody .= "--".$bound."\r\n";
$mailBody .= "Content-Type: application/xml name =\"$fname\"\r\n";
$mailBody .= "Content-Transfer-Encoding: base64 \r\n";
$mailBody .= "Content-Disposition: attachment; filename=\"$fname\""."\r\n"."\r\n";
$mailBody .= $at_con."\r\n"."\r\n";
$mailBody .= "--".$bound."\r\n"; 
$mailBody .="Content-Type: multipart/alternative \r\n"; 
$mailBody .= "Content-type: text/html; charset=iso-8859-1";
$mailBody .= "Content-Transfer-Encoding: 8bit"."\r\n";  
$mailBody .= "<html><body><br><center><h3>Names Changed to protect the guilty</h3></center><br>";
$mailBody .="<table border='1'><tr><th>Service Provider Name :</th><td>Not Here</td><th>Customer Ref Number:</th><td>{$fieldArray[0][0]}</td></tr>";
$mailBody .="<tr><th>Contact Name :</th><td>{$fieldArray[0][1]}</td><th>Customer :</th><td>{$fieldArray[0][2]}</td></tr>";
$mailBody .="<tr><th>Contact No :</th><td>{$fieldArray[0][3]}</td><th>Target ID :</th><td>{$fieldArray[0][4]}</td></tr>";
$mailBody .= "<tr><th>Query Date :</th><td>{$fieldArray[0][5]}</td><th>Job Date :</th><td>{$fieldArray[0][6]}</td></tr>";
$mailBody .= "<tr><td colspan='4'>QUERY TYPE :</td></tr>";
$mailBody .= "<tr><th>QUERY INFO :</th><td colspan='3'>{$fieldArray[0][7]}</td></tr>";
$mailBody .= "<tr><td colspan='4'>DETAILS OF THE ISSUE :</td></tr>";
$mailBody .= "<tr><td colspan='4'>{$fieldArray[0][8]}</td></tr>";
$mailBody .= "</table><p>Please do not reply to this email unless there is a technical issue.</p></body></html>"."\r\n"."\r\n" ;
$mailBody .= "--".$bound."--"."\r\n"."\r\n";

return $mailBody;

The attachment, from, reply-to and subject are all coming through spot on, but the body of the message is empty.

Where is the code where you are actually mail()ing this information?

 

Furthermore, your headers (Subject, From, Attachment, etc) should not be within the body, they should be in a variable of their own.  For instance, for your current code.

$mailBody  = null;
$mailHeaders = null;
//Headers
$mailHeaders .= "--".$bound."\r\n";
$mailHeaders  .= "Content-Type: application/xml name =\"$fname\"\r\n";
$mailHeaders .= "Content-Transfer-Encoding: base64 \r\n";
$mailHeaders .= "Content-Disposition: attachment; filename=\"$fname\""."\r\n"."\r\n";
$mailHeaders .= $at_con."\r\n"."\r\n";
$mailHeaders .= "--".$bound."\r\n"; 
$mailHeaders .="Content-Type: multipart/alternative \r\n"; 
$mailHeaders .= "Content-type: text/html; charset=iso-8859-1";
$mailHeaders .= "Content-Transfer-Encoding: 8bit"."\r\n";
// Body
$mailBody .= "
Names Changed to protect the guilty
";
$mailBody .="</pre>
<table border="'1'">Service Provider Name :Not HereCustomer Ref Number:{$fieldArray[0][0]}";
$mailBody .="Contact Name :{$fieldArray[0][1]}Customer :{$fieldArray[0][2]}";
$mailBody .="Contact No :{$fieldArray[0][3]}Target ID :{$fieldArray[0][4]}";
$mailBody .= "Query Date :{$fieldArray[0][5]}Job Date :{$fieldArray[0][6]}";
$mailBody .= "QUERY TYPE :";
$mailBody .= "QUERY INFO :{$fieldArray[0][7]}";
$mailBody .= "DETAILS OF THE ISSUE :";
$mailBody .= "{$fieldArray[0][8]}";
$mailBody .= "</table>
<p>Please do not reply to this email unless there is a technical issue.</p>"."\r\n"."\r\n" ;<br>$mailBody .= "--".$bound."--"."\r\n"."\r\n

here's the headers and the actual mail() calll

function postMail($message, $bound){
$to = "[email protected]";
$subject = 'Query Management Sheet';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: multipart/related; boundary="'.$bound.'"'."\r\n";
$headers .= 'From: Company Name' . "\r\n"; 
$headers .= 'Reply-To: [email protected]'. "\r\n";
    
mail($to, $subject, $message, $headers);
}

and here is the function calls, just in case

$bound = md5(time());
$mail = buildMessage($res, $xmlFile, $bound);
print_r($mail);

postMail($mail, $bound);

didn't help. Still nothing in the body of the mail, and the attachment is now remaining encoded even after reciept, where before it was openable (this could be due to the encode not peing parsed if it doesn't work as part of the body though - unsure on that one)

 

is there a reason for nulling the mail variables at the top of your code?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.