kalevra Posted July 28, 2008 Share Posted July 28, 2008 I'm trying to send an small "html" file as an attachment to a program that should take it and do stuff with it. The program needs this to be sent (example): %requestor=Brochu, Elise %affected_user=Brochu, Elise %priority=None %description=New - Remote Access %category=rq.mtl.app.remote access %status=1-Approved %est_time=0 %justification=test remote access %group=MTL Service Desk %backout_plan=MTL0241556-02 %zchg_type=31991365 %string1=[i]"attached file.html"[/i] Now here's the code that I am using: <?php $to = '[email protected]'; //define the subject of the email $subject = 'co'; //create a boundary string. It must be unique //so we use the MD5 algorithm to generate a random hash $random_hash = md5(date('r', time())); //define the headers we want passed. Note that they are separated with \r\n $headers = "From: [email protected]\r\nReply-To: [email protected]"; //add boundary string and mime type specification $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; //read the atachment file contents into a string, //encode it with MIME base64, //and split it into smaller chunks $attachment = chunk_split(base64_encode(file_get_contents($filename))); //define the body of the message. 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 %requestor=daigneault, toby %priority=none %category=rq.gbl.ops.firewall %group=gbl ops security %status=0-Approval in progress %description=Request for a Vulnlerability scan(details in attached file) %justification=Please proceed a.s.a.p. %affected_user=daigneault, toby $string1= --PHP-mixed-<?php echo $random_hash; ?> Content-Type: application/zip; name=<?php echo $filename; ?> 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(); //send the email $mail_sent = @mail( $to, $subject, $message, $headers ); //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" echo $mail_sent ? "Mail sent" : "Mail failed"; The email is received by the program, but it doesn't seem to see the attached file... I think it's because of all the white spaces, and empty line between the '%string1=' (which lets the program know that the following is an attachment) and the actual file sent by the PHP script... Any help please?! Maybe an easier way to send an attachment, the files are about 1.3kb or so... Oh and when sending the email to myself, and not to the program, everything seems perfect. Link to comment https://forums.phpfreaks.com/topic/117037-solved-problem-sending-attachment/ Share on other sites More sharing options...
kalevra Posted July 28, 2008 Author Share Posted July 28, 2008 Ooops, I gave the same name to the file in my last post... But the "right" part is what my script above send me when I send it to myself instead of to the program. I asked someone to send me an email that is sent to the program where the program DOES accept the attachment... It's really similar... but not quite it: Link to comment https://forums.phpfreaks.com/topic/117037-solved-problem-sending-attachment/#findComment-602003 Share on other sites More sharing options...
kalevra Posted July 28, 2008 Author Share Posted July 28, 2008 Problem solved (Don't really understand why,or how?!) so if anyone sees the difference between the earlier code and this... point it out. <?php $to = "[email protected]"; $from = "Do_not_Reply <[email protected]>"; $subject = "co"; $fileatt = $filename; $fileatttype = "text/html"; $fileattname = "vscan_request.html"; $headers = "From: $from"; $file = fopen( $fileatt, 'rb' ); $data = fread( $file, filesize( $fileatt )); fclose( $file ); $semi_rand = md5( time() ); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . '%requestor=daigneault, toby %priority=none %category=rq.gbl.ops.firewall %group=gbl ops security %status=0-Approval in progress %description=Request for a Vulnlerability scan(details in attached file) %justification=Please proceed a.s.a.p. %affected_user=daigneault, toby %string1=' . "\n\n"; $data = chunk_split( base64_encode( $data ) ); $message .= "--{$mime_boundary}\n" . "Content-Type: {$fileatttype};\n" . " name=\"{$fileattname}\"\n" . "Content-Disposition: attachment;\n" . " filename=\"{$fileattname}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; $mail_sent = @mail( $to, $subject, $message, $headers ); //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" echo $mail_sent ? "Mail sent" : "Mail failed"; ?> Link to comment https://forums.phpfreaks.com/topic/117037-solved-problem-sending-attachment/#findComment-602075 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.