note 60940 added to function.mail

From: Date: Fri, 20 Jan 2006 19:25:58 +0000
Subject: note 60940 added to function.mail
Groups: php.notes 
Request: Send a blank email to php-notes+get-102174@lists.php.net to get a copy of this message
# Description: Simple class using php mail function to construct and send mime multipart # emails (i.e. emails with attachments) and support content-id style # embedded images in html messages # # Limitations: Uses the ubiquitously supported 7bit (i.e. no encoding) message encoding where as # qouted-printable is recommended for html messages. Does not ensure that message # line lengths do not exceed the 998 character limit specified by RFC 2822. # # Usage Example: # $mulmail = new multipartmail("krisd@work.net", "destination@anywhere.com", "Some Subject"); # $cid = $mulmail->addattachment("/var/www/html/img/pic.jpg", "image/jpg"); # $mulmail->addmessage( # "<html>\n" . # " <head>\n" . # " </head>\n" . # " <body>\n" . # " This is text before<img src=\"cid:$cid\"> and after\n" . # " </body>\n" . # "</html>\n", "text/html"); # $mulmail->sendmail(); class multipartmail{ var $header; var $parts; var $message; var $subject; var $to_address; var $boundary; function multipartmail($dest, $src, $sub){ $this->to_address = $dest; $this->subject = $sub; $this->parts = array(""); $this->boundary = "------------" . md5(uniqid(time())); $this->header = "From: $src\r\n" . "MIME-Version: 1.0\r\n" . "Content-Type: multipart/related;\n" . " boundary=\"" . $this->boundary . "\"\r\n" . "X-Mailer: PHP/" . phpversion(); } function addmessage($msg = "", $ctype = "text/plain"){ $this->parts[0] = "Content-Type: $ctype; charset=ISO-8859-1\r\n" . "Content-Transfer-Encoding: 7bit\r\n" . "\n" . $msg; //chunk_split($msg, 68, "\n"); } function addattachment($file, $ctype){ $fname = substr(strrchr($file, "/"), 1); $data = file_get_contents($file); $i = count($this->parts); $content_id = "part$i." . sprintf("%09d", crc32($fname)) . strrchr($this->to_address, "@"); $this->parts[$i] = "Content-Type: $ctype; name=\"$fname\"\r\n" . "Content-Transfer-Encoding: base64\r\n" . "Content-ID: <$content_id>\r\n" . "Content-Disposition: inline;\n" . " filename=\"$fname\"\r\n" . "\n" . chunk_split( base64_encode($data), 68, "\n"); return $content_id; } function buildmessage(){ $this->message = "This is a multipart message in mime format.\n"; $cnt = count($this->parts); for($i=0; $i<$cnt; $i++){ $this->message .= "--" . $this->boundary . "\n" . $this->parts[$i]; } } /* to get the message body as a string */ function getmessage(){ $this->buildmessage(); return $this->message; } function sendmail(){ $this->buildmessage(); mail($this->to_address, $this->subject, $this->message, $this->header); } } ?> # inspired by code found here. thanks to all ---- Server IP: 64.246.30.37 Probable Submitter: 208.130.114.157 ---- Manual Page -- http://www.php.net/manual/en/function.mail.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+60940 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+60940&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+60940&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+60940&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+60940&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+60940&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+60940&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+60940&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+60940&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#102174) next »