Languages :: PHP :: Creating multipart emails with PHP |
|||
| By: ma14dmin |
Date: 06/05/2003 00:00:00 |
Points: 125 | Status: Answered Quality : Excellent |
|
Ok I know I'm reinventing the wheel but I'm trying to create a webmail application using PHP. I want it to be able to create emails with attachments, so far I have managed to create the email, use the multipart boundry stuff to create a mail that my email client (outlook;opera etc) recognises as an attachment and send it, so far so good. The problem comes when I try to attach binary files such as jpeg's, I am using this function: function encodedata(){ $rawfilepointer=fopen($this->filename,'r'); //or die("Failed opening file"); if($rawfilepointer==0){ $this->errstr="Failed opening file"; return 0; } $rawfile=fread($rawfilepointer,filesize($this->filename)); fclose($rawfilepointer); return chunk_split(base64_encode($rawfile)); } to base64 encode the data but something isnt working. The email looks like it has an attachment but the client is failing to decode it properly. Any clues on this issue recieved gratefully. Thanks Mal |
|||
| By: VGR | Date: 06/05/2003 16:38:00 | Type : Answer |
|
| no problem see this as a basis : <? // // ----------------------------- EMAILS -------------------------------- // //globales $headers=''; $subject=''; $recipient=''; $body=''; $separateur=''; function CreateEmail($destinataires,$sujet,$contenu) { GLOBAL $headers,$subject,$recipient,$body,$separateur,$SERVER_NAME; // REMISE ` VIDE $headers=''; $subject=''; $recipient=''; $body=''; $separateur='12EDAIN34'; $From="Robot MyCompany. <myemailaddress>"; //siparateur // sujet $subject=$sujet; // headers de base $headers.="MIME-Version: 1.0\r\nFrom: $From\n"; // later : contact@$SERVER_NAME // dicoupage pour destinataires $tableau=array(); $tableau=explode(';',$destinataires); for ($i=0;$i<count($tableau);$i++) { if (substr($tableau[$i],0,3)=='CC:') { $adresses=substr($tableau[$i],3); // fin de la sous-channe // $adresses=substr($adresses,0,strlen($adresses)-2); // on enlhve le 0A 0D final $headers2="cc: $adresses\n"; } else if (substr($tableau[$i],0,4)=='BCC:') { $adresses=substr($tableau[$i],4); // fin de la sous-channe // $adresses=substr($adresses,0,strlen($adresses)-2); // on enlhve le 0A 0D final $headers2.="Bcc: $adresses\n"; } else { // cas par difaut ou To: if (substr($tableau[$i],0,3)=='To:') $adresses=substr($tableau[$i],3); // fin de la sous-channe else $adresses=$tableau[$i]; // $adresses=substr($adresses,0,strlen($adresses)-2); // on enlhve le 0A 0D final $recipient="$adresses"; // pas de \r\n } } // for destinataires rencontris $headers.="Reply-To: $From\r\nX-Mailer: PHP/" . phpversion()."\r\nX-Priority: 3\r\n"; // premier siparateur et body textuel $headers.="Content-Type: multipart/mixed; boundary=\"$separateur\"\r\n"; $headers.="Content-Transfer-Encoding: 7bit\r\n"; $headers.=$headers2; $body.="This part of the E-mail should never be seen. If\nyou are reading this, consider upgrading your e-mail\nclient to a MIME-compatible client.\r\n"; $body.="\r\n--$separateur\r\n"; //entjtes $body.="Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"; $body.="Content-Transfer-Encoding: 7bit\r\n"; //data $body.="\r\n$contenu\r\n"; // blank line required before body part !!! } // CreateEmail Procedure function AddAttachment( $filename ) { GLOBAL $headers,$subject,$recipient,$body,$separateur; // ditermination du type de l'attachement // calcul extension du nom local if (strpos($filename,'.')) $locext=substr($filename,strpos($filename,'.')); // . dedans else $locext=''; $locext=strtolower($locext); switch ($locext) { case '.jpg' : case '.jpe' : case '.jpeg' : $locmimetype='image/jpeg'; $locencode='base64'; break; case '.txt' : $locmimetype='text/plain'; $locencode="7bit"; break; case '.ai' : case '.eps' : case '.ps' : $locmimetype='application/postscript'; $locencode="7bit"; break; case '.rtf' : $locmimetype='application/rtf'; $locencode="7bit"; break; case '.wav' : $locmimetype='audio/x-wav'; $locencode="base64"; break; case '.gif' : $locmimetype='image/gif'; $locencode="base64"; break; case '.tiff' : case '.tif' : $locmimetype='image/tiff'; $locencode="base64"; break; case '.html' : $locmimetype='text/html'; $locencode="7bit"; break; case '.mpeg' : case '.mpg' : case '.mpe' : $locmimetype='video/mpeg'; $locencode="base64"; break; case '.mov' : $locmimetype='video/quicktime'; $locencode="base64"; break; case '.avi' : $locmimetype='video/x-msvideo'; $locencode="base64"; break; case '.doc' : $locmimetype='application/msword'; $locencode="base64"; break; case '.pdf' : $locmimetype='application/pdf'; $locencode="base64"; break; default : $locmimetype='text/plain'; $locencode="7bit"; break; } // case of $locext $locname=basename($filename); $fd = fopen ($filename, "r"); $dataread = fread ($fd, filesize ($filename)); fclose ($fd); //test:$dataread=file($filename); if ($locencode=='base64') $locdata=base64_encode($dataread); else $locdata=$dataread; // siparateur et body $body.="\r\n--$separateur\r\n"; //entjtes $body.="Content-Type: $locmimetype; name=$locname\r\n"; $body.="Content-Transfer-Encoding: $locencode\r\n"; $body.="Content-Description: $locname\r\n"; $body.="Content-Disposition: attachment\r\n"; //data $body.="\r\n$locdata\r\n"; // blank line required before body part !!! } // AddAttachment Procedure function CloseAndSend() { GLOBAL $headers,$subject,$recipient,$body,$separateur; //siparateur final (terminateur) $body.="\r\n--$separateur--\r\n"; mail("$recipient", "$subject", "$body","$headers"); /*pour virifs : echo "recipient ".htmlspecialchars($recipient)."<HR>"; echo "subject ".htmlspecialchars($subject)."<HR>"; echo "body ".htmlspecialchars($body)."<HR>"; echo "headers ".htmlspecialchars($headers)."<HR>"; */ } // CloseAndSend Procedure ?> //VGR28082002 REM attention! Procidure spicifique aux commandes, et vgraux@easynet.be pour l'instant function SendEmail($email,$titre,$corps,$lien,$attach = '') { $delim='\r\n'; CreateEmail("$email","$titre", "$corps".$delim.$delim."Merci de votre confiance,".$delim.$delim."Signature."); if ($attach<>'') AddAttachment($attach); // full file path CloseAndSend(); } // SendEmail procedure ?> this is for one attachment, but you will easily understand that looping on AddAttachment($filepathes) will do the trick. |
|||
| By: blackelvis | Date: 07/05/2003 20:06:00 | Type : Comment |
|
| check this one out! <A HREF="http://www.phpguru.org/downloads/html.mime.mail/htmlMimeMail-2.5.1.tar.gz">http://www.phpguru.org/downloads/html.mime.mail/htmlMimeMail-2.5.1.tar.gz</a> |
|||
| By: ma14dmin | Date: 07/05/2003 20:08:00 | Type : Comment |
|
| Thanks for your answers, Its going to take me a couple of days to get round to looking at this but don't panic I wont have forgotten you all. |
|||
| By: ma14dmin | Date: 03/06/2003 12:16:00 | Type : Comment |
|
| Apologies for the lack of feedback, the functions do work perfectly well but I was trying to find out why my versions of the same were not working correctly. Since your code is commented in french, not my first language I didnt find your code to be particually helpful for debuging however I am grateful for your answer. |
|||
|
Do register to be able to answer |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








