Solved

How to attach and send an XLS file using PHP?

Posted on 2011-02-14
3
801 Views
Last Modified: 2013-12-13
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
Comment
Question by:evibesmusic
3 Comments
 
LVL 39

Expert Comment

by:Aaron Tomosky
ID: 34890357
Use phpmailer. Super easy to send attachments. The manual way is a pita.
0
 
LVL 3

Accepted Solution

by:
mohanupadhyay earned 500 total points
ID: 34905974
<?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
 

Author Closing Comment

by:evibesmusic
ID: 34912544
Perfect...thank you.  Works like a charm.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello Friends, My friends and relatives always ask me how to delete all the various types of emails at once in our g-mail  or windows live account.  So I researched this topic to find a unique solution to this query.  Here it is for those who do …
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question