【发布时间】:2021-12-24 22:59:19
【问题描述】:
我正在尝试在收到 onApprove 时发送邮件,但我不知道该怎么做。所以,这是我的 index.html:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Fede Pistone</title>
<!-- Internos -->
<link rel="stylesheet" type="text/css" href="css/index.css">
<!-- Externos -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<h3>Formulario de Contacto</h3>
<div class="container">
<form method="POST" id="myForm" action="enviarmail.php" >
<label for="nombre">Nombre:</label>
<input type="text" id="nombre" name="nombre" placeholder="Ingresa tu nombre...">
<label for="apellido">Apellido</label>
<input type="text" id="apellido" name="apellido" placeholder="Ingresa tu apellido...">
<label for="email">Email</label>
<input type="text" id="email" name="email" placeholder="Ingresa tu email...">
<label for="mensaje">Mensaje:</label>
<textarea id="mensaje" name="mensaje" placeholder="Ingresa tu consulta..." style="height:200px"></textarea>
<div id="smart-button-container">
<div style="text-align: center;">
<div id="paypal-button-container" name="divPayPal"></div>
</div>
</div>
</form>
</div>
<p>Al presionar el botón, se abrirá un formulario en el cuál deberá abonar un monto de u$s20. <br>Recuerde que al presionarlo, usted está aceptando esto. <br>Una vez procesado el pago, recibirá un mail con un link para una reunión virtual con un horario específico. <br>Por favor, consulte en su bandeja de entrada y/o spam.</p>
<script src="https://www.paypal.com/sdk/js?client-id=sb&enable-funding=venmo¤cy=USD" data-sdk-integration-source="button-factory"></script>
<script>
function initPayPalButton() {
paypal.Buttons({
style: {
shape: 'rect',
color: 'gold',
layout: 'vertical',
label: 'paypal',
},
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{"description":"Ejemplo de botón","amount":{"currency_code":"USD","value":20}}]
});
},
onApprove: function(data, actions) {
var nombre = document.getElementById('nombre').value;
var apellido = document.getElementById('apellido').value;
var nombreJunto = nombre + " " + apellido;
var mailForm = document.getElementById('email').value;
var mensajeForm = document.getElementById('mensaje').value;
$.ajax({
url: "enviarmail.php",
method: "POST",
data: {action: 'e-mail', nombre: nombreJunto, email: mailForm, mensaje: mensajeForm},
dataType: "json",
success: function(response){
if(response.status == 200){
console.log(response + "status: " + response.status);
const element = document.getElementById('paypal-button-container');
element.innerHTML = '';
element.innerHTML = '<h3>Gracias, nos estaremos comunicando contigo a la brevedad.</h3>';
}else{
console.log(response);
}
}
})
},
onError: function(err) {
const element = document.getElementById('paypal-button-container');
element.innerHTML = '';
element.innerHTML = '<h3>Ha ocurrido un error, reintente más tarde. </h3>';
console.log(err);
}
}).render('#paypal-button-container');
}
initPayPalButton();
</script>
</body>
</html>
这是我发送邮件的 php 文件:
<?php
function json_output($status = 200, $msg = 'OK', $data = null){
header("Content-Type: application/json; charset=UTF-8");
return json_encode([
'status' => $status,
'msg' => $msg,
'data' => $data
]);
die;
}
if(isset($_POST["nombre"]) && isset($_POST["email"]) && isset($_POST["mensaje"]) ){
$to = "francojoelbalsamo@gmail.com";
$subject = "Datos de formulario de contacto";
$contenido .= "Nombre: ".$_POST["nombre"]."\n";
$contenido .= "Apellido: ".$_POST["apellido"]."\n";
$contenido .= "Email: ".$_POST["email"]."\n\n";
$contenido .= "Mensaje: ".$_POST["mensaje"]."\n\n";
$header = "From: francojoelbalsamo@gmail.com\nReply-To:".$_POST["email"]."\n";
$header .= "Mime-Version: 1.0\n";
$header .= "Content-Type: text/plain; charset=\"UTF-8\"\r\n";
$header .= "Content-Transfer-Encoding: 8bit\r\n";
if(mail($to, $subject, $contenido ,$header)){
json_output();
}else{
json_output();
}
}
?>
所以,这是我收到onApprove的时候:
onApprove: function(data, actions)
php 文件工作正常,因为我正在做测试并且工作正常,现在我需要在收到 onAppove 后发送邮件,但我是这方面的新手,我不知道该怎么做,所以任何人都可以告诉我怎么做我做吗?
但是,当我收到此结果时,如何发送邮件?
好吧,在阅读了 cmets 之后,我尝试使用 AJAX function 发送邮件但无法正常工作,因为邮件永远不会发送,所以这是我修改的新代码:
$.ajax({
url: "enviarmail.php",
method: "POST",
data: {action: 'e-mail', nombre: nombreJunto, email: mailForm, mensaje: mensajeForm},
dataType: "json",
success: function(response){
if(response.status == 200){
console.log(response + "status: " + response.status);
const element = document.getElementById('paypal-button-container');
element.innerHTML = '';
element.innerHTML = '<h3>Gracias, nos estaremos comunicando contigo a la brevedad.</h3>';
}
else{
console.log(response);
}
}
})
我的新 php 文件已编辑:
<?php
function json_output($status = 200, $msg = 'OK', $data = null){
header("Content-Type: application/json; charset=UTF-8");
return json_encode([
'status' => $status,
'msg' => $msg,
'data' => $data
]);
die;
}
if(isset($_POST["nombre"]) && isset($_POST["email"]) && isset($_POST["mensaje"]) ){
$to = "francojoelbalsamo@gmail.com";
$subject = "Datos de formulario de contacto";
$contenido .= "Nombre: ".$_POST["nombre"]."\n";
$contenido .= "Apellido: ".$_POST["apellido"]."\n";
$contenido .= "Email: ".$_POST["email"]."\n\n";
$contenido .= "Mensaje: ".$_POST["mensaje"]."\n\n";
$header = "From: francojoelbalsamo@gmail.com\nReply-To:".$_POST["email"]."\n";
$header .= "Mime-Version: 1.0\n";
$header .= "Content-Type: text/plain; charset=\"UTF-8\"\r\n";
$header .= "Content-Transfer-Encoding: 8bit\r\n";
if(mail($to, $subject, $contenido ,$header)){
json_output();
}else{
json_output();
}
}
?>
谁能告诉我为什么这不能正常工作?
【问题讨论】:
-
使用 fetch() 或 XHR 或 library-of-your-choice 向服务器发送 AJAX 请求
-
网上的 Ajax 教程已经够你用一辈子了,你不需要其他的了。
-
“不起作用”不是对问题的有用描述。仅仅因为邮件没有发送并不一定意味着 AJAX 失败 - 在其他地方可能会出现很多其他问题。您是否进行了任何调试以尝试缩小问题范围?如果我注意到任何东西,我现在会查看代码并进一步评论,但问题并不总是通过阅读就很明显。
-
我能看到的第一件事是你正在做
document.getElementById('nombre').value,但在 HTML 中你有id="name",而不是id="nombre",所以这不会匹配并获得值。您甚至可能遇到阻止代码继续运行的 JS 错误 - 您是否检查过浏览器的控制台中的消息?appelido与lname和mensaje与subject的问题相同。
标签: php html jquery ajax email