[英]Send email from JS code , with the help of PHP
我想在PHP的帮助下从JS代码发送电子邮件。
function validateForm(){
// sending a mail
$.get("../sendMail.php");
// more code
}
我有一个名为sendMail.php
的文件,该文件存储在服务器上:
sendMail.php:
<?php
session_start();
if(($_POST['name'] == '') || ($_POST['phone'] == '') || ($_POST['email'] == '')) {
/*------
echo 'Complete the forms correctly <br>
Click <a href="contact.php">here</a> to return on contact us page.';
-------*/
header("location:index.php");
} else {
$to = 'johnny@gmail.com';
$send_date = date('d-m-Y H:i:s');
$subject = 'Hello';
if (isset($_POST['check'])) {
$check = "yes";
} else {
$check = "no";
}
$message .= "<br>"."<br>";
$message .= "<strong>Date and time:</strong> ".$send_date ."<br/>";
$message .="<strong>Name:</strong> ".$_POST['name']."<br/>";
$message .="<strong>Phone: </strong> ".$_POST['phone']."<br/>";
$message .="<strong>Email: </strong> ".$_POST['email']."<br/>";
$message .="<strong>Agree to post ads:</strong> ".$check."<br/>";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8"' . "\r\n";
$headers .= 'From: someone@somewhere.com' . "\r\n";
mail($to, $subject, $message,$headers);
/*-----
echo 'The message was send <br>
Click <a href="index.php">here</a> to return on home page.';
window.location.href=""
-----*/
$_SESSION['thankyou']='Your email has been sent successfully';
header ('Location: index.php');
}
?>
但是,当我运行JS代码时,不会发送电子邮件。
有没有一种方法可以发送邮件,而无需在客户端使用某种电子邮件客户端?
如何解决此代码? 谢谢
您正在向PHP处理程序发出HTTP GET请求,而PHP处理程序要求使用HTTP POST和适当的POST变量执行该请求。 尝试使用正确的变量发布到URL。
(此外,您应该添加一个特殊的唯一签名,以防止将该表单用于垃圾邮件)。
试试这个
$.post( "../sendMail.php", { name: "John", phone: "1234", email:"abc@test.com" } );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.