Jump to content

[SOLVED] Mail() Adding Attachments


SharkBait

Recommended Posts

 

When I am trying to send an attachment with an email it seems to go inline with the email and not attached. What am I doing wrong with my MIME headers?

 

<?php
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_{$semi_rand}x";

$HEADERS = "MIME-Version: 1.0\n 
		Content-Type: multipart/mixed; boundary=\"{$mime_boundary}\"";

$BODY = "This is a multi-part message in MIME Format \n\n
	--{$mime_boundary}\n
	Content-Type: text/plain; charset=\"iso-8859-1\"\n
	Content-Transfer-Encoding: 7bit\n\n
	{$message}\n\n";

$FILE = fopen("log.txt", 'rb');
//$FILETYPE = mime_content_type("log.txt");
$FILETYPE = "text/plain";

$DATA = fread($FILE, filesize("log.txt"));
fclose($FILE);

// Base64 encode the file data
$DATA = chunk_split(base64_encode($DATA));

// Add the attachment to the message
$BODY .= "--{$mime_boundary}\n
	Content-Type: {$FILETYPE}; name=\"log.txt\"\n
	Content-Disposition: attachment; filename=\"log.txt\"\n
	Content-Transfer-Encoding: base64\n\n
	{$DATA}\r\n
	--{$mime_boundary}--\n";

if(!mail('my@blah.com', 'Log File', $BODY, $HEADERS)) {
echo "Error in sending Email";
} else {
echo "Mail sent successfully";
}
?>

Link to comment
Share on other sites

I figured out what the problem was.  The $BODY and $HEADERS had too much white space after each line so the headers were not being set up properly.

 

All fixed now :)

 

<?php
// Generate Boundary String
$semi_rand = md5(date('r'));
$mime_boundary = "x{$semi_rand}x";

$HEADERS = "MIME-Version: 1.0\r\n".
		"Content-Type: multipart/mixed; boundary=\"{$mime_boundary}\"\r\n";

$BODY = "This is a multi-part message in MIME Format \r\n".
	"--{$mime_boundary}\r\n".
	"Content-Type: text/plain; charset=\"iso-8859-1\"\n".
	"Content-Transfer-Encoding: 7bit\r\n".
	"{$message}\n\n";

$FILE = fopen("log.txt", 'rb');
//$FILETYPE = mime_content_type("log.txt");
$FILETYPE = "text/plain";

$DATA = fread($FILE, filesize("log.txt"));
fclose($FILE);

// Base64 encode the file data
$DATA = chunk_split(base64_encode($DATA));

// Add the attachment to the message
$BODY .= "--{$mime_boundary}\r\n".
"Content-Transfer-Encoding: base64\n\n".
"Content-Disposition: attachment; filename=\"log\"\r\n".
"{$DATA}\r\n".
"--{$mime_boundary}--\r\n";


if(!mail('blah@blah.com', 'Log File', $BODY, $HEADERS)) {
echo "Error in sending Email";
} else {
echo "Mail sent successfully";
}
?>

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.