Re: Sending MIME emails with base64 encoded attachments
| From: | Analysis and Solutions | Date: | Wed, 06 Feb 2002 19:21:25 +0000 |
| Subject: | Re: Sending MIME emails with base64 encoded attachments | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-83742@lists.php.net to get a copy of this message | ||
Hi Petr:
> I using PHP4 on SuSe Linux.
> I'm sending an MIME email (I generatate all the headers and so on) with
> attachments.
Your coding worked fine for me.
> When I then send it, the mail arrives, but the attached file (e.g. *.zip) is
> corrupted.
> In original there are #0 (char. ascii 0), in attachment they come as "\0"
> (#92#48).
Two things I can think of...
First: things might run differnetly on SuSE Linux. Perhaps there are
configuration settings on your server that are throwing things off. Check
your OS documentation and newsgroup archives. I guess, also check the PHP
mailing list archives too:
http://groups.google.com/groups?hl=en&group=php.general.
You might need to
take a look at RFC-2045 Section 6.8 to see why that your system is properly
converting things.
Second: the way you've formatted the MIME headers et al may be causing some
problem. Here's a test script (fix email line wrapping as needed) of mine
that works (on PHP 4.0.7-dev / NT 4 / Apache). Try it on your system and see
what happens.
<?php
# Set accordingly...
$To = 'foo@blue.goo';
$FirstName = 'Joe'; # name of person who wants file
$Subject = 'the big test';
$Email = 'blue@goo.boo'; # from address
$Name = 'Analysis and Solutions'; # your name
$FileName = 'email.attach.test'; # should be a .zip file
# don't include file extension
$InDir = '.'; # directory where file can be found
# Used for purposes of this example,
# but set another way in my real script...
$Invoice = date('YmdHis');
# Leave this.
$Encoding = 'base64';
# Make sure variables are empty to start with...
$Body = '';
$AdditionalHeaders = '';
# Here we go...
if ( !$InHandle = @fopen("$InDir/$FileName.zip","r") ) {
echo "<h3>Couldn't open input file.</h3>";
exit();
}
$Boundary = "=====================_$Invoice" . "==_";
$AdditionalHeaders .= "Mime-Version: 1.0\n";
$AdditionalHeaders .= "Content-Type: multipart/mixed;
boundary=\"$Boundary\"\n";
$Body = "This is a multi-part message in MIME format.\n\n";
$Body .= "--$Boundary\n";
$Body .= "Content-Type: text/plain; charset=us-ascii\n";
$Body .= "Content-Transfer-Encoding: 7bit\n\n";
$Body .= "Greetings $FirstName:\n\nThanks for your order.\n\n";
$Body .= "The copy of $Subject you ordered is attached below.\n\n";
$Body .= "\n\nSincerely,\n\n$Name\n\n";
$Body .= "--$Boundary\n";
$Body .= "Content-Type: application/x-zip-compressed;
name=\"$FileName.zip\"\n";
$Body .= "Content-Transfer-Encoding: $Encoding\n";
$Body .= "Content-Disposition: attachment; filename=\"$FileName.zip\"\n\n";
$Body .= chunk_split( base64_encode( fread( $InHandle,
filesize("$InDir/$FileName.zip") ) ) );
$Body .= "\n--$Boundary--";
mail("$To", "$Subject", "$Body", "From: $Email\nX-Loop:
$Email\n$AdditionalHeaders\n");
# Show results in browser...
echo '<p>The following body was sent:</p>';
echo "\n<pre>$Body</pre>";
?>
Enjoy,
--Dan
--
PHP scripts that make your job easier
http://www.analysisandsolutions.com/code/
SQL Solution | Layout Solution | Form Solution
T H E A N A L Y S I S A N D S O L U T I O N S C O M P A N Y
4015 7 Ave, Brooklyn NY 11232 v: 718-854-0335 f: 718-854-0409