Home > Enterprise >  phpMailer file attachment not working, any advice?
phpMailer file attachment not working, any advice?

Time:12-14

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"  autocomplete="off">
                    <ol >
                        
                        <li>
                            <label  for="file">Attachment</label>
                            <input  id="file" name="file" type="file" required/>
                        </li>

CodePudding user response:

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"  autocomplete="off" enctype="multipart/form-data">

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

CodePudding user response:

$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.

CodePudding user response:

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);
}

CodePudding user response:

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

  • Related