snippet library logo
Turnkey Websites from Built2go.com
in category
PHP / Email / Email with an attachment
Snippet details
ID: 493
Viewed: 2183
Added: 2003-02-26
Version: php4.0+
Sorry no demo

User Rated at:
Rate This:
Snippets in this catagory         
Show Printable Version
This fine dandy code will send a file to an email address. This one is for zipped files but I am sure if you get the right mime type you can send any attachment you want. It will also send text in the email.

Can be highly configurable to run with any code you have.


General Details
Snippet uploaded by: snippet
Email : webmaster@snippetlibrary.com
Snippet By: snippet

<!---Head--->
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +------------------------------------------------------------------------+
// | PHP version 4.0+                                                       |
// +------------------------------------------------------------------------+
// | Copyright (c)2002 The Snippet Library                                  |
// +------------------------------------------------------------------------+
// | email_attach.php v1.0 - Send emails with attachments                   |
// +------------------------------------------------------------------------+
// |                                                                        |
// | This program is free software; you can redistribute it and/or modify   |
// | it under the terms of the GNU General Public License as published by   |
// | the Free Software Foundation; either version 2 of the License, or      |
// | (at your option) any later version.                                    |
// |                                                                        |
// | This program is distributed in the hope that it will be useful,        |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of         |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          |
// | GNU General Public License for more details.                           |
// |                                                                        |
// | You should have received a copy of the GNU General Public License      |
// | along with this program; if not, write to the Free Software            |
// | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA |
// +------------------------------------------------------------------------+
// | Authors: Scoutt <webmaster@snippetlibrary.com>                         |
// |                                                                        |
// |                                                                        |
// |                                                                        |
// +------------------------------------------------------------------------+

<!---Body--->


<?php
// get the mime type for the zip attachment
// and set some variables.
$mime_type "application/zip-compressed";
      
// subject of the email
        
$mail_subject "Your Purchase";
        
$message "this is the email text, can be a lot of lines or just one word.n";
        
$path "path/to/your/file.zip";
// create a unique boundry, can basically be anything.
// generally people use time()
        
$mail_boundary md5(uniqid(time()));
      
// create header
        
$mail_headers "From: $fromrn";
        
$mail_headers .= "MIME-Version: 1.0rn";
        
$mail_headers .= "Content-type: multipart/mixed; boundary="$mail_boundary"rnrn";

// must open the file in readonly and binary format
      
$fp fopen($path"rb");
      
$file fread($fpfilesize($path));
// also you have to base64 it so the email can see it.
      
$file chunk_split(base64_encode($file));
// must get the name of the file
      
$filename basename($path);
// sets the email up so we can send the text and the attachment at the same time
      
$new_mail_body .= "--{$mail_boundary}rn";
      
$new_mail_body .= "Content-Type: text/plain; charset="iso-8859-1"rn";
      
$new_mail_body .= "Content-Transfer-Encoding: 7bitrnrn";
      
$new_mail_body .= "{$message}rn";

      
$new_mail_body .= "--{$mail_boundary}rn";
      
$new_mail_body .= "Content-Type: $mime_type; name="{$filename}"rn";
      
$new_mail_body .= "Content-Transfer-Encoding:base64rn ";
      
$new_mail_body .= "Content-Disposition: attachment; filename="{$filename}"rnrn";
      
$new_mail_body .= $file "rn";
      
$new_mail_body .= "--{$mail_boundary}--rn";

// now the good stuff as we actually send the message and the attachment.
        
mail($email,$mail_subject,$new_mail_body,$mail_headers);
}
?>




No Reviews to show


Please completely fill out the form below if you want to review this snippet. All reviews are subject to validation.


Subject:

Reviewed By:

Write a review:





Terms of Conditions
Powered By
Avian Hosting
© 2005 snippetlibrary.com All Rights Reserved.