[英]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.