Solved

How to attach and send an XLS file using PHP?

Posted on 2011-02-14
3
799 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 38

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Resolve Outlook connectivity issues after moving mailbox to new Exchange 2016 server
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

770 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