【发布时间】:2014-08-23 17:12:46
【问题描述】:
我已经成功地在我的网站上为我的联系表单设置了一个 php 脚本,但我最近发现我的服务器提供商不接受 php。相反,我使用了 SMTP。
任何人都可以帮助我,因为这对我来说是新的。我尝试过使用其他脚本,但无法实现。
这是我的 php 代码:
<?php
/* Set e-mail recipient */
$myemail = "mail@louisreed.co.uk";
/* Check all form inputs using check_input function */
$name = check_input($_POST['name'], "Your Name");
$email = check_input($_POST['email'], "Your E-mail Address");
$subject = check_input($_POST['subject'], "Message Subject");
$message = check_input($_POST['message'], "Your Message");
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("Invalid e-mail address");
}
/* Let's prepare the message for the e-mail */
$subject = "Someone has sent you a message";
$message = "
Someone has sent you a message using your contact form:
Name: $name
Email: $email
Subject: $subject
Message:
$message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: http://louisreed.co.uk');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>
</body>
</html>
<?php
exit();
}
?>
我的 HTML 表单:
<div class="row">
<div class="col-lg-8 col-lg-offset-2">
<form role="form" action="http://www.louisreed.co.uk/includes/mailer.php" method="post">
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="name">Name</label>
<input class="form-control" type="text" id="name" name="name" placeholder="Name">
</div>
</div>
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="email">Email Address</label>
<input class="form-control" type="email" id="email" name="email" placeholder="Email Address">
</div>
</div>
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="subject">Subject</label>
<textarea placeholder="Subject" class="form-control" id="subject" name="subject" rows="1"></textarea>
</div>
</div>
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="message">Message</label>
<textarea placeholder="Message" class="form-control" id="message" name="message" rows="5"></textarea>
</div>
</div>
<br>
<div class="row">
<div class="form-group col-xs-12">
<button type="submit" class="btn btn-lg btn-primary">Send</button>
</div>
</div>
</form>
</div>
</div>
欢迎提出任何建议!
谢谢! :)
路易
【问题讨论】:
-
smtp 与您的代码无关,它位于您本地主机某处的 php.ini 文件中。如果您在使用 SMTP 的 php 中使用 mail(),那么如果您的提供商不支持 php,您就会遇到问题
-
感谢您的回复,对不起,我听起来像个菜鸟,但我的 php.ini 文件中有什么? - 我已经进行了搜索,但我什至找不到这个文件。干杯
-
不是找不到。问题是您的主机控制着这个文件。
-
是的,我的提供商不支持 php,我还有什么其他选择?
-
@SuperDJ 这是我想要避免的!我想要一个漂亮的联系表格而不是使用 mailto:
标签: php forms email smtp contact