Problems sending mail attachment in php form

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
nashyboy
Forum Newbie
Posts: 14
Joined: Thu May 02, 2002 8:46 am

Problems sending mail attachment in php form

Post by nashyboy »

Guys i have a script that i have played around with a little - most works fine - (its parses through the values on the form perfect) but i really cant get to grips with why the attachment wont come thorugh.

The mail says it has an attachment.zip but there is nothing inside it.

Can you have a look and let me know if you can see the problem.

Submit Page

Code: Select all

<form action="emailtest.php" method="post" enctype="multipart/form-data" name="logcall"  onSubmit="return checkrequired(this)">

<table width="1000" border="0" align="center" cellpadding="1" cellspacing="0" bgcolor="#0054B4">
  <tr>
    <td><table width="100%" border="0" cellspacing="5" cellpadding="5">
      <tr>
        <td width="85" align="center></td>
        <td width="81%" align="center"><span class="headerTextbig">Log A Helpdesk Call</span></td>
        <td width="85" align="center"></td>
      </tr>
    </table>
      <table width="1000" border="0" cellspacing="5" cellpadding="5">
        <tr>
        <td>
        </td>
      </tr>
  </table>
      <table width="1000" border="0" align="center" cellpadding="5" cellspacing="5" bgcolor="#FFFFFF">
        <tr>
        <td width="227" valign="top" class="formText"><img src="1x1.png" width="100%" height="5" /></td>
        <td width="738" valign="top" class="formText"><img src="1x1.png" width="100%" height="5" /></td>
      </tr>
      <tr>
        <td width="227" align="right" class="formText"><strong>Name</strong></td>
        <td width="738" valign="top" class="formText"><input name="requiredname" type="text" id="name" size="60" maxlength="80" />
          <span class="requiredText">*<span class="main">
          <input name="date" type="hidden" id="date" value="<?php echo date('d/m/Y');?>" />
          <input name="date2" type="hidden" id="date2" value="<?php echo date('Y/m/d');?>" />
          <input name="time" type="hidden" id="time" value="<?php echo date('H:i:s'); ?>" />
          </span></span></td>
      </tr>
            <tr>
        <td width="227" align="right" class="formText"><strong>Site / Location</strong></td>
        <td width="738" valign="top" class="formText"><input name="requiredsite" type="text" id="requiredsite" size="40" maxlength="80" />
          <span class="requiredText">*</span></td>
      </tr>
            <tr>
        <td width="227" align="right" class="formText"><strong>Postcode</strong></td>
        <td width="738" valign="top" class="formText"><input name="requiredpostcode" type="text" id="requiredpostcode" size="8" maxlength="12" />
          <span class="requiredText">*</span></td>
      </tr>
      <tr>
        <td align="right" class="formText"><strong>Best Contact Number</strong></td>
        <td valign="top" class="formText"><input name="requiredphone" type="text" id="requiredphone" size="20" maxlength="20" />
          <span class="requiredText">*</span></td>
      </tr>
      <tr>
        <td align="right" valign="top" class="formText"><strong><img src="1x1.png" width="100%" height="3" />Email Address</strong></td>
        <td valign="top" class="formText"><input name="requiredemail" type="text" id="requiredemail" size="60" maxlength="80" />
          <span class="requiredText">*</span>
<table width="100%" height="30" border="0" cellspacing="0" cellpadding="0">
  <tr>
              <td class="microText">Upon logging a ticket with Selection Services you will receive your Ticket Reference to this Email Address</td>
            </tr>
        </table>
          <img src="1x1.png" width="100%" height="3" /></td>
      </tr>
      <tr>
        <td align="right" valign="top" class="formText">&nbsp;</td>
        <td valign="top" class="requiredText">* required fields</td>
      </tr>
      </table>
	  <table width="100%" border="0" cellspacing="0" cellpadding="0">
	    <tr>
	      <td><img src="1x1blue.png" width="100%" height="1" /></td>
        </tr>
      </table>
<table width="1000" border="0" align="center" cellpadding="5" cellspacing="5" bgcolor="#FFFFFF">
        <tr>
        <td width="458" valign="top" class="formText"><img src="1x1.png" width="100%" height="5" /></td>
        <td width="507" valign="top" class="formText"><img src="1x1.png" width="100%" height="5" /></td>
      </tr>
      <tr>
        <td align="right" class="formText"><strong>Does This Relate To A Previous Ticket? </strong></td>
        <td valign="top" class="formText"><input name="previousticket" type="checkbox" id="previousticket" value="Yes" />          
          Yes          </td>
      </tr>
      <tr>
        <td align="right" valign="top" class="formText"><strong><img src="1x1.png" width="100%" height="3" />If Yes, What Is The Reference Number? </strong></td>
        <td valign="top" class="formText"><input name="callref" type="text" id="callref" size="7" maxlength="10"  onfocus="CheckCheckBox(this.form)"/> 
          <table width="100%" height="30" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td class="microText">If you do not have this don't worry. We can find this for you.</td>
            </tr>
          </table>
          <img src="1x1.png" width="100%" height="3" /></td>
      </tr>
          </table>
<table width="1000" border="0" align="center" cellpadding="5" cellspacing="5" bgcolor="#FFFFFF">
      <tr>
        <td width="227" align="right" valign="top" class="formText"><strong><img src="1x1.png" width="100%" height="20" />Urgency Of The Call<br />
          </strong></td>
        <td width="738" valign="top" class="formText"><table width="100%" height="20" border="0" cellspacing="0" cellpadding="0">
          <tr>
              <td valign="top" class="microText">The speed with which an incident must be resolved.</td>
            </tr>
        </table>
  
          <select name="requiredurgency" class="selectdx" id="urgency">
            <option selected="">Please Select From The List</option>
            <option value="Critical">Critical - Work has stopped, loss of a critical service, loss of access to a user(s) that performs critical tasks</option>
            <option value="High">High - Work severly hindered, problem with a critical system or service, degraded performance on primary system</option>
            <option value="Medium">Medium - Work slowed, problem with of loss of service to a non-critical business function, can continue to work</option>
            <option value="Normal">Normal - Non service affecting work, limited impact which is not critical to the business and a work around available</option>
          </select>
          <span class="requiredText">*</span></td>
      </tr>
      <tr>
        <td align="right" valign="top" class="formText"><strong><img src="1x1.png" width="100%" height="40" />Impact Of The Call</strong><br /></td>
        <td class="formText"><table width="100%" height="40" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td height="20" valign="bottom" class="microText">&nbsp;</td>
          </tr>
          <tr>
            <td height="20" valign="top" class="microText">The degree to which the operation of services are disrupted within the business.</td>
          </tr>
        </table>
          <select name="requiredimpact" class="selectdx" id="impact">
            <option selected="">Please Select From The List</option>
            <option value="Critical">Critical - Affects multiple departments, teams, sites or users and is affecting revenue, clients or customers</option>
            <option value="High">High - Affects an entire department, team or site or a critical business function</option>
            <option value="Medium">Medium - Affects on to several business ciritical / operational users</option>
            <option value="Normal">Normal - Affects one to several non-critical users</option>          
          </select>
          <span class="requiredText">*</span></td>
      </tr>
      <tr>
        <td align="right" valign="top" class="formText"><strong><img src="1x1.png" width="100%" height="20" />Description of Issue</strong></td>
        <td valign="top" class="formText"><img src="1x1.png" width="100%" height="20" />
          <textarea name="requiredissue" cols="100" rows="8" class="selectdx" id="requiredissue"></textarea>
          <span class="requiredText">*</span></td>
      </tr>
            <tr>
        <td align="right" valign="top" class="formText"><strong><img src="1x1.png" width="100%" height="5" />Attachment(s)</strong></td>
        <td valign="top" class="formText"><input type="file" name="attachment" id="attachment" /></td>
      </tr>
      <tr>
        <td align="right" valign="top" class="formText">&nbsp;</td>
        <td valign="top" class="requiredText">* required fields</td>
      </tr>
            <tr>
        <td valign="top"><span class="formText"><img src="1x1.png" width="100%" height="5" /></span></td>
        <td valign="top"><span class="formText"><img src="1x1.png" width="100%" height="5" /></span></td>
      </tr>
      <tr>
        <td valign="top">&nbsp;</td>
        <td valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="18%"><input type="submit" name="button" id="button" value="Submit Issue" /></td>
            <td width="82%"><input type="reset" name="button2" id="button2" value="Reset Form" /></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td valign="top"><span class="formText"><img src="1x1.png" width="100%" height="5" /></span></td>
        <td valign="top"><span class="formText"><img src="1x1.png" width="100%" height="5" /></span></td>
      </tr>
  </table></td>
  </tr>
</table></form>

emailtest.php

Code: Select all

<?php 

//define the receiver of the email 
$to = 'paul@paul.com; 
//define the subject of the email 
$subject = 'Helpdesk Call To Be Logged'; 
//create a boundary string. It must be unique 
//so we use the MD5 algorithm to generate a random hash 
$random_hash = md5(date('r', time())); 
//define the headers we want passed. Note that they are separated with \r\n 
$headers = "From: web@web.co.uk\r\nReply-To: web@web.co.uk"; 
//add boundary string and mime type specification 
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('attachment.zip'))); 
//define the body of the message. 
ob_start(); //Turn on output buffering 
?> 
--PHP-mixed-<?php echo $random_hash; ?>  
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" 

--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

Hello World!!! 
This is simple text email message. 

--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

<?php


echo "<font face=calibri size=3>Please log the call below. <br><br>This information was submitted on ". $_POST['date']. " at ". $_POST['time']. " <br><br><hr noshade size=1 width=100%><br><br>
<strong>Client Name: </strong>Test<br><br><br>
<strong>Name: </strong>". $name. "<br><br>
<strong>Site / Location: </strong>". $_POST['requiredsite']. "<br><br>
<strong>Postcode: </strong>". $_POST['requiredpostcode']. "<br><br>
<strong>Best Contact Number: </strong>". $_POST['requiredphone']. "<br><br>
<strong>Email: </strong>". $_POST['requiredemail']. "<br><br><br>
<strong>Does This Relate To A Previous Ticket?: </strong>". $_POST['previousticket']. "<br><br>
<strong>If Yes, What Is The Reference Number?: </strong><br>". $_POST['callref']. "<br><br><br>
<strong>Remedy Case Type: </strong>Incident<br><br>
<strong>Urgency: </strong>". $_POST['requiredurgency']. "<br><br>
<strong>Impact: </strong>". $_POST['requiredimpact']. "<br><br><br>
<strong>Description of Issue: </strong><br>". $_POST['requiredissue']. "<br><br>";

?>

--PHP-alt-<?php echo $random_hash; ?>-- 

--PHP-mixed-<?php echo $random_hash; ?>  
Content-Type: application/zip; name="attachment.zip" 
Content-Transfer-Encoding: base64  
Content-Disposition: attachment  

<?php echo $attachment; ?> 
--PHP-mixed-<?php echo $random_hash; ?>-- 

<?php 
//copy current buffer contents into $message variable and delete current output buffer 
$message = ob_get_clean(); 
//send the email 
$mail_sent = @mail( $to, $subject, $message, $headers ); 
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed"; 
?>

Any help would be much appreciated guys.

Thanks,

Paul
nashyboy
Forum Newbie
Posts: 14
Joined: Thu May 02, 2002 8:46 am

Re: Problems sending mail attachment in php form

Post by nashyboy »

Guys desperate for help on this....

Thanks,

Paul
User avatar
Technocrat
Forum Contributor
Posts: 127
Joined: Thu Oct 20, 2005 7:01 pm

Re: Problems sending mail attachment in php form

Post by Technocrat »

Might I make a suggestion try using swiftmailer instead. Trust me it will make this process much easier.

That said to me it looks like your code should be working. I would try to var_dump your message code and make sure the code is building as expected
Post Reply