这是我到目前为止所得到的。我的内联代码在哪里?我也有html页面,其中表单编码但我已将其链接到此php页面。表单非常好,只是想为客户添加一些样式。
<?php
// define variables and set to empty values
$name_error = $email_error = $phone_error = $subject_error = "";
$name = $email = $phone = $message = $subject = $success = "";
//form is submitted with POST method
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$name_error = "Name is required";
} else {
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$name_error = "Only letters and white space allowed";
}
}
if (empty($_POST["email"])) {
$email_error = "Email is required";
} else {
$email = test_input($_POST["email"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$email_error = "Invalid email format";
}
}
if (empty($_POST["phone"])) {
$phone_error = "Phone is required";
} else {
$phone = test_input($_POST["phone"]);
// check if phone number is well-formed
if (!preg_match("/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3} [\s-]?\d{4}$/i",$phone)) {
$phone_error = "Invalid phone number";
}
}
if (empty($_POST["subject"])) {
$subject_error = "Subject is required";
} else {
$subject = test_input($_POST["subject"]);
// check if subject is proper text form
if (!preg_match("/^[a-zA-Z ]*$/",$subject)) {
$subject_error = "Invalid URL";
}
}
if (empty($_POST["message"])) {
$message = "";
} else {
$message = test_input($_POST["message"]);
}
if ($name_error == '' and $email_error == '' and $phone_error == '' and $subject_error == '' ){
$message_body = '';
unset($_POST['submit']);
foreach ($_POST as $key => $value){
$message_body .= "$key: $value\n";
}
// sends message to an email address specified.
$to = 'email@gmail.com';
$subject = 'Contact Form Submit';
if (mail($to, $subject, $name, $message)){
$success = "Message sent, thank you for contacting us!";
$name = $email = $phone = $message = $subject = '';
}
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
任何帮助将不胜感激!
答案 0 :(得分:0)
首先,我认为你的mail()函数调用中有错误。 使用此功能发送电子邮件的正确方法是:
mail($to, $subject, $message, $headers);
$ headers参数是可选的,但是如果你需要发送样式电子邮件,你需要这个。
这些标题的示例:
$headers = "From: $name<$email>".PHP_EOL;
$headers .= "MIME-Version: 1.0".PHP_EOL;
$headers .= "X-Mailer:PHP/".phpversion()."".PHP_EOL;
$headers .= "Content-Type: text/html; charset=utf-8".PHP_EOL;
使用这些标题,您要向电子邮件客户端说明您要发送html内容,然后,在您的$ message中,您可以添加一些标记,如:
$message = "<strong>Name:</strong> $name<br /><strong>Lorem ipsum</strong><br /><hr />Lorem ipsum";
注意:如果你想要包含一些图片,只需使用标签,但在src中使用公共网址,如下所示:
<img src="http://www.yoursite.com/emails/email-header.jpg" />