联系表格不发送电子邮件
我正在编写我的第一个 PHP 联系表单。我已经为此工作了一整天了。在谷歌上查找所有内容,我都找不到答案。联系人转到成功页面,但我的收件箱中仍然没有任何内容......
PHP:
<?php
// Define some constants
define( "RECIPIENT_NAME", "name" );
define( "RECIPIENT_EMAIL", "[email protected]" );
define( "EMAIL_SUBJECT", "Message" );
// Read the form values
$success = false;
$sender = isset( $_POST['sender'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['sender'] ) : "";
$email = isset( $_POST['email'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['email'] ) : "";
$message = isset( $_POST['message'] ) ? preg_replace( "/(From: |To: |BCC: |CC: |Subject: |Content-Type:)/", "", $_POST['message'] ) : "";
//If all values exist, send the email
if ( $sender && $email && $message ) {
$recipient = RECIPIENT_NAME . " <" . RECIPENT_EMAIL . $email . ">";
$headers = "From: " . $sender . " <" . $email . ">";
$success = mail( $recipient, EMAIL_SUBJECT, $message, $headers );
}
// Return an appropriate reponse to the browser
if( isset($_GET["ajax"]) ) {
echo $success ? "success" : "error";
} else {
?>
<html>
<head>
<title>Thanks!</title>
</head>
<body>
<?php if ( $success ) echo "<h2>Thanks for sending your message! We'll get back to you shortly.</h2>" ?>
<?php if ( !$success ) echo "<h2> Sorry, There was a problem sending your message. Please try again.</h2>" ?>
<p> Click your browser's back button to return to the page</p>
</body>
</html>
<?php
}
?>
有什么问题吗?
这是 HTML
<form id="contactForm" action="process.php" method="post">
<label for="sender">Your Name</label>
<input type="text" name="sender" id="sender" placeholder="Your Name" required="required" maxlength="40">
<label for="email">Your Email Address</label>
<input type="email" name="email" id="email" placeholder="Please type your email address"
required="required" maxlength="50">
<label for="message">Your Message</label>
<textarea name="message" id="message" placeholder="Please type your message" required="required"
cols="50" rows="5" maxlength="10000"></textarea>
<div id="form-buttons">
<input type="submit" id="send-message" name="send-message" value="Send Email" />
<input type="button" id="cancel" name="cancel" value="Cancel" />
</div><!-- #form-buttons -->
有人能看一下吗?我确信我做的一切都是对的......
I'm coding my first ever PHP contact form. I've been working on it all day. Looking up everything on Google and I can't get a answer. The contact from goes to the success page and still nothing goes into my inbox...........
PHP:
<?php
// Define some constants
define( "RECIPIENT_NAME", "name" );
define( "RECIPIENT_EMAIL", "[email protected]" );
define( "EMAIL_SUBJECT", "Message" );
// Read the form values
$success = false;
$sender = isset( $_POST['sender'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['sender'] ) : "";
$email = isset( $_POST['email'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['email'] ) : "";
$message = isset( $_POST['message'] ) ? preg_replace( "/(From: |To: |BCC: |CC: |Subject: |Content-Type:)/", "", $_POST['message'] ) : "";
//If all values exist, send the email
if ( $sender && $email && $message ) {
$recipient = RECIPIENT_NAME . " <" . RECIPENT_EMAIL . $email . ">";
$headers = "From: " . $sender . " <" . $email . ">";
$success = mail( $recipient, EMAIL_SUBJECT, $message, $headers );
}
// Return an appropriate reponse to the browser
if( isset($_GET["ajax"]) ) {
echo $success ? "success" : "error";
} else {
?>
<html>
<head>
<title>Thanks!</title>
</head>
<body>
<?php if ( $success ) echo "<h2>Thanks for sending your message! We'll get back to you shortly.</h2>" ?>
<?php if ( !$success ) echo "<h2> Sorry, There was a problem sending your message. Please try again.</h2>" ?>
<p> Click your browser's back button to return to the page</p>
</body>
</html>
<?php
}
?>
Is there anything wrong with it?
Heres the HTML
<form id="contactForm" action="process.php" method="post">
<label for="sender">Your Name</label>
<input type="text" name="sender" id="sender" placeholder="Your Name" required="required" maxlength="40">
<label for="email">Your Email Address</label>
<input type="email" name="email" id="email" placeholder="Please type your email address"
required="required" maxlength="50">
<label for="message">Your Message</label>
<textarea name="message" id="message" placeholder="Please type your message" required="required"
cols="50" rows="5" maxlength="10000"></textarea>
<div id="form-buttons">
<input type="submit" id="send-message" name="send-message" value="Send Email" />
<input type="button" id="cancel" name="cancel" value="Cancel" />
</div><!-- #form-buttons -->
Could someone take a look at this? I'm sure I did everything right.....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将此行:更改
为:
这可能是问题所在
change this line:
to:
that might be the issue
至少你的行
看起来是错误的,
mail
之前应该有一个等号。At least your line
seems wrong, there should be an equality sign before
mail
.