Email Attachment Php

i have following script to send email with attachment in php. but it is not working. Please review it and tell the errors present in doing coding.

<?php
   // request variables // important
   $from = $_REQUEST["from"];
   $emaila = $_REQUEST["emaila"];
   $filea = $_REQUEST["filea"];
   $to = "[email protected]";
   $candidatename = $_POST['candidate-name'];
    $guardianname = $_POST['guardian-name'];
    $relationwithguardian = $_POST['relation-with-guardian'];
    $candidateemail = $_POST['candidate-email'];
    $guardianemail = $_POST['guardian-email'];
    $dateofbirth = $_POST['birthday'];
    $ageofCandidate = $_POST['Age'];
    $candidatesex = $_POST['sex'];
    $phone = $_POST['phone'];
    $skypeusername = $_POST['skype-username'];
    $whatsapp = $_POST['Whatsapp-username'];
    $permanentaddress = $_POST['permanent-address'];
    $mailingaddress = $_POST['mailing-address'];
    $country = $_POST['country'];
    $timezone = $_POST['timezone_offset'];
    $stateprovince = $_POST['State/Province'];
    $city = $_POST['City'];
    $zipcode = $_POST['zip-code'];
    $moftech = $_POST['Medium-Of-Teaching'];
    $classenroll = $_POST['Class-Enrollment'];
    $paymentmethod = $_POST['Payment-Method'];
    $fileattachment = $_FILES['attachment']['name'];
    $admitdate = $_POST['Admission-Form-Submission-Date'];
    
   if ($filea) {
      function mail_attachment ($email_from , $to, $subject, $message, $attachment){
         $fileatt = $attachment; // Path to the file
         $fileatt_type = "application/octet-stream"; // File Type 
         
         $start = strrpos($attachment, '/') == -1 ? 
            strrpos($attachment, '//') : strrpos($attachment, '/')+1;
				
         $fileatt_name = substr($attachment, $start, 
            strlen($attachment)); // Filename that will be used for the file as the attachment 
         
         $email_from = $from . '$emaila'; // Who the email is from
         $subject = "New Applicant Documents";
         
         $email_subject =  $subject; // The Subject of the email 
         $email_txt = $message; // Message that the email has in it 
         $email_to = $to; // Who the email is to
         
         $headers = "From: . '$email_from;
         $file = fopen($fileatt,'rb'); 
         $data = fread($file,filesize($fileatt)); 
         fclose($file); 
         
         $msg_txt='\n\n You have recieved a new attachment message from $from'  .  '\n\n Candidate Name:  . $candidatename <br/>' . '\n\n Guardian Name:  . $guardianname<br/>' . '\n\n Relation With Candidate: . $relationwithguardian<br/>' . '\n\n Candidate Email:  . $candidateemail <br/>' .
'\n\n Guardian Email:  . $guardianemail<br/>' . '\n\n Date of Birth:  . $dateofbirth <br/>' . '\n\n Candidate Age At the Time of Admisssion:  . $ageofCandidate<br/>' . '\n\n Candidate Sex:  . $candidatesex<br/>' . '\n\n Phone: . $phone <br/>' . '\n\n Skype Username:  . $skypeusername <br/>' . '
'\n\n Whatsapp:  . $whatsapp <br/>' . '\n\n Permanent Address:  . $permanentaddress<br/>' . '\n\n Mailing Address:  . $mailingaddress<br/>' . '\n\n Country:  . $country<br/>' . '\n\n State/Province:  . $stateprovince<br/>' . '\n\n City:  . $city<br/>' . '\n\n Time-Zone:  . $timezone<br/>' . '\n\n Zip Code:  . $zipcode<br/>' . '\n\n Medium Of Teaching:  . $moftech<br/>' . ' 
\n\n Class Inwhich Want to Enroll:  . $classenroll<br/>' . '\n\n Payment Method:  . $paymentmethod<br/>' . '\n\n Date Of Application submission:  . $admitdate <br/>'
    
         $semi_rand = md5(time()); 
         $mime_boundary = '==Multipart_Boundary_x{$semi_rand}x"; 
         $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . "
            boundary=\"{$mime_boundary}\"";
         
         $email_txt = "$msg_txt";
			
         $email_message = "This is a multi-part message in MIME format.\n\n" . 
            "--{$mime_boundary}\n" . "Content-Type:text/html; 
            charset = \"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . 
            $email_txt . "\n\n";
				
         $data = chunk_split(base64_encode($data));
         
         $email_message .= "--{$mime_boundary}\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_boundary}--\n';
         
         $ok = mail($email_to, $email_subject, $email_message, $headers);
         
         if($ok) {
            echo "File Sent Successfully.";
            unlink($attachment); // delete a file after attachment sent.
         }else {
            die("Sorry but the email could not be sent. Please go back and try again!");
         }
      }
      move_uploaded_file($_FILES["filea"]["tmp_name"],
         'temp/'.basename($_FILES['filea']['name']));
			
      mail_attachment("$from", "$to", 
         "$subject", "$message", ("temp/".$_FILES["filea"]["name"]));
   }
?>

Since you didn’t provide information about what ‘it is not working’ means, it’s not directly possible to help. There could be a dozen different things it could have done that all count as not working.

Also, please edit your post and add bbcode [code][/code] tags or markdown so that the code is readable.

Some suggestions -

  1. Detect that a post method form was submitted before referencing any of the form data.
  2. Check that the upload worked before referencing any of the uploaded file information.
  3. Validate all input data before using it, storing validation errors in an array.
  4. Use the phpmailer or swiftmailer class to handle building the email.
  5. Don’t copy variables to other variables without a good reason. This is just a waste of typing.
  6. Be consistent. Don’t use $_REQUEST if you expect the form data to be in $_POST.
  7. After you have validated all the inputs, if there are no validation errors (the array holding the error messages is empty), use the submitted form data.
  8. Dynamically process the form data using a data-driven design/template, rather than to write out bespoke code for every possible value.

Now that I have the formatting showing. Notice anything funny about the highlighting?

I would recommend to use PHPMailer or SwiftMailer libraries for this kind of advanced mails.

https://swiftmailer.symfony.com/

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service