[英]what do i need to modify to send attachments with my phpmailer form?
我在我的網站上有一個工作正常的phpmailer聯系表,現在我希望能夠將文件附加到郵件中並能夠發送,但是我不知道如何發布數據。
這是我的HTML上的腳本
$(document).ready(function (e){
$("#contactForm").on('submit',(function(e){
e.preventDefault();
$('#boton').hide();
$('#loader-icon').show();
$.ajax({
url: "curriculum.php",
type: "POST",
dataType:'json',
data: {
"nombre":$('input[name="nombre"]').val(),
"fecha":$('input[name="fecha"]').val(),
"correo":$('input[name="correo"]').val(),
"ocupacion":$('input[name="ocupacion"]').val(),
"domicilio":$('input[name="domicilio"]').val(),
"telefono":$('input[name="telefono"]').val(),
"nacionalidad":$('input[name="nacionalidad"]').val(),
"salario":$('input[name="salario"]').val(),
"mensaje":$('input[name="mensaje"]').val()},
success: function(response){
alert(response.text);
},
error: function(){
alert(response.text);
}
});
}));
});
使用此腳本,我可以輸入下一個php,並且現在已經發送了我的電子郵件,我已經手動設置了郵件的附件,但顯然我想刪除該行,並能夠從網站上載ht文件
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'phpmailer/Exception.php';
require 'phpmailer/PHPMailer.php';
require 'phpmailer/SMTP.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = '****'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '****'; // SMTP username
$mail->Password = '****'; // SMTP password
$mail->SMTPSecure = '****'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = ****; // TCP port to connect to
//Recipients
$mail->setFrom('noreply@nautilusagency.com');
$mail->addAddress('ontiverosmtz.alberto@gmail.com');
$user_name = filter_var($_POST["nombre"], FILTER_SANITIZE_STRING);
$user_fecha = filter_var($_POST["fecha"], FILTER_SANITIZE_STRING);
$user_email = filter_var($_POST["correo"], FILTER_SANITIZE_EMAIL);
$user_ocupacion = filter_var($_POST["ocupacion"], FILTER_SANITIZE_STRING);
$user_domicilio = filter_var($_POST["domicilio"], FILTER_SANITIZE_STRING);
$user_telefono = filter_var($_POST["telefono"], FILTER_SANITIZE_STRING);
$user_nacionalidad = filter_var($_POST["nacionalidad"], FILTER_SANITIZE_STRING);
$user_salario = filter_var($_POST["salario"], FILTER_SANITIZE_STRING);
$content = filter_var($_POST["mensaje"], FILTER_SANITIZE_STRING);
$mail->addAttachment('assets/pagina.zip');
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = utf8_decode($subject);
$mail->Body = utf8_decode("<style>
body {background: linear-gradient(141deg, #ffffff 0%, #080708a1 51%, #000000 75%);}
.contenido
{
color: #428bca;
font-family: serif;
}
.msj1
{
color: #428bca;
}
.empresa
{
color: black;
}
</style>
<body>
<h3 class=msj1> Nombre: $user_name <br> </h3>
<h3 class=msj1> Fecha: $user_fecha <br> </h3>
<h3 class=msj1> Correo: $user_email <br> </h3>
<h3 class=msj1> Ocupacion: $user_ocupacion <br> </h3>
<h3 class=msj1> Domicilio: $user_domicilio <br> </h3>
<h3 class=msj1> Telefono: $user_telefono <br> </h3>
<h3 class=msj1> Nacionalidad: $user_nacionalidad <br> </h3>
<h3 class=msj1> Salario: $user_salario <br> </h3>
<h3 class=msj1> Mensaje: $content <br> </h3>
</body>");
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
?>
誰能幫助我或指出正確的方向?
引用的附件文件必須位於服務器上,並且可以使用文件的完整地址訪問。 當您通過Ajax上傳文件時,該文件將附加到何處?
目前您有:
$mail->addAttachment('assets/pagina.zip');
典型的正確且完全合格的文件參考為:
$uploadFileName = 'assets/pagina.zip'; // or wherever you put Ajax uploads.
$mail->addAttachment($_SERVER['DOCUMENT_ROOT'].'/upload/'.$uploadFileName);
// example string:
// /home/accountname/public_html/upload/assets/pagina.zip
我能夠成功上傳並發送帶有附件的郵件,但現在我無法添加其余的表單輸入。 這是我更改腳本以能夠附加文件的方式,但是我不知道如何發布其他輸入的其余信息。
HTML
$(document).ready(function (e){
$("#contactForm").on('submit',(function(e){
e.preventDefault();
$('#boton').hide();
$('#file').hide();
var file_data = $('#file').prop('files')[0];
var form_data = new FormData();
form_data.append('file', file_data);
$.ajax({
url: 'curriculum.php', // point to server-side PHP script
dataType: 'text',// what to expect back from the PHP script, if anything
cache: false,
contentType: false,
processData: false,
data: form_data,
type: 'post',
success: function(php_script_response){
alert(php_script_response); // display response from the PHP script, if any
}
});
}));
});
在PHP上,我添加了此代碼行
if ( 0 < $_FILES['file']['error'] ) {
echo 'Error: ' . $_FILES['file']['error'] . '<br>';
}
else {
move_uploaded_file($_FILES['file']['tmp_name'], 'assets/' . $_FILES['file']['name']);
$file='assets/' . $_FILES['file']['name'];
}
我嘗試將我擁有的第一個腳本與此腳本結合在一起,但是我還沒有弄清楚該怎么做
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.