使用phpmailer表单发送附件需要修改什么



我的网站上有一个可以使用的phpmailer联系表格,现在我想能够将文件附加到邮件中并发送它,但我不知道如何POST数据。

这是我的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 PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerException;
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
  • 这篇文章对您也很有用,告诉您如何通过Ajax上传文件。

  • 如RiggsFilly所述,请阅读PHPMailer文档。

  • 请看Synchro的这个例子

我成功地上传并发送了带有附件的邮件,但现在我无法添加其余的表单输入。这是我更改脚本以能够附加文件的方式,但这种方式我不知道如何发布其他输入的其余信息。

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'];
}

我试着把我的第一个剧本和这个结合起来,但我还没想好怎么做

最新更新