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!