i have function to send mail with attachment to microsoft exchange server. My problem is that when I wanna to add attachment the whole message part is adding my text to attachment source. When I save attachment to body section of mail, my attachment source is wrote down in email body instead of creating an attachment. Belowe is my source.
$eol = "\r\n";
$boundary = md5(time());
$mail = "explame@explame.com";
$headers = "From: no-replay@explame.com".$eol;
$headers .= "MIME-Version: 1.0".$eol; //utworzenie headera wiadomosci
$headers .= "Content-type: multipart/alternative; charset=utf-8".$eol;
$headers .= "Message-ID:< TheSystem@".$_SERVER['SERVER_NAME'].">".$eol;
$headers .= "X-Mailer: PHP v".phpversion().$eol;
$headers .= 'MIME-Version: 1.0'.$eol;
$headers .= "Content-Type: multipart/related; boundary=\"".$boundary."\"".$eol;
$headers .= "--$boundary".$eol;
$headers .= "Content-Type: text/plain; charset=utf-8".$eol;
$headers .= "Content-Transfer-Encoding: 8bit".$eol;
$headers .= "--$boundary--".$eol.$eol;
if ($file != ''){
$handle = fopen($file['tmp_name'], 'rb');
$f_content = fread($handle, $file['size']);
$attachment = chunk_split(base64_encode($f_content));
fclose($handle);
$content .= "--$boundary".$eol;
$content .= "Content-type: ".$file['type'].'; '.'name="'.$file['name'].'"'.$eol;
$content .= 'Content-Disposition: attachment; filename="'.$file['name'].'"'.$eol.$eol;
$content .= "Content-Transfer-Encoding: base64".$eol;
$content .= $attachment.$eol.$eol;
$content .= "--$boundary--".$eol.$eol;
}
mail($mail, 'title', $content, $headers)
i think I tried everything but nothing works for me. 🙁
a really good PHP lib for sending mail (especially for dealing with attachments) is the phpmailer class.
You can find it here: http://code.google.com/a/apache-extras.org/p/phpmailer/
EDIT – the above link is to the old project, it’s now hosted on Github and more regularly maintained: https://github.com/PHPMailer/PHPMailer
And an example of how to use it to send an attachment: