Subham Kotnala
Subham Kotnala

Reputation: 29

Sending email with an attachment from the form not working

I have been working on a website where i want to get the input type "file" from the user, and email the same to the admin using php.

My code is as follows:

html:

<form class="pure-form"  enctype="multipart/form-data" action="resumeUpload.php" method="POST">
                                            <label for="resume_attachment">Upload your resume</label>
                                            <input type="file" class="pure-input-rounded" name="resume_attachment">
                                            <button type="submit" name="resume_send" class="mt-20 mb-20" value="1">Submit</button>
                                        </form>

php:

if($_POST['resume_send'] && isset($_FILES['resume_attachment'])) 
{ 

    //$from_email      = '[email protected]'; //from mail, sender email addrress 
    $recipient_email = '[email protected]'; //recipient email addrress 

    //Load POST data from HTML form 
    //$sender_name = $_POST["sender_name"] //sender name 
    //$reply_to_email = $_POST["sender_email"] //sender email, it will be used in "reply-to" header 
    //$subject     = 'New Resume' //subject for the email 
    //$message     = $_POST["message"] //body of the email 


    /*Always remember to validate the form fields like this 
    if(strlen($sender_name)<1) 
    { 
        die('Name is too short or empty!'); 
    } 
    */

    //Get uploaded file data using $_FILES array 
    $tmp_name = $_FILES['resume_file']['tmp_name']; // get the temporary file name of the file on the server 
    $name    = $_FILES['resume_file']['name']; // get the name of the file 
    $size    = $_FILES['resume_file']['size']; // get size of the file for size validation 
    $type    = $_FILES['resume_file']['type']; // get type of the file 
    $error   = $_FILES['resume_file']['error']; // get the error (if any) 

    //validate form field for attaching the file 
    if($file_error > 0) 
    { 
        die('Upload error or No files uploaded'); 
    } 

    //read from the uploaded file & base64_encode content 
    $handle = fopen($tmp_name, "r"); // set the file handle only for reading the file 
    $content = fread($handle, $size); // reading the file 
    fclose($handle);                 // close upon completion 

    $encoded_content = chunk_split(base64_encode($content)); 

    $boundary = md5("random"); // define boundary with a md5 hashed value 

    //header 
    $headers = "MIME-Version: 1.0\r\n"; // Defining the MIME version 
    //$headers .= "From:".$from_email."\r\n"; // Sender Email 
    //$headers .= "Reply-To: ".$reply_to_email."\r\n"; // Email addrress to reach back 
    $headers .= "Content-Type: multipart/mixed;\r\n"; // Defining Content-Type 
    $headers .= "boundary = $boundary\r\n"; //Defining the Boundary 

    //plain text 
    $body = "--$boundary\r\n"; 
    $body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n"; 
    $body .= "Content-Transfer-Encoding: base64\r\n\r\n"; 
    $body .= chunk_split(base64_encode($message)); 

    //attachment 
    $body .= "--$boundary\r\n"; 
    $body .="Content-Type: $file_type; name=".$file_name."\r\n"; 
    $body .="Content-Disposition: attachment; filename=".$file_name."\r\n"; 
    $body .="Content-Transfer-Encoding: base64\r\n"; 
    $body .="X-Attachment-Id: ".rand(1000, 99999)."\r\n\r\n"; 
    $body .= $encoded_content; // Attaching the encoded file with email 

    $sentMailResult = mail($recipient_email, $subject, $body, $headers); 

    if($sentMailResult ) 
    { 
    echo "File Sent Successfully."; 
    unlink($name); // delete the file after attachment sent. 
    } 
    else
    { 
    die("Sorry but the email could not be sent. 
                    Please go back and try again!"); 
    } 
} 
?>

I have used this code but could not accomplish the task.

Can anyone here find the error in my code, and give the correct way of doing it.

Thank you.

Upvotes: 0

Views: 621

Answers (2)

Marten Koetsier
Marten Koetsier

Reputation: 3559

Your problem seems to be in this line:

if($_POST['button'] && isset($_FILES['attachment']))

It has two problems:

First, $_POST['button'] will always yield a false value as it is not set. For this to work, you need the html to change: <button type="submit" class="mt-20 mb-20" name="button" value="1">Submit</button>. That is: add a name and value attribute, and the value should not be something that results in falsey.

What do you intent with this check? If you just want to check that the request came in as POST, just skip this because the presence of $_FILES should be enough. There is no $_GET file upload.

Second, isset($_FILES['attachment']) is always false because in the HTML, this file upload name is resume_file, not attachment.

Only after solving these issues, you could continue with the mail-problems, if they exist.

To be clear: your mail code is never reached.

Upvotes: 2

Victor D.
Victor D.

Reputation: 16

I think you should use the PHPMailer API to send your mails with attachments.

Here is a tip :

  • Ask the user to upload a file with an input type file

  • Upload this file to your server

  • Send the attachment with PHPMailer

    $mail->addAttachment('/var/tmp/file.tar.gz');

https://github.com/PHPMailer/PHPMailer

Upvotes: -1

Related Questions