drnl10253 2018-07-03 13:23
浏览 58

PHPMailer - 无法访问文件

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.

</div>
  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥15 MCNP里如何定义多个源?
    • ¥20 双层网络上信息-疾病传播
    • ¥50 paddlepaddle pinn
    • ¥20 idea运行测试代码报错问题
    • ¥15 网络监控:网络故障告警通知
    • ¥15 django项目运行报编码错误
    • ¥15 请问这个是什么意思?
    • ¥15 STM32驱动继电器
    • ¥15 Windows server update services
    • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏