I have the following code

How do i first edit this to take errors and also to be able to attach files.

<?php
//Gets these from page before
$mail_ref = $_GET['mail_ref'];
$mail_title = $_GET['mail_title'];
$mail_location = $_GET['mail_location'];

?>

<title>RFC Job Application</title>

<link rel='stylesheet' href='../../rfcStyle.css' type='text/css'>
</head>
<body bgcolor='#ffffff' leftMargin='0' topMargin='6' marginheight='0' marginwidth='0'>
<table cellspacing='0' border='2' cellpadding='0' align='center' width='620' bordercolor='#000080'>
  <tr>
    <td class=bar><table width='97%' align='center' cellspacing='0' cellpadding='0' border='0'>
      <tr>
        <td class='banner' nowrap>RFC Recruitment Group</td>
        <td class='banner' nowrap align='right'>Management & Professional Recruitment</td>
      </tr>
    </table>
    </td>
  </tr>
  <tr>
    <td><img src='../../images/titleRfc.gif' width='185' height='70'></td>
  </tr>
  <tr>
    <td class=title><img src='../../images/imgJobApplication.gif' width='295' height='33'></td>
  </tr>
  <tr>
    <td align='center'>


<!-- START OF FORM -->
	<form ACTION="sendmail.php" METHOD=POST align='center'>
	    <table cellspacing='2' cellpadding='2' border='0' width='100%' bgcolor=#ffffff>
      <tr class=content2>
        <td class='heading'>Ref No.</td> 
        <td class='heading'>Job Title</td> 
        <td class='heading'>Location</td> 
      </tr>
      <tr class=content2>
        <td><input name='mail_ref' value="<? echo $mail_ref ?>" type='text' size='20' style='width: 180px; height: 22px;'></td>
        <td><input name="mail_title" value="<? echo $mail_title ?>" type='text' size='20' style='width: 180px; height: 22px;'></td>
        <td><input name='mail_location' value="<? echo $mail_location ?>" type='text' size='20' style='width: 180px; height: 22px;'></td>
      </tr>
      <tr>
        <td colspan=3 class='title' height=20 valign='center' align='center'>Your Details&nbsp;&nbsp;&nbsp;&nbsp;<font class=content3>(* Required Information)</font></td>
      </tr>
      <tr>
        <td class='heading'><font class='content3'>*</font> Your Name</td>
        <td class='heading'>Email Address </td>
        <td class='heading'>Phone No.</td>
      </tr>
      <tr>
        <td><input name='mail_Name' tabindex='1' type='text' size='20' style='width: 180px; height: 22px;'></td>
        <td><input name='mail_Email' tabindex='2' type='text' size='20' style='width: 180px; height: 22px;'></td>
        <td><input name='mail_Tel' tabindex='3' type='text' size='20' style='width: 180px; height: 22px;'></td>
      </tr>
      <tr>
        <td class='heading'><font class='content3'>*</font> Contact Address</td>
        <td class='heading' colspan='2'>Additional Comments</td>
      </tr>
      <tr>
        <td>
		<textarea name='mail_Contact' tabindex='4' style='width: 180px;' cols='20' rows='4' wrap='physical'></textarea></td>
        <td colspan='2'><textarea name='mail_Comments' tabindex='5' rows='4' style='width: 360px;' cols='40'></textarea></td>
      </tr>
      <tr>
        <td colspan='3' align='center' valign='center' height='50'>&nbsp;
		</td>
      </tr>
      <tr>
        <td colspan='3' align='center' valign='center' height='50'><input name="submit" type='submit' tabindex='6' value=' Send Application '></td>
      </tr>
    </table>
    </form>
    </td>
  </tr>
  <tr>
    <td class=bar height='15' align='center'>
	<a class=headlink href='javascript:window.close();'>Close Window</a></td>
  </tr>
</table>
</body>
</html>
<?


//////////////////////////PAGE 2

//first_mail.php
$mail_to = "bla@bla.com";
//$mail_subject = "Hi there";

$mail_ref = $_POST['mail_ref']; //Subject
$mail_title = $_POST['mail_title']; //Job Title
$mail_location = $_POST['mail_location']; //Location 
$mail_Name = $_POST['mail_Name']; //Name
$mail_Email = $_POST['mail_Email']; //Email address
$mail_Tel = $_POST['mail_Tel'];  //Tel number
$mail_Contact = $_POST['mail_Contact']; //Contact details
$mail_Comments = $_POST['mail_Comments']; //Comments

///MAin Body :::
$mail_body = "Location: $mail_location \n Job Title: $mail_title \nName: $mail_Name \nEmail: $mail_Email \nTel: $mail_Tel \nContact: $mail_Contact \nComments: $mail_Comments \n";
?>

Dani AI

Generated

Quick summary and practical additions referencing and : the form in the first post needs to send files (change the form enctype and add a file input), the server must validate required fields and handle $_FILES, and manual MIME assembly (what described) teaches the protocol but is error-prone in production. Important hardening points follow.

Server-side validation and header-safety (collect errors, stop if any): validate required fields, use PHP's filter_var for email, and strip CR/LF from any values that will end up in mail headers to prevent header injection.

function sanitize_header($s) { return trim(str_replace(array("\r","\n"), '', $s)); }

$errors = array();
$name  = trim($_POST['mail_Name'] ?? '');
$email = trim($_POST['mail_Email'] ?? '');

if ($name === '') $errors[] = 'Name is required';
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) $errors[] = 'Valid email required';

$from = sanitize_header($name) . ' <' . sanitize_header($email) . '>';
if ($errors) { /* return or display $errors and stop sending */ }

File handling checklist and example: require enctype="multipart/form-data" on the form; check $_FILES['cv']['error'] === UPLOAD_ERR_OK; use finfo to verify MIME; limit size; move the upload with move_uploaded_file() into a directory outside the webroot; sanitize filenames.

if (isset($_FILES['cv']) && $_FILES['cv']['error'] === UPLOAD_ERR_OK) {
  $tmp = $_FILES['cv']['tmp_name'];
  $finfo = finfo_open(FILEINFO_MIME_TYPE);
  $mime = finfo_file($finfo, $tmp);
  finfo_close($finfo);

  $allowed = array('application/pdf', 'application/msword',
                   'application/vnd.openxmlformats-officedocument.wordprocessingml.document');

  if ($_FILES['cv']['size'] <= 2*1024*1024 && in_array($mime, $allowed)) {
    $target = __DIR__ . '/uploads/' . basename($_FILES['cv']['name']);
    move_uploaded_file($tmp, $target);
    /* attach $target with a mail library below */
  } else { $errors[] = 'Invalid file'; }
}

Recommended approach and troubleshooting: prefer a maintained mail library (PHPMailer/SwiftMailer) so attachments, boundaries and encodings are handled correctly (for example, $mail->addAttachment($target)). If assembling MIME manually, use CRLF ("\r\n") per RFC, correct boundary placement, and base64 for binary files. Also check file_uploads, upload_max_filesize and post_max_size in php.ini, ensure the uploads directory is writable and outside webroot, sanitize filenames to prevent traversal, and consider virus-scanning uploaded files.

Recommended Answers

All 4 Replies

I am not going to fix your code for you.

If I do .. you will never learn anything form it.

But have a look at this:

How to add multiple attachment to an email:

An email can be split into many parts separated by a boundary followed by a Content-Type and a Content-Disposition.

The boundary is initialized as follows:
<?php
$boundary = '-----=' . md5( uniqid ( rand() ) );
?>

You can attach a Word document if you specify:
<?php
$message .= "Content-Type: application/msword; name=\"my attachment\"\n";
$message .= "Content-Transfer-Encoding: base64\n";
$message .= "Content-Disposition: attachment; filename=\"$theFile\"\n\n";
?>

When adding a file you must open it and read it with fopen and add the content to the message:
<?php
$path = "whatever the path to the file is";
$fp = fopen($path, 'r');
do //we loop until there is no data left
{
       $data = fread($fp, 8192);
       if (strlen($data) == 0) break;
       $content .= $data;
     } while (true);
$content_encode = chunk_split(base64_encode($content));
$message .= $content_encode . "\n";
$message .= "--" . $boundary . "\n";
?>

Add the needed headers and send!
<?php
$headers  = "From: \"Me\"<me@here.com>\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"";
mail('myAddress@hotmail.com', 'Email with attachment from PHP', $message, $headers);
?>

Finally, if you add an image and want it displayed in your email, change the Content-Type from attachment to inline:

<?php
$message .= "Content-Disposition: inline; filename=\"$theFile\"\n\n";
?>

This should give you everything you need to fix your own issue.

Go on you know you want to fix it for me at least show me how to put one if statement in it to check if the fields are filled in properly.

Thank you

Thank you for your help got it done it was not that hard. :D

My Pleasure

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.