0

I've been trying for a while now to get my head around this and I have no clue. I'm trying to write a simple form to send an email with an uploaded file (which will eventually be expanded into something actually useful) and it just isn't working at all.

The emails are coming through with the appropriate body, but no attachments are being included. I've tried this with the file upload form, with AddAttachments linking to a file on the server and with AddAttachments pointing to an image on imgur and none of them work; the attachment NEVER comes through. I'm at the end of my patience right now, does anyone know what I'm doing wrong or a way to do this without phpmailer?

HTML Form

<form action="xxxx.php" id="upload" method="post" name="upload">
<input id="fileToUpload" name="fileToUpload" type="file" /> 
<input type="submit" />
</form>

PHP code

require("../../../classes/class.phpmailer.php");
$mail = new PHPMailer();

$mail->From     = "xx@xx.co.uk";
$mail->FromName = "Uploader";
$mail->AddAddress("xx@xx.co.uk");

$mail->Subject  = "First PHPMailer Message";
$mail->Body     = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;
$mail->AddAttachment( $_FILES['fileToUpload']['tmp_name'], $_FILES['fileToUpload']['name'] );

if(!$mail->Send()) {
  echo 'Message was not sent.';
  echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
  echo 'Message has been sent.';
AlexRS
  • 31
  • 1
  • 8
  • This might be useful to you : http://stackoverflow.com/questions/4007969/application-x-www-form-urlencoded-or-multipart-form-data. I think you need to use `enctype="multipart/form-data"` on your form. – potame Apr 01 '15 at 13:50

2 Answers2

0

Looking at your form you don't have enctype="multipart/form-data" set in your form tag.

In addition, you need to do a file attachment check to make sure it actually attached before sending the email. For an example,

if (isset($_FILES['uploaded_file']) &&
    $_FILES['fileToUpload']['error'] == UPLOAD_ERR_OK) {
    $mail->AddAttachment($_FILES['fileToUpload']['tmp_name'],
                         $_FILES['fileToUpload']['name']);
}
unixmiah
  • 2,808
  • 1
  • 8
  • 25
  • I've added the enctype to the form and that attachment check to the php and it still isn't working. I still get the same email with no attachment on it. – AlexRS Apr 01 '15 at 13:59
0

You're using some old example, and an old version of PHPMailer, so I suggest you update to the latest. You also need to know how to handle file uploads, which is what you're missing. Here's the example bundled with PHPMailer:

<?php
/**
 * PHPMailer simple file upload and send example
 */
$msg = '';
if (array_key_exists('userfile', $_FILES)) {
    // First handle the upload
    // Don't trust provided filename - same goes for MIME types
    // See http://php.net/manual/en/features.file-upload.php#114004 for more thorough upload validation
    $uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['userfile']['name']));
    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('from@example.com', 'First Last');
        $mail->addAddress('whoto@example.com', 'John Doe');
        $mail->Subject = 'PHPMailer file sender';
        $mail->msgHTML("My message body");
        // 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;
    }
}
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>PHPMailer Upload</title>
</head>
<body>
<?php if (empty($msg)) { ?>
    <form method="post" enctype="multipart/form-data">
        <input type="hidden" name="MAX_FILE_SIZE" value="100000"> Send this file: <input name="userfile" type="file">
        <input type="submit" value="Send File">
    </form>
<?php } else {
    echo $msg;
} ?>
</body>
</html>
Synchro
  • 29,823
  • 14
  • 69
  • 85
  • I'm working to make a small addition on an existing website which has a fair amount set up and have limited time with it, so changing everything to a different version of phpmailer doesn't sound very practical. – AlexRS Apr 01 '15 at 14:39
  • I've tried running that example and I'm getting the response "Failed to move file to /tmp/88d542d578b1fc7dae265b22296701a217343830LIhRZ0" – AlexRS Apr 01 '15 at 14:39
  • So your system's temp folder is broken, or your web server does not have write access to it - fix them and it will work. Updating is a matter of swapping your old version for the new and changing one line of code. – Synchro Apr 01 '15 at 14:43