如何使用PHP从联系表单发送电子邮件 [英] How to send email from contact form using PHP
本文介绍了如何使用PHP从联系表单发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好
我正在尝试将联系表单数据发送到电子邮件ID。页面显示成功消息,但我没有收到任何电子邮件。我如何在电子邮箱中收到所有联系信息。
联系表单的HTML ode:
Hi
I am trying to send contact form data to an email id. Page showing success message but i am not getting any email. And how can i receive all contact information in email box.
HTML ode for contact form:
<form method="post" action="sendEmail.php">
<div>
<div id="main">
<div class="comment1">
<p><input type="text" placeholder="your name..." name="name" id="name" /></p>
</div>
<div class="comment2">
<p><input type="text" placeholder="your e-mail..." name="email" id="email" /></p>
</div>
<div class="comment1">
<p><input type="text" placeholder="your suburb..." name="suburb" id="suburb" /></p>
</div>
<div class="comment2">
<p><input type="text" placeholder="your mobile no..." name="mobile" id="mobile"/></p>
</div>
<div class="comment1">
<p><input type="text" placeholder="prefer date...(dd/mm/yyyy)" name="date" id="date" /></p>
</div>
<div class="comment2">
<p><input type="text" placeholder="prefer time...(hh/mm)" name="time" id="time" /></p>
</div>
<div class="comment3">
<p><input type="text" placeholder="subject..." name="subject" id="subject"/></p> </div>
<div class="textarea">
<p><textarea placeholder="message..." name="comments" id="comments" rows="14" cols="5"></textarea></p>
</div>
<div class="buttoncontact">
<p><input type="submit" name="submit" id="submit" value="Submit"/></p>
</div>
< br $>
sendEmail.php的PHP代码。我不知道问题在哪里
PHP code for sendEmail.php. I dont know where is problem
$name = trim($_POST['name']);
$email = $_POST['email'];
$suburb = $_POST['suburb'];
$mobile = $_POST['mobile'];
$date = $_POST['date'];
$time = $_POST['time'];
$subject = $_POST['subject'];
$comments = $_POST['comments'];
$site_owners_email = 'owner@example.com'; // Replace this with your own hosting email address
$site_owners_name = 'Test email'; // replace with your name
if (strlen($name) < 2) {
$error['name'] = "Please enter your name";
}
if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
$error['email'] = "Please enter a valid email address";
}
if (strlen($suburb) < 3) {
$error['suburb'] = "Please enter your suburb.";
}
if (strlen($mobile) < 10) {
$error['mobile'] = "Please enter your mobile no. Only numerics!!";
}
if (strlen($date) < 3) {
$error['date'] = "Please enter your booking date.";
}
if (strlen($time) < 3) {
$error['time'] = "Please enter your booking time.";
}
if (strlen($subject) < 2) {
$error['title'] = "Please enter a subject";
}
if (strlen($comments) < 3) {
$error['comments'] = "Please leave your message.";
}
if (!$error) {
require_once('phpMailer/class.phpmailer.php');
$mail = new PHPMailer();
$mail->From = $email;
$mail->FromName = $name;
$mail->Subject = "Website Contact Form";
$mail->AddAddress($site_owners_email, $site_owners_name);
$mail->AddAddress('', '');
$mail->Message = $comments;
$mail->Mailer = "smtp";
$mail->Host = "my host";
$mail->Port = "port";
$mail->SMTPSecure = "None";
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "my@email.com"; // SMTP username
$mail->Password = "password"; // SMTP password
$mail->Send();
echo "<li class="success"> Congratulations, " . $name . ". We've received your email. We'll be in touch as soon as we possibly can! </li>";
} # end if no error
else {
$response = (isset($error['name'])) ? "<li>" . $error['name'] . "</li> \n" : null;
$response .= (isset($error['email'])) ? "<li>" . $error['email'] . "</li> \n" : null;
$response .= (isset($error['suburb'])) ? "<li>" . $error['suburb'] . "</li>\n" : null;
$response .= (isset($error['mobile'])) ? "<li>" . $error['mobile'] . "</li>\n" : null;
$response .= (isset($error['date'])) ? "<li>" . $error['date'] . "</li>\n" : null;
$response .= (isset($error['time'])) ? "<li>" . $error['time'] . "</li>\n" : null;
$response .= (isset($error['subject'])) ? "<li>" . $error['subject'] . "</li>\n" : null;
$response .= (isset($error['comments'])) ? "<li>" . $error['comments'] . "</li>\n" : null;
echo $response;
} # end if there was an error sending
?>
推荐答案
name = trim(
name = trim(
_POST [' name']);
_POST['name']);
email =
email =
这篇关于如何使用PHP从联系表单发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文