Hi,
Nodrals advice is probably best on this. This can be a little bit of a can of worms.
you have to deal with mime types, setting content type etc...
A few years back I used the following code which is by no means efficient or neat. but it works...
$emailAddress = $_POST['toAddress'];
$emailSubject = $_POST['toSubject'];
$emailBody = $_POST['toBody'];
$fromAddress = $_SESSION['email'];
$fromName = $_SESSION['firstName'];
$msg = '';
// end of line depends on system.
// only here in case we move servers to another OS
if (strtoupper(substr(PHP_OS,0,3)=='WIN'))
$eol="\r\n";
elseif (strtoupper(substr(PHP_OS,0,3)=='MAC'))
$eol="\r";
else $eol="\n";
# Common Headers
//$headers = 'To: $emailAddress';
$headers = '';
$headers .= 'From: '.$fromName.' <'.$fromAddress.'>'.$eol;
$headers .= 'Reply-To: '.$fromName.' <'.$fromAddress.'>'.$eol;
// these two to set reply address
$headers .= 'Return-Path: '.$fromName.' <'.$fromAddress.'>'.$eol;
//$headers .= "Message-ID: <".$now." TheSystem@".$_SERVER['SERVER_NAME'].">".$eol;
// These two to help avoid spam-filters
//$headers .= "X-Mailer: PHP v".phpversion().$eol;
# Boundry for marking the split & Multitype Headers
$mime_boundary = md5(time());
$headers .= 'MIME-Version: 1.0'.$eol;
$headers .= "Content-Type: multipart/related; ".
"boundary=\"".$mime_boundary."\"".$eol;
# Attachment
$docLocation = '';
$docName = '';
// is there an attachment on the users machine?
if(isset($_FILES['userfile']['tmp_name']))
{
if(is_uploaded_file($_FILES['userfile']['tmp_name']))
{
// data passed in
$fileName = $_FILES['userfile']['name'];
// use relative path OR ELSE big headaches
$filePath = $_FILES['userfile']['tmp_name'];
# File for Attachment
$handle = fopen($filePath, 'rb');
$fileContents = fread($handle, filesize($filePath));
// Encode The Data For Transition using base64_encode();
$fileContents = chunk_split(base64_encode($fileContents));
$fileType = filetype($filePath);
fclose($handle);
$msg = "";
$msg .= "--".$mime_boundary.$eol;
// sometimes i have to send MS Word, use 'msword' instead of 'pdf'
$msg .= "Content-Type: application/msword; ".
"name=\"".$fileName."\"".$eol;
$msg .= "Content-Transfer-Encoding: base64".$eol;
// !! This line needs TWO end of lines !! IMPORTANT !!
$msg .= "Content-Disposition: attachment; ".
"filename=\"".$fileName."\"".$eol.$eol;
$msg .= $fileContents.$eol.$eol;
// ###### move file to "Uploaded Files" folder ######
$uploadDir = $_SERVER['DOCUMENT_ROOT'].
'/Projects/Uploaded/'.$_SESSION['userId'].'/';
$docLocation = $uploadDir.basename($fileName);
// create the users directory if it does not already exist
if(!file_exists($uploadDir))
{
mkdir($uploadDir);
}
// move file
if(!move_uploaded_file($filePath, $docLocation))
{
echo "You're attachment was sent. ".
"However there was a problem moving the attachment.".
"<br>Please contact 'projects@updatetech.ie'.<br>";
}
$docLocation = "http://dav.updatetechnology.ie/Uploaded/".
$_SESSION['userId']."/".basename($fileName);
}
}
// or an attachment on the server
else if(isset($_POST['attachment']))
{
$docLocation = base64_decode($_POST['attachment']);
$docName = base64_decode($_POST['docName']);
# File for Attachment
$handle = fopen($docLocation, 'rb');
$fileContents = fread($handle, filesize($docLocation));
// Encode The Data For Transition using base64_encode();
$fileContents = chunk_split(base64_encode($fileContents));
$fileType = filetype($docLocation);
fclose($handle);
$msg = "";
$msg .= "--".$mime_boundary.$eol;
// sometimes i have to send MS Word, use 'msword' instead of 'pdf'
$msg .= "Content-Type: application/pdf; name=\"".$docName."\"".$eol;
$msg .= "Content-Transfer-Encoding: base64".$eol;
// !! This line needs TWO end of lines !! IMPORTANT !!
$msg .= "Content-Disposition: attachment; ".
"filename=\"".$docName."\"".$eol.$eol;
$msg .= $fileContents.$eol.$eol;
$docLocation = base64_decode($_POST['urlAttachment']);
}
# Setup for text OR html
$msg .= '';
$msg .= "Content-Type: multipart/alternative".$eol;
# Text Version
date_default_timezone_set('GMT');
$msg .= "--".$mime_boundary.$eol;
$msg .= "Content-Type: text/plain; charset=iso-8859-1".$eol;
$msg .= "Content-Transfer-Encoding: 8bit".$eol;
$msg .= "This is a multi-part message in MIME format.".$eol;
$msg .= $emailBody.$eol.$eol.$eol;
$msg .= "Email sent on: ".date("d/m/y", time()).
" at ".date("H:i:s", time()).$eol;
//$msg .= "If you are reading this, please update ".
// "your email-reading-software.".$eol;
//$msg .= "+ + Text Only Email from Genius Jon + +".$eol.$eol;
# HTML Version
$body = '';
$msg .= "--".$mime_boundary.$eol;
$msg .= "Content-Type: text/html; charset=iso-8859-1".$eol;
$msg .= "Content-Transfer-Encoding: 8bit".$eol;
$msg .= $body.$eol.$eol;
# Finished
// finish with two eol's for better security. see Injection.
$msg .= "--".$mime_boundary."--".$eol.$eol;
# SEND THE EMAIL
$success = mail($emailAddress, $emailSubject, $msg, $headers) or
die('<br><h5>Mail not sent. Please Try Again</h5>');
if(!$success)
{
die('<br><h5>Mail not sent. Please Try Again</h5>');
}