如何用Ajax发送电子邮件?

我试图通过按钮按下Ajax发送电子邮件。

PHP:

<?php
if($_POST){
    $message = $_POST['msg'];

    mail("mymailbox@gmail.com", "subj!", $message);
}
?>

JS:

(function($) {
    $('.cart__item__send-order').click(function() {
        var data = '&msg=' + 'random text';
        $.ajax({
            type: "POST",
            url: "order_mail.php",
            data: data,
            success: function(){
                console.log('email sent!');
            }
        });
    });
});
}(jQuery));

但什么都没发生!你能帮我找出它不起作用的原因吗?顺便给自己个安慰,mail sent!(电子邮件发送了!)'日志。

Well there might be something wrong with the PHP side, but you are building the data string wrong.

var data = '&msg=' + 'random text';

needs to be

var data = 'msg=' + 'random text';

or

var data = { msg : 'random text' }; 

if you are working in a own machine, you must have activated some application that let you connect to a smtp server, or you must be mount your own smtp server. If you don't know how do this, I recommend to you that use PHPMailer is a php class that let you connect, for example, to google smtp and send from there any mail

http://phpmailer.worxware.com/

a comment, in the success function try

success: function(response){
console.log(response);
}

with this you confirm the response of your php code