This question already has an answer here:
- Send attachments with PHP Mail()? 13 answers
- Sending multiple attachment in an email using PHP 2 answers
- Anatomy of an email message 2 answers
Making a PHP script that also has a text file included as attachment. It works perfectly when I send to a GMAIL-address, but when I send to an address located at another web hoster, I can see that the file is attached, but when I open it, the file is empty (no content)... What can cause this?? This is the code I use:
$filename = basename($dirfilename);
$body = "File is included in mail";
$path = "downloads/";
$file = $path.$filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($file);
$eol = PHP_EOL;
// Headers
$header = "From: ".$from_name." <".$from_mail.">".$eol;
$header .= "Reply-To: ".$replyto.$eol;
$header .= "MIME-Version: 1.0
";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"";
// Message
$message = "--".$uid.$eol;
$message .= "Content-Type: text/html; charset=UTF-8".$eol;
$message .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$message .= $body.$eol;
$message .= "--".$uid.$eol;
$message .= "Content-Type: text/plain; name=\"".$filename."\"".$eol;
$message .= "Content-Transfer-Encoding: base64".$eol;
$message .= "Content-Disposition: attachment;
filename*=utf-8''" . rawurlencode($filename).$eol;
$message .= $content.$eol;
$message .= "--".$uid."--";
if (mail($mail_to, $subject, $message, $header))
{
echo "File sent";
} else {
echo "Fail";
}
</div>