所以我遇到了这个错误,并且不知道问题出在哪里。 这是我第一次使用PHPMailer,当我尝试附加文件时,它仅适用于与php路径相同的文件。我知道JavaScript无法获取文件的路径,但是如果用户选择了一个应该具有路径的文件,那我应该传递什么?
这是我的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>
这是我的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');
}
}
});
;}
这是我的控制器:
<?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;
}
?>
它完美地发送了没有附件的电子邮件,但是我确实需要这样做,而且我可能缺少一些简单的东西。
还是谢谢。