我正在嘗試在收到 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 = "[email protected]";
$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: [email protected]\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 后發送郵件,但我是這方面的新手,我不知道該怎么做,所以任何人都可以告訴我我是怎么做的?
但是,當我收到此結果時如何發送郵件?
好吧,在閱讀了我嘗試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 = "[email protected]";
$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: [email protected]\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();
}
}
?>
誰能告訴我為什么這不能正常作業?
uj5u.com熱心網友回復:
在設定標準支付指南中,“添加和修改代碼”部分有注釋說明如何使用服務器,并鏈接到必要的資源——包括創建和捕獲訂單的 REST API 實作,以及演示呼叫實作它們的服務器路由的代碼fetch()。
發送電子郵件應該在這樣的服務器路由中完成,同時它將成功的捕獲回應傳播到呼叫 JavaScript。
uj5u.com熱心網友回復:
看了大家的回復,終于可以發郵件了。我添加了以下內容:
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;
return actions.order.capture().then(function(orderData) {
//all details
console.log('Capturando resultados', orderData, JSON.stringify(orderData, null, 2));
//show a msg
const element = document.getElementById('paypal-button-container');
element.innerHTML = '';
element.innerHTML = '<h3>Thanks for your purchase.</h3>';
$.ajax({
type: 'POST',
url: 'enviarmaildos.php',
dataType: "json",
data:{nombre:nombreJunto, email:mailForm, mensaje:mensajeForm},
success: function(response) {
alert(response.success);
},
error: function(xhr, status, error){
console.log(xhr);
}
});
if (document.referrer !== document.location.href) {
setTimeout(function() {
document.location.reload()
}, 5000);
}
});
},
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/359830.html
上一篇:無法在php中啟動類
