PHPMailer 添加附件
PHPMailer AddAttachment
我正在使用 PHPMailer 从表单发送数据,它都已正确连接并且适用于我的所有输入字段。除了我在发送文件上传作为附件时遇到问题。
我的php附件如下
if(is_array($_FILES)) {
$mail->AddAttachment($_FILES['image']['tmp_name'],$_FILES['image']['name']);
}
这是输入文件的HTML,您可以看到它是基本的
<input type="file" id="image" name="image" class="" />
表单已提交,但在 error_log 中显示
PHP Notice: Undefined index: image in /dir_location/mailer.php
哦也顺便排除一下,我用的是<form id="form" method="POST" enctype="multipart/form-data">
我也试过了
if (array_key_exists('image', $_FILES)) {
$uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['image']['name']));
if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile)) {
$mail->addAttachment($uploadfile, 'UPLOAD file');
}
}
但这也没有用。同样的问题。
非常感谢其他人的任何建议,谢谢。
通过进一步研究,如果有人在 PHPMailer 和 Ajax 提交文件附件时遇到问题,我找到了解决方案。
使用 PHPMailer for file upload 提供的 PHP 文档,这适用于最终的 ajax 修正案。
if (array_key_exists('userfile', $_FILES)) {
$uploadfile = tempnam(sys_get_temp_dir(), hash('sha256', $_FILES['userfile']['name']));
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
// Attach the uploaded file
$mail->addAttachment($uploadfile, $_FILES['userfile']['name']);
//Send the message, check for errors
if (!$mail->send()) {
$msg = 'Sorry, something went wrong. Please try again later.';
} else {
$msg = 'Message sent! Thanks for contacting us.';
}
} else {
}
}
Ajax 为了提交表单,我需要更改 formData 并添加数据类型。
$(function() {
var form = $('#paymentForm');
$(form).submit(function() {
var formData = new FormData(this);
$.ajax({
type: 'POST',
url: 'mailer.php',
data: formData,
dataType : "json",
contentType: false,
cache: false,
processData: false
})
});
});
我正在使用 PHPMailer 从表单发送数据,它都已正确连接并且适用于我的所有输入字段。除了我在发送文件上传作为附件时遇到问题。
我的php附件如下
if(is_array($_FILES)) {
$mail->AddAttachment($_FILES['image']['tmp_name'],$_FILES['image']['name']);
}
这是输入文件的HTML,您可以看到它是基本的
<input type="file" id="image" name="image" class="" />
表单已提交,但在 error_log 中显示
PHP Notice: Undefined index: image in /dir_location/mailer.php
哦也顺便排除一下,我用的是<form id="form" method="POST" enctype="multipart/form-data">
我也试过了
if (array_key_exists('image', $_FILES)) {
$uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['image']['name']));
if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile)) {
$mail->addAttachment($uploadfile, 'UPLOAD file');
}
}
但这也没有用。同样的问题。
非常感谢其他人的任何建议,谢谢。
通过进一步研究,如果有人在 PHPMailer 和 Ajax 提交文件附件时遇到问题,我找到了解决方案。
使用 PHPMailer for file upload 提供的 PHP 文档,这适用于最终的 ajax 修正案。
if (array_key_exists('userfile', $_FILES)) {
$uploadfile = tempnam(sys_get_temp_dir(), hash('sha256', $_FILES['userfile']['name']));
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
// Attach the uploaded file
$mail->addAttachment($uploadfile, $_FILES['userfile']['name']);
//Send the message, check for errors
if (!$mail->send()) {
$msg = 'Sorry, something went wrong. Please try again later.';
} else {
$msg = 'Message sent! Thanks for contacting us.';
}
} else {
}
}
Ajax 为了提交表单,我需要更改 formData 并添加数据类型。
$(function() {
var form = $('#paymentForm');
$(form).submit(function() {
var formData = new FormData(this);
$.ajax({
type: 'POST',
url: 'mailer.php',
data: formData,
dataType : "json",
contentType: false,
cache: false,
processData: false
})
});
});