sendmail.php很难过。

时间:2013-01-10 05:12:08

标签: php html email

我有两段代码,我无法通过电子邮件表单发送邮件。我想我错过了sendmail.php文件。任何人都可以帮我写这个或使用脚本来使用它吗?我的代码是。

  var name = jQuery("input#name").val();
    if (name == "") {
  jQuery("span#name_error").show();
  jQuery("input#name").focus();
  return false;
}
  var email = jQuery("input#email").val();
  if (email == "") {
  jQuery("span#email_error").show();
  jQuery("input#email").focus();
  return false;
}

var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
if(!emailReg.test(email)) {
jQuery("span#email_error2").show();
jQuery("input#email").focus();
  return false;
}

var subject = jQuery("input#subject").val();

  var msg = jQuery("textarea#msg").val();
  if (msg == "") {
  jQuery("span#msg_error").show();
  jQuery("textarea#msg").focus();
  return false;
}

    var dataString = 'name='+ name + '&email=' + email + '&subject=' + subject + '&msg=' + msg;
    //alert (dataString);return false;

  jQuery.ajax({
  type: "POST",
  url: "sendemail.php",
  data: dataString,
  success: function() {
    jQuery('#contactform').html("<div id='message'></div>");
    jQuery('#message').html("<strong>Contact Form Submitted!</strong>")
    .append("<p>We will be in touch soon.</p>")
    .hide()
    .fadeIn(1500, function() {
      jQuery('#message');
    });
  }
 });
return false;
});

});'

<div id="contactform">
                              <form id="contact" action="#">
                                  <fieldset>
                                      <span class="error" id="name_error">Please enter name !</span>
                                      <span class="error" id="email_error">Please enter email address !</span>
                                      <span class="error" id="email_error2">Please enter valid email address !</span>
                                      <span class="error" id="msg_error">Please enter message !</span>
                                      <label for="name" id="name_label">Your Name (required)</label>
                                      <input type="text" name="name" id="name" size="50" value="" class="text-input" />
                                      <label for="email" id="email_label">Your Email (required)</label>
                                      <input type="text" name="email" id="email" size="50" value="" class="text-input" />
                                      <label for="subject" id="subject_label">Subject</label>
                                      <input type="text" name="subject" id="subject" size="50"  value="" class="text-input" />
                                      <label for="msg" id="msg_label">Your Message (required)</label>
                                      <textarea rows="8" name="msg" id="msg" class="text-input"></textarea>
                                      <br />
                                      <input type="submit" name="submit" class="button" id="submit_btn" value="Send"/>
                                  </fieldset>
                              </form>
                        </div><!-- end contactform --> 

请帮忙。

2 个答案:

答案 0 :(得分:0)

使用以下代码从您的

发送电子邮件
<?php
if(isset($_POST['submit']))
{
    $to = "someone@example.com"; // Replace This Id With Another ( Where U Need to Send )
    $subject = $_POST['subject']; // Enter A Subject..
    $message = 'Name : '.$_POST['name'].'<br/> Email : '.$_POST['email'].'<br/> Subject : '.$_POST['subject'].'<br/> Message : '.$_POST['msg'].' ';
    $from = $_POST['email'];
    $headers = "From:" . $from;
    mail($to,$subject,$message,$headers);
    echo "Mail Sent.";
}
?> 

*您需要将$to值替换为* 的值 例如,如果您需要将邮件发送至abc@va.com,请使用此$to = "abc@va.com";

答案 1 :(得分:0)

试试以下示例:

sendmail.php

<?php
$uname     =   mysql_real_escape_string($_POST['name']);
$uemail    =   mysql_real_escape_string($_POST['email']);
$usubject  =   mysql_real_escape_string($_POST['subject']);
$umsg      =   mysql_real_escape_string($_POST['msg']);
$to = 'john@gmail.com'; //Here add your to email where you have to sent after submitting the form
$subject = 'Test Mail';
$headers = "From: " . strip_tags($uemail) . "\r\n";
$headers .= "Reply-To: ". strip_tags($uemail) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr style='background: #eee;'><td><strong>User Name :</strong> </td><td>".$uname."</td></tr>";
$message .= "<tr><td><strong>Email :</strong> </td><td>".$uemail."</td></tr>";
$message .= "<tr><td><strong>Subject :</strong> </td><td>".$usubject."</td></tr>";
$message .= "<tr><td><strong>Message :</strong> </td><td>".$umsg."</td></tr>";
$message .= "</table>";
$message .= "</body></html>";
mail($to, $subject, $message, $headers);
echo "<strong>Contact Form Submitted!</strong>";
?>

我认为这可以帮助您解决问题。