0

I have a simple form on a web page. I want the person who fills in the form to be able to upload a file. Then I want the message and attached file to be e-mailed to me. I have been playing with lots of bits of code for days now, but haven't found anything that works for me.

My current code:

<?php

    if ($_SERVER['REQUEST_METHOD'] == 'POST'){

        $flags = 'style="display:none;"';
        $message = $_POST['message'];
      $email="me@me.com";
        $to = "someone@somewhere.com";
        $subject = "test Message with attachment";

        $attachment = chunk_split(base64_encode(file_get_contents($_FILES['file']['uploaded_file'])));
        $filename = $_FILES['file']['uploaded_file'];

        $boundary =md5(date('r', time())); 

$headers  = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=utf-8" . "\r\n";
$headers .= "From: ".$email."\r\n";  
$headers .= "Reply-To: ".$email."\r\n" .

 $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";
 exit();

    } // end of what happens if form submitted
else{
?>
<!-- end of form processing-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test e-mail attachment form</title>
</head>
<body>

<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" <?php echo $flags;?>>
    <input type="hidden" name="status" value="sent">
    <table >
    <tr>
        <td>
            Message:
        </td>
        <td>
             <input type="text" name="message" size="20" required>
        </td>
    </tr>


    <tr>
        <td>
            Attach a file: 
        </td>
        <td>

<input type="file" name="uploaded_file">
                </td>
    </tr>
        <tr>
        <td colspan="2">
            <input type="submit" value="Send message">
        </td>
    </tr>
</table>

</form>

</body>
</html>
<?php
}
?>

The message comes through OK, but there is no attachment. I have tried lots of different methods, including a couple found on here, like [Simple PHP form: Attachment to email (code golf), but cannot get what I want.

I also tried something I found at: [http://form.guide/email-form/php-email-form-attachment.html][1], but that involved installing PEAR. I did try that but then discovered it had overwritten my home page, so I had to uninstall it, as it seems to be too complicated and overkill for what I need.

I feel sure that I must be somewhere near getting this to work, but cannot see what is wrong.

halfer
  • 19,824
  • 17
  • 99
  • 186
Tog Porter
  • 421
  • 1
  • 7
  • 23
  • Did you tried PHPMailer? – Abraham Tugalov Jul 20 '17 at 17:30
  • `file_get_contents($_FILES['file']['uploaded_file']` that for one thing, failed you. Best you turn on error reporting http://php.net/manual/en/function.error-reporting.php and consult the duplicate the question was closed with. – Funk Forty Niner Jul 20 '17 at 17:34
  • The answer at: https://stackoverflow.com/questions/826265/simple-php-form-attachment-to-email-code-golf does work after all. I had a typo in it :-) – Tog Porter Jul 21 '17 at 11:05

1 Answers1

-1

I suggest you to use PHPMailer library.
You may want to use send_file_upload example.
Also, lot of examples is listed here.

So, your code may be rewritten like this:

<?php
if( $_SERVER['REQUEST_METHOD'] == 'POST' )
{
    $flags = 'style="display:none;"';
    $message = $_POST['message'];
    $email="me@me.com";
    $to = "someone@somewhere.com";
    $subject = "test Message with attachment";
    $uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['file']['name']));
    $msg = '';

    if( move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile) )
    {
        // Upload handled successfully
        // Now create a message
        // This should be somewhere in your include_path
        require '../PHPMailerAutoload.php';
        $mail = new PHPMailer;
        $mail->setFrom($email);
        $mail->addAddress($to);
        $mail->Subject = $subject;
        $mail->Body = $message;

        // Attach the uploaded file
        $mail->addAttachment($uploadfile, 'My uploaded file');
        if (!$mail->send()) {
            $msg .= "Mailer Error: " . $mail->ErrorInfo;
        } else {
            $msg .= "Message sent!";
        }
    } else
    {
        $msg .= 'Failed to move file to ' . $uploadfile;
    }

    exit( $msg );
} else { ?>
<!-- end of form processing-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test e-mail attachment form</title>
</head>
<body>

<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" <?php echo $flags;?>>
    <input type="hidden" name="status" value="sent">
    <table >
    <tr>
        <td>
            Message:
        </td>
        <td>
             <input type="text" name="message" size="20" required>
        </td>
    </tr>


    <tr>
        <td>
            Attach a file: 
        </td>
        <td>

<input type="file" name="uploaded_file">
                </td>
    </tr>
        <tr>
        <td colspan="2">
            <input type="submit" value="Send message">
        </td>
    </tr>
</table>

</form>

</body>
</html>
<?php }?>

p.s. Using such libraries will give you lot more flexible and working code.
Just because they form all required headers and provides simple OOP interface.

Abraham Tugalov
  • 1,902
  • 18
  • 25