使用PHP和AJAX发送邮件

时间:2016-10-28 18:31:36

标签: php jquery html ajax email

我正在尝试使用Ajax和PHP发送自定义电子邮件。但是我没有将邮件发送到我的收件箱或说该脚本无法正常工作。请帮帮我们。

HTML表单:

<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="">
        <div class="col-sm-5 col-sm-offset-1">
          <div class="form-group">
            <label>Name *</label>
            <input type="text" id="name" class="form-control" required="required">
          </div>
          <div class="form-group">
            <label>Email *</label>
            <input type="email" id="email" class="form-control" required="required">
          </div>
          <div class="form-group">
            <label>Phone *</label>
            <input type="number" class="form-control" id="phone" required="required">
          </div>
          <div class="form-group">
            <label>Company Name</label>
            <input type="text" class="form-control" id="company">
          </div>
        </div>
        <div class="col-sm-5">
          <div class="form-group">
            <label>Subject *</label>
            <input type="text" name="subject" class="form-control" required="required" id="subject">
          </div>
          <div class="form-group">
            <label>Message *</label>
            <textarea name="message" id="message" required="required" class="form-control" rows="8"></textarea>
          </div>
          <div class="form-group">
            <button type="submit" name="submit" class="btn btn-primary btn-lg" id="submit">Send Mail</button>
          </div>
        </div>
      </form>

Ajax:

<script type="text/javascript">
$(function() {
    $("#submit").click(function() {
        var name       = $("#name").val();
        var email      = $("#email").val();
        var phone      = $("#phone").val();
        var company    = $("#company").val();
        var subject    = $("#subject").val();
        var message    = $("#message").val();
        var dataString = 'name='+ name + '&email='+ email + '&phone='+ phone + '&company='+ company + '&subject='+ subject + '&message='+ message;
        $.ajax({
            type: "POST",
            url: "send-mail.php",
            data: dataString,
            cache: true,
            success: function(html){
                $("#name").val('');
                $("#email").val('');
                $("#phone").val('');
                $("#company").val('');
                $("#subject").val('');
                $("#message").val('');
                $('.alert-success').html("Message sent! Rest assaured. I will reply within 24 hours.").fadeIn(500);
            }  
        });
        return false;
    });
});
</script>

send-mail.php页面:

<?php
$to = 'info@siliguriwebmedia.in';
$subject = "Inquiry form received";

$name    = (!empty($_POST['name']))?$_POST['name']:null;
$email   = (!empty($_POST['email']))?$_POST['email']:null;
$phone   = (!empty($_POST['phone']))?$_POST['phone']:null;
$company = (!empty($_POST['company']))?$_POST['company']:null;
$subject = (!empty($_POST['subject']))?$_POST['subject']:null;
$message = (!empty($_POST['email']))?$_POST['message']:null;

$htmlContent = '
    <html>
    <head>
        <title>Inquiry form received</title>
    </head>
    <body>
        <table cellspacing="0" style="border: 2px dashed #FB4314; width: 300px; height: 200px;">
            <tr>
                <th>Name:</th>
                <td>$name</td>
            </tr>
            <tr style="background-color: #e0e0e0;">
                <th>Email:</th>
                <td>$email</td>
            </tr>
            <tr>
                <th>Phone:</th>
                <td>$phone</td>
            </tr>
            <tr style="background-color: #e0e0e0;">
                <th>Company:</th>
                <td>$company</td>
            </tr>
            <tr>
                <th>Subject:</th>
                <td>$subject</td>
            </tr>
            <tr style="background-color: #e0e0e0;">
                <th>Message:</th>
                <td>$message</td>
            </tr>
        </table>
    </body>
    </html>';

// Set content-type header for sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// Send email
mail($to,$subject,$htmlContent,$headers);
?>

0 个答案:

没有答案