简体   繁体   English

使用ajax和jquery发送电子邮件

[英]Sending an email using ajax and jquery

I am trying to send an email using Ajax and jquery but even though the Ajax request gets successful, the email is not sent. 我正在尝试使用Ajax和jquery发送电子邮件,但是即使Ajax请求成功,也不会发送电子邮件。 The email system works for other things but not for the reply system I am making... My code is as below: 电子邮件系统可用于其他用途,但不适用于我正在制作的回复系统...我的代码如下:

var message =  $('#message').val();
var dataString = 'message=' + message ;
$.ajax({
    type: "POST",
    url: "reply.php",
    data: dataString,
    cache: false,
    success: function(html)
    {
        // success function
    }
});

the ajax calls become successful and the success function is called but the email is not sent .. the php code is as below ajax调用成功并调用成功函数,但未发送电子邮件.. php代码如下

$title = "something";
$message =  $_POST['message'];
$email = demo@demo
mail($email, "Reply to \"".$title."\"", $message,
"From: \"Ad reply\" <auto-reply@$host>\r\n" .
"X-Mailer: PHP/" . phpversion());

and I have done the ... if(isset($_POST)) thing as well. 而且我也做了... if(isset($_POST))事情。

Your code does not show where $host is set. 您的代码不显示$ host的设置位置。 Could be a malformed "from" address that is causing it? 可能是导致其格式错误的“发件人”地址吗?

On another not, it is not a good idea to send post data directly from a form into an email, you should run it through some filters first to ensure no nasty code can end up in the email. 另一方面,将表单中的帖子数据直接发送到电子邮件中不是一个好主意,您应该首先通过一些过滤器运行它,以确保电子邮件中不会包含讨厌的代码。

Try this code for sure this will work - 尝试使用此代码,以确保它可以正常工作-


PHP CODE: PHP代码:

if(isset($_GET['email'])){

$email = $_GET['email'];
$message = $_GET['msg'];

$to = "demo@example.com";
$subject = "Demo email";

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From:' .$email. "\r\n";            
mail($to, $subject, $message, $headers);
echo'success';
}
else{ echo'error'; }


AJAX/JQUERY SCRIPT : AJAX / JQUERY脚本:

email = $("#email").val();
msg = $("#msg").val();
$.ajax({
    url: 'ajxContact.php?email='+email+'&msg='+msg,
    cache: false,
    success: function(html){
    if(html == 'error'){
        alert("An error occurer and the message can't be sent.");
      }
    else {
        alert('Email has been successfully sent.');
      }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM