Unable to read PHP Mail Attachment
Asked Answered
K

1

0

I am new to PHP, I need to create a form such that it asks the user to enter several fields and upload his/her resume. When he/she submits the form, his/her submissions should be email to me with his/her resume as the attachment with the email. I have used PHP for sending the email using php mail function. Everything works fine, except that the file attachment is not able to read. Please see the screenshot attached. https://i.stack.imgur.com/sY8AG.jpg
Also the file uploaded is in odt format. I need users to upload all type of resume formats.

I am posting the Essential Part of the Code. Please correct me if I am wrong

if ($_POST['submit_x']) {

            $cand_name = trim($_POST['cand_name']);
            $appl_email = $_POST['email'];

            $target_dir = "/home/test/public_html/new/job/hr/Resume/";
            $file = $_FILES['my_file']['name']; // Resume-Test.odt
            $path = pathinfo($file);
            $ext = $path['extension']; // odt
            $temp_name = $_FILES['my_file']['tmp_name']; // /tmp/phpqkLeuL
            $path_filename_ext = $target_dir.$file.".".$ext;
            move_uploaded_file($temp_name,$path_filename_ext);


            $mailto = '[email protected]';
            $subject = 'Subject';
            $message = 'My message';

            $content = file_get_contents($path_filename_ext);
            $content = chunk_split(base64_encode($content));

            // a random hash will be necessary to send mixed content
            $separator = md5(time());

            // carriage return type (RFC)
            $eol = "\r\n";

            // main header (multipart mandatory)
            $headers = "From: name <[email protected]>" . $eol;
            $headers .= "MIME-Version: 1.0" . $eol;
            $headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
            $headers .= "Content-Transfer-Encoding: 7bit" . $eol;
            $headers .= "This is a MIME encoded message." . $eol;

            // message
            $body = "--" . $separator . $eol;
            $body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
            $body .= "Content-Transfer-Encoding: 8bit" . $eol;
            $body .= $message . $eol;

            // attachment
            $body .= "--" . $separator . $eol;
            $body .= "Content-Type: application/octet-stream; name=\"" . $file . "\"" . $eol;
            $body .= "Content-Transfer-Encoding: base64" . $eol;
            $body .= "Content-Disposition: attachment" . $eol;
            $body .= $content . $eol;
            $body .= "--" . $separator . "--";

            //SEND Mail
            if (mail($mailto, $subject, $body, $headers)) {
                echo "mail send ... OK"; // or use booleans here
            } else {
                echo "mail send ... ERROR!";
                print_r( error_get_last() );
            }
   } 



   <input type="file" name="my_file" /><br /><br />
Kerge answered 4/5, 2018 at 5:18 Comment(2)
I would recommend you to use some tried and tested mail library (like PHPMailer or SwiftMailer) instead of PHP's low level mail()-function. They are much easier to work with.Timothee
@MagnusEriksson Thanks for your suggestion, but can you please tell me where I am wrong?Kerge
S
0

If you do it with PHP's mail() function,it difficult to find bugs.Download the PHPMailer script from here: http://github.com/PHPMailer/PHPMailer and include the main script file.

<?php
$email = new PHPMailer();
$email->From      = 'From mail id';
$email->FromName  = 'from name';
$email->Subject   = 'Subject of message';
$email->Body      = $bodytext;//body of the subject
$email->AddAddress( 'receiver email id' );

$file_path = 'path of the file you want to attach';

$email->AddAttachment( $file_path , 'filename' );

return $email->Send();
?>
Snide answered 4/5, 2018 at 5:49 Comment(2)
We don't have other access to server for including external scripts. So can you please check code using php mail function?Kerge
Can you please tell me the main script file?Kerge

© 2022 - 2024 — McMap. All rights reserved.