richag
richag

Reputation: 185

phpMailer file attachment not working, any advice?

I have an HTML form and I am using phpMailer. When completing the form and clicking submit all data entered in the text input fields etc work as expected. However, when I attach a file to the input type "file" when receiving the email no attachment is there.

Does anyone have any suggestions as to why the attachment is not attached?

FYI - at current, the site has no SSL certificate and the emails are going to the spam folder - could this be the issue?

I' am new to phpMailer and have been able to achieve the below by research.

phpMailer()

<?php

$file = $_POST['file'];

// more variables... 

$CustomerSignature = $_POST['q21'];
$Date = $_POST['q22'];
$filename = "./upload/" . $_FILES["file"]["name"];
move_uploaded_file($_FILES["file"]["tmp_name"],"./upload/" . $_FILES["file"]["name"]);

    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;
    use PHPMailer\PHPMailer\SMTP;

    require 'phpMailer/src/Exception.php';
    require 'phpMailer/src/PHPMailer.php';
    require 'phpMailer/src/SMTP.php';
    require 'phpMailer/src/OAuth.php'; 

    $mail = new PHPMailer;
    $mail->isSMTP();
    $mail->SMTPDebug = 3;
    $mail->Host = 'smtp.hostinger.com';
    $mail->Port = 465;
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = 'ssl';
    $mail->Username = '######';
    $mail->Password = '######';
    $mail->isHTML(true);
    $mail->setFrom('######', '######');
    $mail->addReplyTo('######', '######');
    $mail->addAddress('######', '######');
    $mail->addAddress('######');
    $mail->AddAttachment($filename);
    $mail->Subject = 'New Credit Application';
    $mail->Body = '<b>New Credit Application (Online)!</b></br>'
.'Trading Name: '.$TradingName.'</br>'
.'blah blah blah etc.'
.'Customer Signature: '.$CustomerSignature.'</br>'
.'Date: '.$Date.'</br>';
    if (!$mail->send()) {
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        echo 'The email message was sent.';
    }
?>

HTML file attachment part:

<form method="POST" action="testmail2.php" id="myform" class="fs-form fs-form-full" autocomplete="off">
                    <ol class="fs-fields">
                        
                        <li>
                            <label class="fs-field-label fs-anim-upper" for="file">Attachment</label>
                            <input class="fs-anim-lower" id="file" name="file" type="file" required/>
                        </li>

Upvotes: 1

Views: 4190

Answers (4)

richag
richag

Reputation: 185

I have solved the issue

move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"]);
$filename = "upload/" . $_FILES["file"]["name"];

Thank you to everyone who assisted

Upvotes: 1

Synchro
Synchro

Reputation: 37810

You're missing one fundamental thing: you have not set the encoding type of the form. The default encoding will not work with files, so $_FILES will be empty, and consequently functions like move_uploaded_file will fail because there is no file to move, not because you have got the path or permissions wrong (though you can do that too!). Your form tag should be:

<form method="POST" action="testmail2.php" id="myform" class="fs-form fs-form-full" autocomplete="off" enctype="multipart/form-data">

Aside from that, base your code on the file upload example provided with PHPMailer.

Upvotes: 4

Fabio Scagliola
Fabio Scagliola

Reputation: 472

You might try the addStringAttachment() method instead

Despite of its name, it does not work with text files only, but with any kind of file

Please refer to the following code snippet

if (is_uploaded_file($_FILES["file"]["tmp_name"])) {
    $filename = $_FILES["file"]["tmp_name"];
    $handle = fopen($filename, "r");
    $contents = fread($handle, filesize($filename));
    fclose($handle);
    $mail->addStringAttachment($contents, $filename);
}

Upvotes: 1

Martin
Martin

Reputation: 22770

$filename should be an absolute filepath on your server, not a relative path as you have a leading dot which can screw up a lot of things ./ ...

You have:

 $filename = "./upload/" . $_FILES["file"]["name"];

What you should have is:

 move_uploaded_file($_FILES["file"]["tmp_name"],$_SERVER['Document_root']."/path/to/upload/" . $_FILES["file"]["name"]);
 ...
 $filename = $_SERVER['Document_root']."/path/to/upload/" . $_FILES["file"]["name"];

you should be using the PHPMailer error reporter ($mail->SMTPDebug = SMTP::DEBUG_SERVER;) to check this sort of things, as well as reading your own PHP error messages. Also you should follow the examples set in the github.

Upvotes: 2

Related Questions