[英]Basic email form (php)
我正在嘗試使用PHP從表單發送基本電子郵件。 表單使用jquery.validate.js進行了驗證(工作正常),但是我無法發送電子郵件。 我只是不斷獲取提交時在操作中聲明的php文件。
該站點位於此處 ,表單如下所示:
<form method="post" action="send-email.php" id="contact-form" class="form-horizontal" role="form">
<fieldset>
<div class="col-12 col-md-6 col-lg-6 contact--left">
<div class="form-group">
<label for="name" class="col-12 control-label brand brand-font">Name</label>
<div class="col-12">
<input type="text" class="form-control brand-font--standard" name="name" id="name">
</div>
</div>
<div class="form-group">
<label for="email" class="col-12 control-label brand brand-font">Email</label>
<div class="col-12">
<input type="email" class="form-control brand-font--standard" name="email" id="email">
</div>
</div>
<div class="form-group">
<label for="phone" class="col-12 control-label brand brand-font">Telephone</label>
<div class="col-12">
<input type="tel" class="form-control brand-font--standard" name="phone" id="phone">
</div>
</div>
</div>
<div class="col-12 col-md-offset-1 col-md-5 col-lg-offset-1 col-lg-5 contact--right">
<div class="form-group">
<label for="message" class="col-12 control-label brand brand-font">Message</label>
<div class="col-12">
<textarea class="form-control brand-font--standard" rows="6" name="message" id="message"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-12"> </div>
<div class="col-12">
<button type="submit" class="btn btn-block btn--orange brand-font">Submit</button>
</div>
</div>
</div>
</fieldset>
</form>
...,而send-email.php文件如下所示:
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "contact@designbydarren.com";
$email_subject = "Your email subject line";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['phone']) ||
!isset($_POST['message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$phone = $_POST['phone']; // required
$message = $_POST['message']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$name)) {
$error_message .= 'The name you entered does not appear to be valid.<br />';
}
if(strlen($message) < 2) {
$error_message .= 'The message you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($phone)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
我試過使用其他一些我在網絡上找到的示例,但還沒有任何內容。 為菜鳥式的問題而道歉,但我更像是一個前鋒,所以對此類型的東西一無所知(但是,對於你們來說,這似乎很基本)
謝謝你的時間!
您必須在send-email.php的以下行中更改參數
if(!preg_match($email_exp,$email_from)){
因為您已經在$ email_from中提取了$ _POST ['email']
並且在使用默認的PHP郵件功能時,更改php.ini文件中的SMTP設置
希望能幫助到你!!!
您已按照以下步驟定義了訪問者的電子郵件地址:
$email_from = $_POST['email']; // required
但在第33行中,您嘗試使用$email
,它是未定義的:
if(!preg_match($email_exp,$email)) {
更改為:
if(!preg_match($email_exp,$email_from)) {
那應該可以解決問題,並且您的表格也可以工作。
不相關的旁注:PHP的mail()
函數是一個錯誤的選擇。 我建議切換到PHPMailer或SwiftMailer,因為它們可以為您提供更好的控制並且更強大。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.