Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to attach and send an XLS file using PHP?

Posted on 2011-02-14
3
Medium Priority
?
817 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month9 days, 22 hours left to enroll

926 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