使用Ajax发送电子邮件

时间:2012-11-26 06:58:52

标签: php ajax email

我正在使用以下内容通过ajax发送电子邮件。它没有发送name

var submitForm = function(){
                var name = jQuery('[name=name]').val();
                var email = jQuery('[name=email]').val();
                var gender = jQuery('[name=gender]').val();
$.ajax({
                    url: 'http://domainname.com/Email.php',
                    data: {name: name, email: email, phone: phone, gender: gender},
                    type: 'POST',
                    success: function ( data ) {
                        $(".rhino-container").html("Dear <b>"+name+"</b> You have applied successfully, Our career counseller will get in touch with you."); 
                    }
                });

            };

以下是email.php

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $gender = $_POST['gender'];
    $to = 'yourdomain@gmail.com';
    $from = $email;
    $subject = 'Apply Online Details from a user as below---';
    $body = "Hello Admin<br><br>
            Name: <strong>$name</strong><br>
            Email: $email<br>
            Phone: $phone<br>
            Gender: $gender<br>
                ";
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= "From: $from";
    $ok = mail($to, $subject, $body, $headers);
    if($ok)
        echo '1';
    else
        echo '0';
?>

2 个答案:

答案 0 :(得分:2)

var submitForm = function () {
    var name = $('input[name="name"]').val();
    var email = $('input[name="email"]').val();
    var phone = $('input[name="phone"]').val();
    var gender = $('input[name="gender"]').val();

    var param = {
        "name": name,
            "email": email,
            "phone": phone,
            "gender": gender
    };

    $.ajax({
        url: 'http://domainname.com/Email.php',
        data: param,
        type: 'POST',
        dataType: json,
        success: function (data) {
            if (data.status) {
                $(".rhino-container").html("Dear <b>" + name + "</b> You have applied successfully, Our career counseller will get in touch with you.");
            } else {
                $(".rhino-container").html("Something went wrong");
            }
        }
    });

};

请将此PHP用于服务器端。

header('Content-type:application / json'); echo $ ok? json_encode(array(“status”=&gt; true)):json_encode(“status”=&gt; false));

答案 1 :(得分:0)

每行应使用LF(\ n)分隔。行不应超过70个字符。不要使用休息。

除此之外,您是否尝试过在服务器上发送电子邮件?直接的PHP没有ajax?也许您的邮件设置不正确,至少可以尝试一下。

在jQuery中也使用选择器,我还没有看到你的html,但做了类似

的事情
var name = $('#name').val();
祝你好运。