【问题标题】:Bootstrap form to email doesn't send [duplicate]引导表单到电子邮件不发送[重复]
【发布时间】:2017-02-23 10:26:01
【问题描述】:

我在互联网上找到了一个 Bootstrap 表单,现在我想将其发送到我的电子邮件。 我有在线表格,所以本地主机等没有问题。当我尝试发送它时,我收到一条消息'谢谢!我会联系你'。所以它发送?但是我没有在我的邮箱中收到电子邮件。任何人的想法?请参阅下面的代码。

ps。当我在本地运行代码时,他遇到了一些错误,请参见图片。我在线没有这些错误,但也许这就是它不发送任何内容的原因?

<?php
	if (isset($_POST["submit"])) {
		$name = $_POST['name'];
		$email = $_POST['email'];
		$message = $_POST['message'];
		$human = intval($_POST['human']);
		$from = 'Demo Contact Form'; 
		$to = 'example@example.com'; 
		$subject = 'Message from Contact Demo ';
		
		$body ="From: $name\n E-Mail: $email\n Message:\n $message";
		// Check if name has been entered
		if (!$_POST['name']) {
			$errName = 'Please enter your name';
		}
		
		// Check if email has been entered and is valid
		if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
			$errEmail = 'Please enter a valid email address';
		}
		
		//Check if message has been entered
		if (!$_POST['message']) {
			$errMessage = 'Please enter your message';
		}
		//Check if simple anti-bot test is correct
		if ($human !== 5) {
			$errHuman = 'Your anti-spam is incorrect';
		}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
	if (mail ($to, $subject, $body, $from)) {
		$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
	} else {
		$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
	}
}
	}
?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="Bootstrap contact form with PHP example by BootstrapBay.com.">
    <meta name="author" content="BootstrapBay.com">
    <title>Bootstrap Contact Form With PHP Example</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
  </head>
  <body>
  	<div class="container">
  		<div class="row">
  			<div class="col-md-6 col-md-offset-3">
  				<h1 class="page-header text-center">Contact Form Example</h1>
				<form class="form-horizontal" role="form" method="post" action="mail.php">
					<div class="form-group">
						<label for="name" class="col-sm-2 control-label">Name</label>
						<div class="col-sm-10">
							<input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
							<?php echo "<p class='text-danger'>$errName</p>";?>
						</div>
					</div>
					<div class="form-group">
						<label for="email" class="col-sm-2 control-label">Email</label>
						<div class="col-sm-10">
							<input type="email" class="form-control" id="email" name="email" placeholder="example@domain.com" value="<?php echo htmlspecialchars($_POST['email']); ?>">
							<?php echo "<p class='text-danger'>$errEmail</p>";?>
						</div>
					</div>
					<div class="form-group">
						<label for="message" class="col-sm-2 control-label">Message</label>
						<div class="col-sm-10">
							<textarea class="form-control" rows="4" name="message"><?php echo htmlspecialchars($_POST['message']);?></textarea>
							<?php echo "<p class='text-danger'>$errMessage</p>";?>
						</div>
					</div>
					<div class="form-group">
						<label for="human" class="col-sm-2 control-label">2 + 3 = ?</label>
						<div class="col-sm-10">
							<input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
							<?php echo "<p class='text-danger'>$errHuman</p>";?>
						</div>
					</div>
					<div class="form-group">
						<div class="col-sm-10 col-sm-offset-2">
							<input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
						</div>
					</div>
					<div class="form-group">
						<div class="col-sm-10 col-sm-offset-2">
							<?php echo $result; ?>	
						</div>
					</div>
				</form> 
			</div>
		</div>
	</div>   
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
  </body>
</html>

【问题讨论】:

  • 除非您有任何 SMTP 服务器配置,否则在 localhost 中发送的任何电子邮件都不起作用。这就是为什么你有上述异常。因为您的代码在 php mail() 函数处停止。
  • 我有一个在线运行的网站,我可以在其中测试表单。图片是为了显示本地主机上存在什么样的错误,但我尝试通过网站发送邮件。
  • 您已将错误打印在页面上,但您无法找出问题所在?

标签: php forms twitter-bootstrap email


【解决方案1】:

使用此修改后的代码消除最初的 localhost 错误。

<?php
if (isset($_POST["submit"])) {
    $name = $_POST['name'];
    $email = $_POST['email']; //Sender's email
    $message = $_POST['message'];
    $human = intval($_POST['human']);

    $to = "receivers@email.address";
    $subject = "Message from Contact Demo";
    $body = "From: $name\n";
    $body .= "E-Mail: $email\n";
    $body .= "Message:\n $message";
    $header = "From:" . $email . "\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-type: text/html\r\n";

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";
    // Check if name has been entered
    if (!$_POST['name']) {
        $errName = 'Please enter your name';
    }

    // Check if email has been entered and is valid
    if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
        $errEmail = 'Please enter a valid email address';
    }

    //Check if message has been entered
    if (!$_POST['message']) {
        $errMessage = 'Please enter your message';
    }
    //Check if simple anti-bot test is correct
    if ($human !== 5) {
        $errHuman = 'Your anti-spam is incorrect';
    }
// If there are no errors, send the email
    if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
        if (mail($to, $subject, $body, $header)) {
            $result = '<div class="alert alert-success">Thank You! I will be in touch</div>';
        } else {
            $result = '<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
        }
    }
}
?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="description" content="Bootstrap contact form with PHP example by BootstrapBay.com.">
        <meta name="author" content="BootstrapBay.com">
        <title>Bootstrap Contact Form With PHP Example</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
    </head>
    <body>
        <div class="container">
            <div class="row">
                <div class="col-md-6 col-md-offset-3">
                    <h1 class="page-header text-center">Contact Form Example</h1>
                    <form class="form-horizontal" role="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
                        <div class="form-group">
                            <label for="name" class="col-sm-2 control-label">Name</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="">
                                <?php if (isset($errName)) echo "<p class='text-danger'>$errName</p>" ?>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="email" class="col-sm-2 control-label">Email</label>
                            <div class="col-sm-10">
                                <input type="email" class="form-control" id="email" name="email" placeholder="example@domain.com" value="">                                
                                <?php if (isset($errEmail)) echo "<p class='text-danger'>$errEmail</p>" ?>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="message" class="col-sm-2 control-label">Message</label>
                            <div class="col-sm-10">
                                <textarea class="form-control" rows="4" name="message"></textarea>
                                <?php if (isset($errMessage)) echo "<p class='text-danger'>$errMessage</p>" ?>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="human" class="col-sm-2 control-label">2 + 3 = ?</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
                                <?php if (isset($errHuman)) echo "<p class='text-danger'>$errHuman</p>" ?>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-10 col-sm-offset-2">
                                <input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-10 col-sm-offset-2">                               
                                <?php if (isset($result)) echo $result ?>
                            </div>
                        </div>
                    </form> 
                </div>
            </div>
        </div>   
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
    </body>
</html>

PHP isset() 将确定变量是否已设置且不为 NULL。最初的变量是 NULL,所以它会省略显示错误。

注意:大部分时间电子邮件会发送到您的电子邮件地址。检查 spam/junk 文件夹以查看电子邮件。这是由于电子邮件服务器的来源未经验证。

编辑 1

结合PHPMailermailgun 平台将确保您将电子邮件发送到目标地址,并可能避免进入垃圾邮件/垃圾邮件文件夹。

编辑 2

我已经用最近的修改更新了我的答案。请看一看。在实时服务器上测试。收到联系人演示电子邮件到我在 Outlook 上的垃圾邮件/垃圾文件夹,直接到我的 Gmail 收件箱。

屏幕 Form submitOutlookGmail

祝你好运!

【讨论】:

  • 感谢您的评论,错误已在本地消失 :) 但仍然没有收到邮件,也没有在 spam/junk 文件夹中。我会看看它。谢谢!
  • @Johan,请与我们分享您的服务器提供商。我一回到家就会发布更新。
  • 我的服务器提供商是 Versio。 www.version.nl
  • @Johan 请参考我的全部答案。脚本运行良好。
  • 嗯,好吧。我会看的。
【解决方案2】:

如果您希望代码在不更改的情况下准备就绪,您只需更改您的 php.ini 文件并更改与错误报告相关的行。 搜索类似的东西 error_reporting = E_ALL 并更改为 error_reporting = E_ALL & ~E_NOTICE 这样你就隐藏了未定义的问题

然后你需要在你的 php.ini 中配置你的 smtp 提供程序

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多