• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 828
  • Last Modified:

How to attach and send an XLS file using PHP?

Experts,

I have a site where Admins upload XLS files, then send a message out to users of the site notifying them that a new file is available for use.

Can I modify this script to now attach the file that has already been uploaded to an email and send it?  I have seen a lot of solutions out there but, I have been asked to customize this script to specifically and only allow for the attaching of XLS files.

I'm positive that I need to modify the $headers of the email but, I haven't been able to find a straight forward way to do so.

MIME Type = application/vnd.ms-excel

Can anyone help?
$Site = $_POST['Site'];
	$Attachment = $_POST['Attachment'];
	$Custom_Message = $_POST['Message'];
	
	//END MAIL VARIABLES
	$EMailAddress_From = "user@mysite.org";
	$EMail_Title = "New Quality Report Available";
	
	$message='
&#9;&#9;<html>
&#9;&#9;<head>
&#9;&#9;</head>
&#9;&#9;<body>
&#9;&#9;<h2><i>A New Quality Report Has Been Loaded</i></h2>
&#9;&#9;<hr />
&#9;&#9;<br />
&#9;&#9;A new Quality report has been loaded to the site for '.$Site.'. This report is now ready for your review.
&#9;&#9;<br /><br />
&#9;&#9;<b>'.$Custom_Message.'</b>
&#9;&#9;<br /><br />
&#9;&#9;Please click on the following link to be taken to the Permanente Medicine site.
&#9;&#9;<br /><br />
&#9;&#9;<a href="http://obscured-by-Moderator.org/dept/permmed/" target="_blank">CLICK HERE</a>
&#9;&#9;</body>
&#9;&#9;</html>';
&#9;&#9;
&#9;&#9;$message = stripslashes($message);
&#9;&#9;
&#9;&#9;if($Attachment != ""){
&#9;&#9;&#9;$get_file = "SELECT * FileName FROM quality_reports WHERE Site='$Site' AND ReportType='$FileType' LIMIT 1";
&#9;&#9;&#9;$get_data = mysql_query($get_file) or die ("Could not run query: " . $get_file . "<br />\n" . mysql_error () );
&#9;&#9;&#9;while($attached_file = mysql_fetch_array($get_data)){
&#9;&#9;&#9;&#9;//THIS IS THE FILE TO BE ATTACHED
&#9;&#9;&#9;&#9;$sending_file = $attached_file['FileName'];
&#9;&#9;&#9;&#9;//THIS IS THE ABSOLUTE PATH ON THE SERVER WHERE THE FILE IS LOCATED
&#9;&#9;&#9;&#9;$file_path = $_SERVER['DOCUMENT_ROOT']."/dept/permmed/reports/quality_reports".$sending_file;
&#9;&#9;&#9;&#9;
&#9;&#9;&#9;&#9;//I NEED TO MODIFY THE HEADERS TO WORK WITH THE ATTACHED FILE
&#9;&#9;&#9;&#9;$headers="MIME-Version: 1.0\r\n". 
&#9;&#9;&#9;&#9;"Content-type: text/html; charset=iso-8859-1\r\n". 
&#9;&#9;&#9;&#9;"From: ".$EMailAddress_From."\r\n". 
&#9;&#9;&#9;&#9;"Date: ".date("r")."\r\n";
&#9;&#9;&#9;&#9;
&#9;&#9;&#9;}//END WHILE STATEMENT
&#9;&#9;}
&#9;&#9;
&#9;&#9;if($Attachment == ""){
&#9;&#9;&#9;$headers="MIME-Version: 1.0\r\n". 
&#9;&#9;&#9;"Content-type: text/html; charset=iso-8859-1\r\n". 
&#9;&#9;&#9;"From: ".$EMailAddress_From."\r\n". 
&#9;&#9;&#9;"Date: ".date("r")."\r\n";&#9;
&#9;&#9;}
&#9;&#9;
&#9;&#9;//GET ALL USERS WHOM HAVE ACCESS TO THE QUALITY REPROTS, WHERE SITE='$Site'
&#9;&#9;$sql = "SELECT EMailAddress FROM members WHERE Quality=1 AND Site='$Site'";
&#9;&#9;$data = mysql_query($sql) or die ("Could not run query: " . $sql . "<br />\n" . mysql_error () );
&#9;&#9;
&#9;&#9;//THIS WHILE LOOP GOES THROUGH EACH USER FROM THE SITE SPECIFIED AND SENDS THEM THE MESSAGE ABOVE
&#9;&#9;//while($send_mail = mysql_fetch_array($data)){
&#9;&#9;&#9;//mail(''.$send_mail['EMailAddress'].'',$EMail_Title,$message,$headers);
&#9;&#9;//}//END EMAIL LOOP
&#9;&#9;
&#9;&#9;//THIS WHILE LOOP GOES THROUGH EACH USER FROM THE SITE SPECIFIED AND SENDS THEM THE MESSAGE ABOVE
&#9;&#9;while($send_mail = mysql_fetch_array($data)){
&#9;&#9;mail($EMailAddress_From,$EMail_Title,$message,$headers);
&#9;&#9;}//END EMAIL LOOP

Open in new window

0
evibesmusic
Asked:
evibesmusic
1 Solution
 
Aaron TomoskySD-WAN SimplifiedCommented:
Use phpmailer. Super easy to send attachments. The manual way is a pita.
0
 
mohanupadhyayCommented:
<?php
$Site = $_POST['Site'];
&#9;$Attachment = $_POST['Attachment'];
&#9;$Custom_Message = $_POST['Message'];
&#9;$semi_rand = md5(time());
  &#9;$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
&#9;//END MAIL VARIABLES
&#9;$EMailAddress_From = "user@mysite.org";
&#9;$EMail_Title = "New Quality Report Available";
&#9;
&#9;$message='
&#9;&#9;<html>
&#9;&#9;<head>
&#9;&#9;</head>
&#9;&#9;<body>
&#9;&#9;<h2><i>A New Quality Report Has Been Loaded</i></h2>
&#9;&#9;<hr />
&#9;&#9;<br />
&#9;&#9;A new Quality report has been loaded to the site for '.$Site.'. This report is now ready for your review.
&#9;&#9;<br /><br />
&#9;&#9;<b>'.$Custom_Message.'</b>
&#9;&#9;<br /><br />
&#9;&#9;Please click on the following link to be taken to the Permanente Medicine site.
&#9;&#9;<br /><br />
&#9;&#9;<a href="http://obscured-by-Moderator.org/dept/permmed/" target="_blank">CLICK HERE</a>
&#9;&#9;</body>
&#9;&#9;</html>';
&#9;&#9;
&#9;&#9;$message = stripslashes($message);
&#9;&#9;// Add a multipart boundary above the plain message
  &#9;&#9;$message = "This is a multi-part message in MIME format.\n\n" .
             "--{$mime_boundary}\n" .
             "Content-Type: text/html;\n charset=\"iso-8859-1\"\n" .
             "Content-Transfer-Encoding: 7bit\n\n" .
             $message . "\n\n" .
&#9;&#9;&#9; "--{$mime_boundary}\n";
&#9;&#9;&#9;
&#9;&#9;if($Attachment != ""){
&#9;&#9;&#9;$get_file = "SELECT * FileName FROM quality_reports WHERE Site='$Site' AND ReportType='$FileType' LIMIT 1";
&#9;&#9;&#9;$get_data = mysql_query($get_file) or die ("Could not run query: " . $get_file . "<br />\n" . mysql_error () );
&#9;&#9;&#9;while($attached_file = mysql_fetch_array($get_data)){
&#9;&#9;&#9;&#9;//THIS IS THE FILE TO BE ATTACHED
&#9;&#9;&#9;&#9;$sending_file = $attached_file['FileName'];
&#9;&#9;&#9;&#9;//THIS IS THE ABSOLUTE PATH ON THE SERVER WHERE THE FILE IS LOCATED
&#9;&#9;&#9;&#9;$file_path = $_SERVER['DOCUMENT_ROOT']."/dept/permmed/reports/quality_reports".$sending_file;
&#9;&#9;&#9;&#9;
&#9;&#9;&#9;&#9;//I NEED TO MODIFY THE HEADERS TO WORK WITH THE ATTACHED FILE
&#9;&#9;&#9;&#9;$headers="MIME-Version: 1.0\r\n".
&#9;&#9;&#9;&#9;"Content-type: multipart/mixed; charset=iso-8859-1\r\n". " boundary=\"{$mime_boundary}\"".
&#9;&#9;&#9;&#9;"From: ".$EMailAddress_From."\r\n".
&#9;&#9;&#9;&#9;"Date: ".date("r")."\r\n";
&#9;&#9;&#9;&#9;
&#9;&#9;&#9;&#9;$message .= "Content-Type: application/doc;\n" .
         &#9;     " name=\"{$Attachment}\"\n" .
            &#9;  "Content-Transfer-Encoding: base64\n" .
           &#9;     "Content-Disposition: attachment;\n" .
           &#9;     " filename=\"{$Attachment}\"\n\n" .
            &#9;  $data . "\n" .
            &#9;  "--{$mime_boundary}\n";
&#9;&#9;&#9;&#9; $message .=  "--\n\n";
&#9;&#9;&#9;&#9;
&#9;&#9;&#9;}//END WHILE STATEMENT
&#9;&#9;}
&#9;&#9;
&#9;&#9;if($Attachment == ""){
&#9;&#9;&#9;$headers="MIME-Version: 1.0\r\n".
&#9;&#9;&#9;"Content-type: text/html; charset=iso-8859-1\r\n".
&#9;&#9;&#9;"From: ".$EMailAddress_From."\r\n".
&#9;&#9;&#9;"Date: ".date("r")."\r\n";&#9;
&#9;&#9;}
&#9;&#9;
&#9;&#9;//GET ALL USERS WHOM HAVE ACCESS TO THE QUALITY REPROTS, WHERE SITE='$Site'
&#9;&#9;$sql = "SELECT EMailAddress FROM members WHERE Quality=1 AND Site='$Site'";
&#9;&#9;$data = mysql_query($sql) or die ("Could not run query: " . $sql . "<br />\n" . mysql_error () );
&#9;&#9;
&#9;&#9;//THIS WHILE LOOP GOES THROUGH EACH USER FROM THE SITE SPECIFIED AND SENDS THEM THE MESSAGE ABOVE
&#9;&#9;//while($send_mail = mysql_fetch_array($data)){
&#9;&#9;&#9;//mail(''.$send_mail['EMailAddress'].'',$EMail_Title,$message,$headers);
&#9;&#9;//}//END EMAIL LOOP
&#9;&#9;
&#9;&#9;//THIS WHILE LOOP GOES THROUGH EACH USER FROM THE SITE SPECIFIED AND SENDS THEM THE MESSAGE ABOVE
&#9;&#9;while($send_mail = mysql_fetch_array($data)){
&#9;&#9;mail($EMailAddress_From,$EMail_Title,$message,$headers);
&#9;&#9;}//END EMAIL LOOP
?>
<?php
$Site = $_POST['Site'];
&#9;$Attachment = $_POST['Attachment'];
&#9;$Custom_Message = $_POST['Message'];
&#9;$semi_rand = md5(time());
  &#9;$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
&#9;//END MAIL VARIABLES
&#9;$EMailAddress_From = "user@mysite.org";
&#9;$EMail_Title = "New Quality Report Available";
&#9;
&#9;$message='
&#9;&#9;<html>
&#9;&#9;<head>
&#9;&#9;</head>
&#9;&#9;<body>
&#9;&#9;<h2><i>A New Quality Report Has Been Loaded</i></h2>
&#9;&#9;<hr />
&#9;&#9;<br />
&#9;&#9;A new Quality report has been loaded to the site for '.$Site.'. This report is now ready for your review.
&#9;&#9;<br /><br />
&#9;&#9;<b>'.$Custom_Message.'</b>
&#9;&#9;<br /><br />
&#9;&#9;Please click on the following link to be taken to the Permanente Medicine site.
&#9;&#9;<br /><br />
&#9;&#9;<a href="http://obscured-by-Moderator.org/dept/permmed/" target="_blank">CLICK HERE</a>
&#9;&#9;</body>
&#9;&#9;</html>';
&#9;&#9;
&#9;&#9;$message = stripslashes($message);
&#9;&#9;// Add a multipart boundary above the plain message
  &#9;&#9;$message = "This is a multi-part message in MIME format.\n\n" .
             "--{$mime_boundary}\n" .
             "Content-Type: text/html;\n charset=\"iso-8859-1\"\n" .
             "Content-Transfer-Encoding: 7bit\n\n" .
             $message . "\n\n" .
&#9;&#9;&#9; "--{$mime_boundary}\n";
&#9;&#9;&#9; 
&#9;&#9;if($Attachment != ""){
&#9;&#9;&#9;$get_file = "SELECT * FileName FROM quality_reports WHERE Site='$Site' AND ReportType='$FileType' LIMIT 1";
&#9;&#9;&#9;$get_data = mysql_query($get_file) or die ("Could not run query: " . $get_file . "<br />\n" . mysql_error () );
&#9;&#9;&#9;while($attached_file = mysql_fetch_array($get_data)){
&#9;&#9;&#9;&#9;//THIS IS THE FILE TO BE ATTACHED
&#9;&#9;&#9;&#9;$sending_file = $attached_file['FileName'];
&#9;&#9;&#9;&#9;//THIS IS THE ABSOLUTE PATH ON THE SERVER WHERE THE FILE IS LOCATED
&#9;&#9;&#9;&#9;$file_path = $_SERVER['DOCUMENT_ROOT']."/dept/permmed/reports/quality_reports".$sending_file;
&#9;&#9;&#9;&#9;
&#9;&#9;&#9;&#9;//I NEED TO MODIFY THE HEADERS TO WORK WITH THE ATTACHED FILE
&#9;&#9;&#9;&#9;$headers="MIME-Version: 1.0\r\n". 
&#9;&#9;&#9;&#9;"Content-type: multipart/mixed; charset=iso-8859-1\r\n". " boundary=\"{$mime_boundary}\"".
&#9;&#9;&#9;&#9;"From: ".$EMailAddress_From."\r\n". 
&#9;&#9;&#9;&#9;"Date: ".date("r")."\r\n";
&#9;&#9;&#9;&#9;
&#9;&#9;&#9;&#9;$message .= "Content-Type: application/doc;\n" .
         &#9;     " name=\"{$Attachment}\"\n" .
            &#9;  "Content-Transfer-Encoding: base64\n" .
           &#9;     "Content-Disposition: attachment;\n" .
           &#9;     " filename=\"{$Attachment}\"\n\n" .
            &#9;  $data . "\n" .
            &#9;  "--{$mime_boundary}\n";
&#9;&#9;&#9;&#9; $message .=  "--\n\n";
&#9;&#9;&#9;&#9;
&#9;&#9;&#9;}//END WHILE STATEMENT
&#9;&#9;}
&#9;&#9;
&#9;&#9;if($Attachment == ""){
&#9;&#9;&#9;$headers="MIME-Version: 1.0\r\n". 
&#9;&#9;&#9;"Content-type: text/html; charset=iso-8859-1\r\n". 
&#9;&#9;&#9;"From: ".$EMailAddress_From."\r\n". 
&#9;&#9;&#9;"Date: ".date("r")."\r\n";&#9;
&#9;&#9;}
&#9;&#9;
&#9;&#9;//GET ALL USERS WHOM HAVE ACCESS TO THE QUALITY REPROTS, WHERE SITE='$Site'
&#9;&#9;$sql = "SELECT EMailAddress FROM members WHERE Quality=1 AND Site='$Site'";
&#9;&#9;$data = mysql_query($sql) or die ("Could not run query: " . $sql . "<br />\n" . mysql_error () );
&#9;&#9;
&#9;&#9;//THIS WHILE LOOP GOES THROUGH EACH USER FROM THE SITE SPECIFIED AND SENDS THEM THE MESSAGE ABOVE
&#9;&#9;//while($send_mail = mysql_fetch_array($data)){
&#9;&#9;&#9;//mail(''.$send_mail['EMailAddress'].'',$EMail_Title,$message,$headers);
&#9;&#9;//}//END EMAIL LOOP
&#9;&#9;
&#9;&#9;//THIS WHILE LOOP GOES THROUGH EACH USER FROM THE SITE SPECIFIED AND SENDS THEM THE MESSAGE ABOVE
&#9;&#9;while($send_mail = mysql_fetch_array($data)){
&#9;&#9;mail($EMailAddress_From,$EMail_Title,$message,$headers);
&#9;&#9;}//END EMAIL LOOP
?>

Open in new window

0
 
evibesmusicAuthor Commented:
Perfect...thank you.  Works like a charm.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now