sending mail function attachments with php mail() function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mdmartiny1977
Forum Newbie
Posts: 16
Joined: Fri Jan 14, 2011 12:05 am
Location: Michigan, USA
Contact:

sending mail function attachments with php mail() function

Post by mdmartiny1977 »

Hello Everyone

I have written a simple mail function to be emailed to a certain person on submission. On submission they would also like to have attachments sent to them.

I got the email being sent but I can;t get the attachments to work. I have read several different examples and tutorials and none of them work.

This is the code that I have so far

Code: Select all

<?php

<?php

$project_name = $_POST['project_name'];
$needed = $_POST['date_needed'];
$submitted = $_POST['date_submitted'];
$department = $_POST['department'];
$contact = $_POST['contact_person'];
$extension = $_POST['extension'];
$project_type = $_POST['project_type'];
$published = $_POST['date_last_published'];
$description = $_POST['description'];
$color = $_POST['color'];
$pdf = $_POST['pdf_needed'];
$web = $_POST['web_needed'];
$quanity = $_POST['quanity'];

$email = "mdmartiny@sc4.edu";  
$subject = "SC4 Graphics Design Service Request Form";  

$headers .= "MIME-Version: 1.0\r\n";  
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "boundry=\"(mime_boundry\"";)

$fileatt = $_FILES['fileatt'] ['tmp_name'];
$fileatt_type $_FILES['fileatt'] ['type']
$fileatt_name = $_FILES['fileatt'] ['name']

if (is_uploaded_file($fileatt)) {
	$file = fopen($fileatt, 'rb');
	$data = fread($file,filesize($fileatt));
	fclose($file);
	
	$semi_rand = nd5(time());
	$mime_boundry = "==Multipart_Boundry_x($semi_rand)x";


$message = "<html><body>
<table width=\"100%\" border=\"0\" cellspacing=\"5px\" >
<tr><td></td>
<td>Project name: $project_name</td>
<td></td>
<td>Date needed by: $needed</td>
</tr>
<tr>
<tr>
<td></td>
<td colspan=\"3\" align=\"left\" valign=\"top\"><strong><font size=\"+1\">Submitted to graphic designer</font></strong></td></tr>
<tr><td height=\"25\"></td><td>Date $submitted</td><td>Department $department</td><td></td></tr>
<tr><td height=\"25\"></td><td>Contact Person $contact</td><td>Extension $extension</td><td></tr>
<tr><td height=\"25\"></td><td>Type of project $project_type</td><td colspan=\"2\">Approximate date of last publication $published</td></tr>
<tr><td height=\"25\"></td><td colspan=\"3\">Project description/special instructions</td>
<tr><td></td>><td colspan=\"3\">$description</td></tr>
<tr><td height=\"25\"></td><td>Color $color</td><td>PDF needed $pdf</td><td>Website update needed $web</td></tr>
<tr><td ></td><td>Estimated print quanity $quanity</td><td></td><td></td></tr>
<tr>
    <td colspan=\"4\" align=\"left\" valign=\"top\"><hr height=\"5\"/>
	<strong><font size=\"+1\">Graphics office use only</font></strong></td>
  </tr>
  <tr>
    <td height=\"25\" width=\"2%\">&nbsp;</td>
    <td width=\"34%\">Print Shop&nbsp;&nbsp;&nbsp;&nbsp;Color copier</td>
    <td colspan=\"2\">Print Vendor_______________________________________</td>
  </tr>
  <tr>
    <td height=\"25\">&nbsp;</td>
    <td><strong><font size=\"+1\">Project tracking</font></strong></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td height=\"25\">&nbsp;</td>
    <td colspan=\"3\">Received by graphic designer_______________________ &nbsp;&nbsp;Date _______</td>
  </tr>
  <tr>
    <td height=\"25\">&nbsp;</td>
    <td colspan=\"3\">
	  <table width=\"100%\" height=\"35\">
  <tr>
  <td>Approved by executive director__________ &nbsp;&nbsp;Date_________</td><td><input type=\"checkbox\">
      Revisions needed<br /><input type=\"checkbox\"> 
      Revisions made ______ &nbsp;&nbsp;Date_______</td><tr>
	  </table>
	  </td>
  </tr>
  <tr>
    <td height=\"25\">&nbsp;</td>
    <td colspan=\"3\">Completed and spell checked by graphic designer___________________________&nbsp;&nbsp;Date__________</td>

  </tr>
  <tr>
    <td>&nbsp;</td>
    <td align=\"center\" colspan=\"3\">
    <table cellpadding=\"10px\" cellspacing=\"0\" border=\"1\" width=\"100%\">
    <tr bgcolor=\"#CCCCCC\">
    <td>
	<table>
		<tr>
		<td>
    Proofread by marketing coordinator __________&nbsp;&nbsp; Date__________</td>
	</tr>
	<tr>
	<td>
    Proofread by secretary __________&nbsp;&nbsp; Date__________ 
	</td>
	</tr>
	</table>
    </td>
    <td>
    <input type=\"checkbox\">
Revisions needed&nbsp;&nbsp;
<br>
<input type=\"checkbox\">
Revisions made ____ &nbsp;&nbsp;Date_____
    </td>
    </tr>
    </table></td>
  </tr>
  <tr>
  <td></td>
  <td colspan=\"3\">
  <table width=\"100%\" height=\"75\">
  <tr>
  <td>Proofread by executive director______ Date______ </td><td><input type=\"checkbox\">
      Revisions needed<br />
	  <input type=\"checkbox\"> Revisions made ______ &nbsp;&nbsp;Date_______</td>
  </tr>
  </table>
	  </td>
  </tr>
  <tr>
  <td></td>
  <td colspan=\"3\">
  <table width=\"100%\" height=\"75\">
  <tr>
  <td>
  Approval by requesting department __________ Date_________ <br />
  &nbsp;&nbsp; <strong><font size=\"-1\">(Include all paperwork when returning)</font></strong></td><td><input type=\"checkbox\">
      Revisions needed<br /><input type=\"checkbox\"> 
      Revisions made ______ &nbsp;&nbsp;Date_______</td>
  </tr>
  </table>
  </td>
  </tr>
  <td></td height=\"25\">
  <td colspan=\"3\">Final approval by executive director _________________________________________ Date_________ </td>
  </tr>
  <tr>
  <td height=\"75\"></td>
  <td><input type=\"checkbox\"> Printed ____ Date _____</td>
  <td colspan=\"2\"><input type=\"checkbox\"> PDF created _____ Date _____<br />
<input type=\"checkbox\"> Website updated _____ Date _____</td>
  </tr>
</table>";
			$message .= "</body></html>";
			
	$data = chunk_split(base64_encode($data));
	
	$message .= "--($mime_boundry)\n" .
	"Content-Type: ($fileatt_type);\n" . 
	" name=\"($fileatt_name)\"n" .
	"Content-Disposition: attachment;\n" . 
	" filename=\"(fileatt_name)\"\n" . 
	"Content-Transfer_Encoding: base64\n\n" . 
	$data . "\n\n" .
	"--(mime_boundry)--\n";
}
mail($email, $subject, $message, $headers, "From: $email");  

echo "The email has been sent.";  

?> 
pburgh
Forum Newbie
Posts: 13
Joined: Mon Feb 14, 2011 3:14 pm
Location: Boston

Re: sending mail function attachments with php mail() functi

Post by pburgh »

Have you verified that the upload of the file (prior to attaching) is working correctly? The file has to be available locally before it can be attached to the email.
Post Reply