Hey, I am trying to send email with an attached file via PHP script. I tried two methods (below) but neither one seems to work correctly. In both cases, the file contents are being included in the body and not as an attachment. Can anyone help me with this or see the problem? Thanks!
Method 1:
random_hash = md5(date('r', time()));
$headers = 'MIME-Version: 1.0' . "\\r\
"; $headers .= 'From: Backlinkbuild <support@backlinkbuild.com>' . "\\r\
"; $headers .= 'Cc: Backlinkbuild <orders@backlinkbuild.com>' . "\\r\
"; $headers .= "\\r\
Content-Type: multipart/mixed; boundary=\\"PHP-mixed-".$random_hash."\\"";
$attachment =
chunk_split(base64_encode(file_get_contents($_FILES['File1']['tmp_name'])));
$fileatt_name = $_FILES['File1']['name'];// Name of file
$output = "
–PHP-mixed-$random_hash;
Content-Type: multipart/mixed; boundary='PHP-alt-$random_hash'
–PHP-alt-$random_hash
Content-Type: multipart/mixed; charset='iso-8859-1'
Content-Transfer-Encoding: 7bit
Hello,
This is testing email with attachment. Please find attachment.
–PHP-alt-$random_hash
Content-Type: multipart/mixed; charset='iso-8859-1'
Content-Transfer-Encoding: 7bit
Hello,
This is testing email with attachment. Please find attachment.
–PHP-alt-$random_hash–
–PHP-mixed-$random_hash
Content-Type: application/vnd.ms-excel; name=$fileatt_name
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
–PHP-mixed-$random_hash–";
mail($order['Email'],$subj,$output,$headers);
Method 2:
$attachment =
chunk_split(base64_encode(file_get_contents($_FILES['File1']['tmp_name'])));
$fileatt = $_FILES['File1']['tmp_name']; // Location of file on server
$fileatt_type = $_FILES['File1']['type'];// Type of file being sent
$fileatt_name = $_FILES['File1']['name'];// Name of file
$fileatt_path = $_FILES['File1']['tmp_name'];
// Read the file to be attached ('rb' = read binary)
$tmp_name1 = $_FILES['file1']['tmp_name'];
$file = $_FILES['File1']['tmp_name'];
$data = file_get_contents($_FILES['File1']['tmp_name']);
// Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= 'From: Backlinkbuild <support@backlinkbuild.com>' . "\\r\
";
$headers .= 'Cc: Backlinkbuild <orders@backlinkbuild.com>' . "\\r\
";
// Add the headers for a file attachment
$headers .= "\
MIME-Version: 1.0\
" .
"Content-Type: multipart/mixed;\
" .
" boundary=\\"{$mime_boundary}\\"";
// Add a multipart boundary above the plain message
$message = "This is a multi-part message in MIME format.\
\
" .
"--{$mime_boundary}\
" .
"Content-Type: text/html; charset=\\"iso-8859-1\\"\
" .
"Content-Transfer-Encoding: 7bit\
\
" .
$message . "\
\
";
$message .="This is an automated email sent from a php script\
";
// Base64 encode the file data
$data = chunk_split(base64_encode($data));
// Add file attachment to the message
$message .= "--{$mime_boundary}\
" .
"Content-Type: {$fileatt_type};\
" .
" name=\\"{$fileatt_name}\\"\
" .
"Content-Disposition: attachment;\
" .
" filename=\\"{$fileatt_name}\\"\
" .
"Content-Transfer-Encoding: base64\
\
" .
$data . "\
\
" .
"--{$mime_boundary}--\
";
mail($order['Email'], $mail_subject, $message, $headers);