Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to attach and send an XLS file using PHP?

Posted on 2011-02-14
3
Medium Priority
?
814 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

There was an incident about the POP3 issue for the double read receipts and delivery receipts in Exchange 2013.  There was huge research been done and found solution for the duplicate mails. Especially when the user gets  duplicate mails.
Resolve Outlook connectivity issues after moving mailbox to new Exchange 2016 server
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

721 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