jon23d Posted July 5, 2010 Share Posted July 5, 2010 I have a class that I use for emailing. I've never needed attachments before, so I added the method to send them, but gmail seems to be the only place that I can see them. In Yahoo and mail.com they appear as zero-length files. I sent a test email to an anonymous email service, you can see it at http://mailinator.com/maildir.jsp?email=mail_class_outgoing_test Because peeps are going to wonder: /** * Add an attachment to the email * * @param STRING $path_to_file * @param STRING $attachment_name * * @return VOID */ function addAttachment($path_to_file, $attachment_name = null) { $this->attachments[$path_to_file] = $attachment_name; } This actually sends the email: private function sendMailWithAttachment() { // create the headers $boundary = 'jfd-' . md5(date('r', time())); foreach ($this->recipients as $email => $name) { // format to & from as 'name <email>' or just email $to = ($name != $email && $name) ? "$name <$email>" : $email; $from = ($this->from_address[1]) ? "{$this->from_address[1]} <{$this->from_address[0]}>" : $this->from_address[0]; $header = "From: $from\n" . "X-Mailer: FM5 1.0\n" . "MIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"$boundary\""; ob_start(); ?>This is a multi-part message in MIME format. --<?=$boundary?> Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: 7bit <?=$this->html?> <? $message = ob_get_clean(); foreach ($this->attachments as $system_filename => $save_as_filename) { if (!is_string($system_filename)) { $system_filename = $save_as_filename; $save_as_filename = basename($system_filename); } $filetype = mime_content_type($system_filename); $data = chunk_split(base64_encode(file_get_contents($system_filename)), 64); ob_start(); ?> --<?=$boundary?> Content-Type: <?=$filetype?>; name="<?=$save_as_filename?>" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="<?=$save_as_filename?>" <?=$data?> <? $message .= ob_get_clean(); } // add the last closing boundary $message .= PHP_EOL ."--$boundary--"; // send the message mail($email, $this->subject, $message, $header, "-f{$this->from_address[0]}"); } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.