Jump to content

Attache Excel file to email


samona

Recommended Posts

hI all,

 

I'm trying to attach an excel file that is currently on the server to an email.  Unfortunately its not working.  The code is below:

 

 

$to = "[email protected]";
$subject = "excel attachment";
$filename = "file.xls";

$file = fopen($filename,'rb');
  $data = fread($file,filesize($fileatt));
  fclose($file);

$headers .= "\nMIME-Version: 1.0\n" .
              "Content-Type:  application/octet-stream;\n"
$message =   "Content-Type: application/ms-excel; charset=\"iso-8859-1\"\n" .
                    "Content-Transfer-Encoding: 7bit\n\n" . $message. "\n\n";

$message .= "Content-Type: {$fileatt_type};\n" .
              " name=\"{$fileatt_name}\"\n" .
              "Content-Disposition: attachment; filename=application/octet-stream;\n" .
               "Content-Transfer-Encoding: Content-Transfer-Encoding: binary\n\n";


$ok = @mail($to, $subject, $message, $headers);
if ($ok) {
  echo "<p>Mail sent successfully!</p>";
} else {
  echo "<p>Mail Failed!</p>";
}

 

 

Let me add that I do get an email, I just don't get the attachment.

Link to comment
https://forums.phpfreaks.com/topic/124648-attache-excel-file-to-email/
Share on other sites

samona,

 

  Here's what I use...

 

  Hope it helps...

 

  I just ran it as a stand-alone script and it worked as I expectd... Change the literals, filenames, and path names to match your environment, and it should work...

 

Scot L. Diddle, Richmond...

 


<?php

function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) { 

		// This bad boy came from : http://www.webcheatsheet.com/PHP/send_email_text_html_attachment.php


	$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);

	$header  = "From: ".$from_name." <".$from_mail.">\r\n";
	$header .= "Reply-To: ".$replyto."\r\n";
	// Allow Attachments
	$header .= "MIME-Version: 1.0\r\n";
	$header .= "Content-Type: multipart/mixed; boundary=".$uid."\r\n\r\n";
	$header .= "This is a multi-part message in MIME format.\r\n \n";


	// Here is the Attachment
	$header .= "--".$uid."\r\n";
	$header .= "Content-Type: application/octet-stream; name=".$filename."\r\n"; // use diff. types here 
	$header .= "Content-Transfer-Encoding: base64\r\n";
	$header .= "Content-Disposition: attachment; filename=".$filename."\r\n\r\n";
	$header .= $content."\r\n\r\n";


	// Here is the body
	$header .= "--".$uid."\r\n";
	$header .= "Content-type:text/html; charset=iso-8859-1\r\n";
	$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
	$header .= $message."\r\n\r\n";

	$header .= "--".$uid."\r\n";

	if (mail($mailto, $subject, "", $header)) {
		return TRUE;
	}
	else {
			return FALSE;
	}


}

// how to use
$userEmailString  = $_SESSION['POST']['userSuppliedEmailAddresses'];
$userEmailStringPieces = array();
$userEmailStringPieces = explode(',', $userEmailString);
$trimCandidateOut = array();

foreach($userEmailStringPieces as $trimCandidateIn) {
	if ($trimCandidateIn != '' && $trimCandidateIn != ' ') {
		$trimCandidateOut[] = trim($trimCandidateIn);
	}
}

$userEmailStringPieces = $trimCandidateOut;

$uniqueEmailAddresses  = array_unique($userEmailStringPieces);
$recipients            = implode(', ', $uniqueEmailAddresses);
$recipients 		   = '[email protected], ' . $recipients;
$controlNumberString  = $_SESSION['POST']['HZ_controlNumbersTextArea'];

$controlNumberStringPieces = explode(' ', $controlNumberString); 
$countOfcontrolNumbers = count($controlNumberStringPieces);

if ($countOfcontrolNumbers > 1) {
	$controlLiteral = 'Controls';
}
else {
	$controlLiteral = 'Control';
}

$controlNumberOut = array();

if (is_array($controlNumberStringPieces)) {

	foreach ($controlNumberStringPieces as $controlNumberStringCandidate) {


		$controlNumberOutCandidate = trim($controlNumberStringCandidate);

		$commaPresent = stristr($controlNumberOutCandidate, ',');

		if (!$commaPresent) {
			$controlNumberOutCandidate .= ', ';
		}
		else {
			$controlNumberOutCandidate .= ' ';
		}

		$controlNumberOut[] = trim($controlNumberOutCandidate); 

	}

}
else {
	$controlNumberOut[] = trim($controlNumberString); 
} // END if (is_array($controlNumberStringPieces)) { 

$controlNumberString = implode(' ', $controlNumberOut);
// Eliminate the trailing comma
$lencontrolNumberString = strlen($controlNumberString);

$last = $lencontrolNumberString - 1;

$controlNumberString = substr($controlNumberString,0,$last);

// $xlsFile = 'Transaction_Count_by_SysID_e6k71ak5p1ll1u3lk0c74c3ub6.xls';

$file      = $xlsFile;

$file      = 'someValidFileName.xls Goes Here';

$file      = 'Transaction_Count_by_SysID_e6k71ak5p1ll1u3lk0c74c3ub6.xls';

$path      = 'someValidFilePath Goes Here';

$path      = "Excel/Data/";

$name      = "From Name Litteral Here";

$from      = "[email protected]"; 

$replyto   = "";

$subject   = "$controlLiteral " . $controlNumberString;

$recipient = $recipients;

$recipient = '[email protected]';

// $message   = $myCapturedOutput; // From Smarty processing...
$message    = '<br /> Hi Mom <br />' ;

$mailRC = mail_attachment($file, $path, $recipient, $from, $name, $replyto, $subject, $message);

if ($mailRC) {  
	echo "The 'Form Name Goes Here' form has been sent to: <br /><br /><ul>$recipient</ul><br /></br /> \n ";  
} 
else {
	echo "The 'Form Name Goes Here'  form has NOT been sent to: <br /><br /><ul>$recipient</ul><br /></br />  "; 

	$error  =  "The 'Form Name Goes Here'  form has NOT been sent to: <br /><br /><ul>$to</ul><br /></br />";  

	$error .=  "        Please report this error to the IS Helpdesk.  Ask for WebForms help for form: \"'Form Name Goes Here' \"<br /><br> \n"; 


	$error .=  "        Report that Email delivery failed! <br /><br>"; 

	echo $error;  
}  



echo "Click <a href='javascript: void(0);' onClick='javascript:/* goBack(); */' title='Click Here To Return to Webforms'>here </a> to return to WebForms...\n "; 


?> 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.