[英]How to make the contact form submit responses to email?
我想讓聯系表單在用戶點擊submit
按鈕時提交回復。 在用戶點擊submit
按鈕時,它還應該將他們重定向到thankyou.html
,我已經完成了。 但是回復也應該通過電子郵件發送給我。 我被困在那部分,無法在 PHP 文件中弄清楚。
$errors = '';
$myemail = 'm.hussainomer03@gmail.com'; // Put Your email address here.
if (empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message'])) {
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address)) {
$errors .= "\n Error: Invalid email address";
}
if (empty($errors)) {
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. " .
" Here are the details:\n Name: $name \n " .
"Email: $email_address\n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to, $email_subject, $email_body, $headers);
// Redirect to the 'thank you' page.
header('Location: contact-form-thank-you.html');
}
input[type=text], [type=email], select, textarea { width: 100%; padding: 12px; border: 1px solid #ccc; margin-top: 6px; margin-bottom: 16px; resize: vertical; } input[type=submit] { background-color: rgb(62, 3, 179); color: white; padding: 12px 20px; border: none; cursor: pointer; } input[type=submit]:hover { background-color: deeppink; }.contactform { position: relative; border-radius: 50px; background-color: #f2f2f2; padding: 5px; z-index: 2; display: block; margin-left: auto; margin-right: auto; margin-bottom: auto; margin-top: 1%; width: 100%; animation-name: gradient; animation-duration: 3s; animation-iteration-count: infinite; }.contactform:hover { animation-name: gradient; animation-duration: 15s; animation-iteration-count: infinite; }.column { float: center; width: 50%; margin-top: 6px; padding: 20px; display: block; margin-left: auto; margin-right: auto; width: 40%; }.row:after { content: ""; display: table; clear: both; } @media screen and (max-width: 600px) {.column, input[type=submit] { width: auto; margin-top: 0; } }
<section id="contact"> <div class="container" data-aos="fade-up"> <div class="contactform"> <div style="text-align:center"> <div class="section-title"> <h2><br/>Get In Touch</h2> </div> <p>Feel Free To Reach Out To Me Through This Form. </p> </div> <div class="row"> <div class="column"> <form name="myform" action="contact.php" method="POST"> <label for="firstname">First Name</label> <input type="text" id="firstname" name="firstname" placeholder="Your name.." required> <label for="lastname">Last Name</label> <input type="text" id="lastname" name="lastname" placeholder="Your last name.:" required> <label for="email">Email.</label> <input type="email" id="email" name="email" placeholder="Your Email.." required> <label for="subject">Subject</label> <textarea id="subject" name="subject" placeholder="Lets Collaborate..:" style="height:170px" required></textarea> <input type="submit" value="Submit"> </form> </div> </div> </div> </div> </section>
我之前嘗試重命名php
文件中的類/對象,但這也沒有成功。
將action="thankyou.html"
更改為action="yourMail.php"
以便將表單 POST 數據發送到包含mail()
function 的 php 文件,而不是發送到您的Thankyou.ZFC35FDC70D22FC69D2698EZA7538A 另請注意,您需要一個 SMTP 服務器來從您的本地主機發送郵件。 我會推薦你使用 PHPMailer。
<form name="myform" action="contact.php" method="POST">
<label for="name">First Name</label>
<input type="text" id="name" name="firstname" placeholder="Your name.." required>
<!-- <label for="lastname">Last Name</label>
<input type="text" id="lastname" name="lastname" placeholder="Your last name.." required> -->
<label for="email">Email:</label>
<input type="email" id="email" name="email" placeholder="Your Email.." required>
<label for="message">Subject</label>
<textarea id="message" name="message" placeholder="Lets Collaborate..." style="height:170px" required></textarea>
<input type="submit" value="Submit">
</form>
如上述答案所述,您應該將表單 POST 操作更改為您的mailTo.php
文件,因為它將發送郵件。 之后你可以做的是你的郵件發送成功,你可以通過PHP服務器重定向到thankYou.html
,失敗時你可以留在同一頁面上,顯示發現/發生的相應錯誤。 所以會的,
# on success
header('Location: http://your-website/contact-form-thank-you.html');
exit();
將action="thankyou.html"
更改為action="yourMail.php"
,以便將表單數據發送到包含mail()
function 的 PHP 文件。
<form name="myform" action="contact.php" method="POST">
<label for="name">First Name</label>
<input type="text" id="name" name="firstname" placeholder="Your name.." required>
<!-- <label for="lastname">Last Name</label>
<input type="text" id="lastname" name="lastname" placeholder="Your last name.." required> -->
<label for="email">Email:</label>
<input type="email" id="email" name="email" placeholder="Your Email.." required>
<label for="message">Subject</label>
<textarea id="message" name="message" placeholder="Lets Collaborate..." style="height:170px" required></textarea>
<input type="submit" value="Submit">
</form>
也許這會對你有所幫助:)
您好,我編寫了下面的代碼並對其進行了編輯,如果它不起作用或者您遇到任何問題,請在評論中告訴我,希望您發現這個答案對 html 頁面有幫助:
<form name="myform" action="contact.php" method="POST">
<label for="firstname">First Name</label>
<input type="text" id="firstname" name="firstname" placeholder="Your name.." required>
<label for="lastname">Last Name</label>
<input type="text" id="lastname" name="lastname" placeholder="Your last name.." required>
<label for="email">Email:</label>
<input type="email" id="email" name="email" placeholder="Your Email.." required>
<label for="subject">Subject</label>
<textarea id="subject" name="subject" placeholder="Lets Collaborate..." style="height:170px" required></textarea>
<input type="submit" value="Submit">
</form>
php 頁面:
$fname = $_POST['firstname'];
$lname = $_POST['lastname'];
$email_address = $_POST['email'];
$message = $_POST['subject'];
if(empty($fname) ||
empty($lname) ||
empty($email_address) ||
empty($message)){
$errors .= "\n Error: all fields are required";
}else{
//some other code
$to = $myemail;
$email_subject = "Contact form submission:" . $name;
$email_body = "You have received a new message. ".
" Here are the details:\n Name: " . $name . "\n ".
"Email: $email_address\n Message \n " . $message;
$headers = "From:" . $myemail . "\n";
$headers .= "Reply-To:" . $email_address;
mail($to,$email_subject,$email_body,$headers);
header('Location: thank-you.html');
}
正如您所提到的,您已經完成了重定向到thankyou.html
,但是 email 沒有發送給您。 問題可能在於mail($to,$email_subject,$email_body,$headers);
.
您會看到, mail
本身並沒有真正發送郵件,而是在主機中調用sendmail
,主機依次將要通過本地交換機發送的消息排隊。 所以
sendmail
(在這種情況下, mail
應該返回 false)sendmail
可能會失敗(檢查嘗試https://stackoverflow.com/a/13390926/1626321 )sendmail
最好的解決方案是切換到使用 SMTP,使用PHPMailer
( https://github.com/PHPMailer/PHPMailer )之類的東西。 應該有很多關於如何使用它的教程。 您可以使用主機中的 SMTP 服務器,甚至可以使用 Gmail。
<section id="contact">
<div class="container" data-aos="fade-up">
<div class="contactform">
<div style="text-align:center">
<div class="section-title">
<h2><br/>Get In Touch</h2>
</div>
<p>Feel Free To Reach Out To Me Through This Form! </p>
</div>
<div class="row">
<div class="column">
<form name="myform" action="mailto:youraddr@domain.tld" method="POST">
<label for="firstname">First Name</label>
<input type="text" id="firstname" name="firstname" placeholder="Your name.." required>
<label for="lastname">Last Name</label>
<input type="text" id="lastname" name="lastname" placeholder="Your last name.." required>
<label for="email">Email:</label>
<input type="email" id="email" name="email" placeholder="Your Email.." required>
<label for="subject">Subject</label>
<textarea id="subject" name="subject" placeholder="Lets Collaborate..." style="height:170px" required></textarea>
<input type="submit" value="Submit">
</form>
</div>
</div>
</div>
</div>
</section>
在 HTML 中,您可以在元素的 [action] 屬性中指定 mailto: 地址。 這將允許用戶的 email 客戶端創建一個 email 預填充 .
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.