I have a file that I am creating on the fly like this:
// Create and save the string on the file system
$str = "Business Plan:
Some more text";
$fp = fopen("alex.txt", 'w+');
fwrite($fp, $str);
// email with the attachment
$to = 'alex.genadinik@gmail.com';
$subject = 'Your business plan attached';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with
$headers = "From: BusinessPlanApp@example.com";
//add boundary string and mime type specification
$headers .= "
Content-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('alex.txt')));
$contents = "some contents of the email";
mail($to, $subject, $contents, $headers);
And the file is saving to the file system AND an email is getting sent to me with the right body and subject.
The only thing going wrong is the attachment is unnamed file of zero bytes. Any idea why that might happen? Is it a permissions issue? Or something in my email?
Thanks!
You email code seems to be missing a few things. I began fixing it but it might be better to start from scratch. I'd recommend a library, but if not, the following code should achieve what you want:
// Create and save the string on the file system
$str = "Business Plan:
Some more text";
$fp = fopen("alex.txt", 'w+');
fwrite($fp, $str);
fclose($fp);
// email fields: to, from, subject, and so on
$from = "BusinessPlanApp@example.com";
$to = 'alex.genadinik@gmail.com';
$subject = 'Your business plan attached';
$headers = "From: $from";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "
MIME-Version: 1.0
" . "Content-Type: multipart/mixed;
" . " boundary=\"{$mime_boundary}\"";
// message text
$contents = "This is the email content";
// multipart boundary
$message = "--{$mime_boundary}
" . "Content-Type: text/plain; charset=\"iso-8859-1\"
" .
"Content-Transfer-Encoding: 7bit
" . $contents. "
";
// preparing attachments
$message .= "--{$mime_boundary}
";
$fp = fopen('alex.txt',"rb");
$data = fread($fp,filesize('alex.txt'));
fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"".basename('alex.txt')."\"
" .
"Content-Description: ".basename('alex.txt')."
" .
"Content-Disposition: attachment;
" . " filename=\"".basename('alex.txt')."\"; size=".filesize('alex.txt').";
" .
"Content-Transfer-Encoding: base64
" . $data . "
";
$message .= "--{$mime_boundary}--";
mail($to, $subject, $message, $headers);
You're putting your mime data into $attachment
, but then don't use that variable anywhere, so you're not actually attaching anything.
You'd be better off using a library, such as PHPMailer or Swiftmailer, to do this for you. It's far less hassle and they provide FAR better diagnostics in case of trouble.