0

So I'm getting this error, and haven't a clue of what the problem is. It's the first time I'm using PHPMailer, and when I try to attach a file, it only works with files that are at the same path as the php. I know that the JavaScript can't get the path of the file, but if the user chooses a file it is supposed to have a path, what am I passing?

Here is my html

<!DOCTYPE HTML>
<html>

<head>
    <script type="text/javascript" src="teste_Functions.js"></script>
    <script type="text/javascript" src="jquery.min.js"></script>
    <script type="text/javascript" src="jquery.fullscreen-min.js"></script>
    <script type="text/javascript" src="jquery.cookie.js"></script>
    <script type="text/javascript" src="jquery.steps.js"></script>
</head>

<body>
    <label>CC:</label>
    <textarea id="cc" style="height: 20px; width: 100%"></textarea><br>
    <label>Name:</label>
    <textarea id="name" style="height: 20px; width: 100%"></textarea><br>
    <label>Telefone:</label>
    <textarea id="telefone" style="height: 20px; width: 100%"></textarea><br>
    <label>Header:</label>
    <textarea id="header" style="height: 20px; width: 100%"></textarea><br>
    <label>Body:</label>
    <textarea id="body" style="height: 20px; width: 100%"></textarea><br>
    <label>Anexos:</label>
    <input id="anexos" type="file" name="userfile">
    <button onclick="teste()">Test</button>
</body>

</html>

Here is my JavaScript:

function teste(){
var cc = $('#cc').val();
console.log(cc);
var name = $('#name').val();
console.log(name);
var telefone = $('#telefone').val();
console.log(telefone);
var header = $('#header').val();
console.log(header);
var body = $('#body').val();
console.log(body);
var fileInput = document.getElementById('anexos');  
var filename = fileInput.files[0].name;
console.log('File Input:', fileInput);
console.log('File Name:', filename);
$.ajax({
    type: "POST",
    url : 'http://localhost/teste/mailer',
    data: {'cc' : cc , 'name' : name, 'telefone' : telefone, 'header' : header, 'body' : body, 'anexos' : filename},
    success:function(response){
        console.log(response);
        if (response == true) {
            console.log('True');
        }
        if (response == false) {
            console.log('False');
        }
        else{
            console.log('Nem true nem false');
        }
    }
});

;}

And here is my controller:

<?php

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

require 'C:/wamp64/www/teste/PHPMailer/src/Exception.php';
require 'C:/wamp64/www/teste/PHPMailer/src/PHPMailer.php';
require 'C:/wamp64/www/teste/PHPMailer/src/SMTP.php';

$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
    //Server settings                                // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp.gmail.com';               // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                              // Enable SMTP authentication
    $mail->Username = '**********';         // SMTP username
    $mail->Password = '**********';                       // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to
    $mail->SMTPOptions = array(
        'ssl' => array(
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => true
        )
    );
    //Recipients
    $mail->setFrom('************', $_POST['name'], $_POST['telefone']);
    $mail->addAddress('*************', 'Domingos');     // Add a recipient
    //$mail->AddCC($_POST['cc']);


    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = $_POST['header'];
    $mail->Body    = $_POST['body'];
    $mail->AltBody = $_POST['body'];
    $mail->addAttachment($_POST['anexos'], 'teste.jpg');

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
?>

It sends the email perfectly without the attachments, but I really need this, and I'm probably missing something easy.

Thanks anyway.

Bharata
  • 13,509
  • 6
  • 36
  • 50
  • http://php.net/manual/en/features.file-upload.post-method.php – NobbyNobbs Jul 03 '18 at 13:35
  • https://stackoverflow.com/questions/11764156/send-file-attachment-from-form-using-phpmailer-and-php – Sajan Jul 03 '18 at 13:36
  • @Sajan I already saw that question and those answers multiple times, but I cant still see what I'm doing wrong... – Gabriel Gomes Jul 03 '18 at 13:47
  • @NobbyNobbs I tryed using that before this question, and I didnt took me anyway, I don't think I'm able to use that, thanks! – Gabriel Gomes Jul 03 '18 at 13:48
  • You're simply not handling the file upload at all - you've ignored all that the PHP docs say. Look at [the send file upload example provided with PHPMailer](https://github.com/PHPMailer/PHPMailer/blob/master/examples/send_file_upload.phps). You should **not** be disabling certificate verification; fix the problem (update your CA certs), don't ignore it - it's been documented here and in the troubleshooting guide many times. Why are you using `textarea` for inputs? Why not `input`? – Synchro Jul 03 '18 at 13:59
  • @Synchro Your way seems a good way to do it, and it seems you understand it. So the problem here is, I need to do it as I'm showing it, I need to have a button that submites the information I want to appear at the email. The text areas are working just fine. Don't worry about it, I send what ever I want with it. AS I SAID, the only problem here is that it returns (C:/fakepath/whateverimg.jpg) even when I var_dump it on my controller. COuld you be a bit clearer and explain me what I'm doing wrong, and not say I ignored everything, please. – Gabriel Gomes Jul 03 '18 at 14:25
  • Your form is workable as it is. The big thing you've missed is that file uploads do not appear in `$_POST`; they are in `$_FILES`. That's not all though - you need to validate the upload using `is_uploaded_file` or `move_uploaded_file` and move it to a safe location (usually outside web root), as the example I pointed you at shows. – Synchro Jul 03 '18 at 14:29
  • That was way clearer. So I will try this out, and say something soon. Thanks @Synchro :) Maybe you could put an answer here for the following people! – Gabriel Gomes Jul 03 '18 at 14:40
  • 1
    Pretty much everything I said is covered by the PHP docs that @NobbyNobbs pointed at, and I'd always recommend reading the docs before posting here. – Synchro Jul 03 '18 at 14:42
  • @Synchro I used the exact same example as you did in the link that you send. It sends te email perfectly, but there's a problem, my file is always empty in the received email, I var_dump the $uploadfile variable, and it seems just fine. I dont receive any error. – Gabriel Gomes Jul 04 '18 at 09:24
  • @Synchro Nevermind, the problem has to do with the fact of the extension. Is there a way of get the extension of the file and set it at the name? – Gabriel Gomes Jul 04 '18 at 09:38
  • Did you set the enctype attribute on your form? If you didn’t, you’ll get no attachments. – Synchro Jul 04 '18 at 09:41
  • @Synchro No, I used because it needs to go directly to my controller... and I doesn't seems to have a way to insert that to it. – Gabriel Gomes Jul 04 '18 at 10:22
  • But the way I'm doing it it sends me the attachment, but i have to give it the name with the extention, example: $mail->addAttachment($uploadfile, 'image.jpg'), if I only set 'image', or doesn't set anything, the file is sent, but it's 'empty'. If this even makes sense... – Gabriel Gomes Jul 04 '18 at 10:29
  • Early in the morning and I'm stupid, this way is in CodeIgniter and it gives me the enctype, in the form. I just want to know how can I get the type of the file and set it in the name of the addAttachments, If you could help, would be awsome. – Gabriel Gomes Jul 04 '18 at 10:35
  • If it has a file name with an extension, PHPMailer will set the type automatically. The form you posted has no enctype, I don’t think it has anything to do with your controller. Var_dump the contents of $_FILES to check you are receiving the upload. This is all straight out of the PHP docs. – Synchro Jul 04 '18 at 13:40
  • I var_dump the $uploadfile variable, and the name of it is whatever.tmp, and it doesn't have any type. – Gabriel Gomes Jul 04 '18 at 14:23

0 Answers0