尝试发送测试电子邮件,下面是html表单和php代码的代码。我正在尝试使用wamp发送电子邮件。我已经更新了php.ini文件。显示页面后,以下代码会显示在我的消息上方。
Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} }
?>
另外,我的php代码直接位于同一文件中的表单代码之上。当我填写表单并提交时,它看似提交,但我从未收到电子邮件。
请帮助,我在这里死。
<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$phone = $_POST['phone'];
$from = 'From: Cruser Computers Website';
$to = 'mcruser@crusercomputers.com';
$subject = 'Hello';
$body = "From: $name\n E-Mail: $email\n Phone: $phone\n Message:\n $message";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} }
?>
<form id='contact-form' action='' method='post' enctype="multipart/form-data">
<div class="success">
Contact form submitted!<br>
<strong>We will be in touch soon.</strong>
</div>
<fieldset>
<label class="name">
<input type="text" name="name" value="Name:">
<span class="error">*This is not a valid name.</span> <span class="empty">*This field is required.</span>
</label>
<label class="email">
<input type="text" name="email" value="E-mail:">
<span class="error">*This is not a valid email address.</span> <span class="empty">*This field is required.</span>
</label>
<label class="phone">
<input type="tel" name="phone" value="Phone:">
<span class="error">*This is not a valid phone number.</span> <span class="empty">*This field is required.</span>
</label>
<label class="message">
<textarea name="message">Message:</textarea>
<span class="error">*The message is too short.</span> <span class="empty">*This field is required.</span>
</label>
<div class="buttons-wrapper">
<a class="button" data-type="reset">Clear</a>
<a class="button" name="submit" data-type="Submit">Submit</a>
</div>
</fieldset>
</form>
答案 0 :(得分:0)
php文件必须与html位于不同的文件中。根据您的代码,php可以与您的html文件夹位于同一文件夹中。
答案 1 :(得分:0)
这就是我能想到的,我在3个主流浏览器(chrome,mozila和IE11)中进行了测试。
只有在用户未提交信息时才会显示表单。
<?php
//If the email is not blank
if($_POST['email'] !== '' and isset($_POST['email'])) {
//Email isn't blank so we can define are vars and sent the message
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$phone = $_POST['phone'];
$from = 'From: Cruser Computers Website';
$to = 'mcruser@crusercomputers.com';
$subject = 'Hello';
$body = "From: $name\n E-Mail: $email\n Phone: $phone\n Message:\n $message";
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
}
else{
//Problem with sending mail
echo '<p>Message could not be sent!</p>';
}
}
else{
echo '<form id="contact-form" action="" method="post" enctype="multipart/form-data">
<fieldset>
<label class="name">
<input type="text" name="name" required placeholder="Name:">
</label>
<br>
<label class="email">
<input type="text" name="email" required placeholder="E-mail:">
</label>
<br>
<label class="phone">
<input type="tel" name="phone" required placeholder="Phone:">
</label>
<br>
<label class="message">
<textarea placeholder="Message" name="message"></textarea>
</label>
<div class="buttons-wrapper">
<input type="reset" value="Clear">
<input type="submit" name="submit" value="Submit"/>
</div>
</fieldset>
</form>';
}
如果您仍有问题,则必须检查更新php.ini文件的方式。这是我们在这里的表格图片:
答案 2 :(得分:0)
改变了很多,但最终对我有用。 html应该要求现在填写每个输入。 PHP通过检查电子邮件输入的值是否为空来检查是否提交了任何内容。我也做了一些造型。
修改强> 现在加载时没有显示任何内容!
更改:
if($_POST['email'] !== '') {
对此:
if($_POST['email'] !== '' and isset($_POST['email'])) {
PHP最终编辑的代码
<?php
//If the email is not blank
if($_POST['email'] !== '' and isset($_POST['email'])) {
//Email isn't blank so we can define are vars and sent the message
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$phone = $_POST['phone'];
$from = 'From: Cruser Computers Website';
$to = 'mcruser@crusercomputers.com';
$subject = 'Hello';
$body = "From: $name\n E-Mail: $email\n Phone: $phone\n Message:\n $message";
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
}else{
//Problem with sending mail
echo '<p>Message could not be sent!</p>';
}
}
?>
<form id='contact-form' action='' method='post' enctype="multipart/form-data">
<fieldset>
<label class="name">
<input type="text" name="name" required placeholder="Name:">
</label>
<br>
<label class="email">
<input type="text" name="email" required placeholder="E-mail:">
</label>
<br>
<label class="phone">
<input type="tel" name="phone" required placeholder="Phone:">
</label>
<br>
<label class="message">
<textarea placeholder='Message' name="message"></textarea>
</label>
<div class="buttons-wrapper">
<input type="reset" value="Clear">
<input type="submit" name="submit" value="Submit"/>
</div>
</fieldset>
</form>
答案 3 :(得分:0)
在页面开头包含这行代码。希望它能运作!
<?PHP
error_reporting(0);
?>
答案 4 :(得分:0)
使用表单上方的以下代码
<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$phone = $_POST['phone'];
$from = 'From: Cruser Computers Website';
$to = 'mcruser@crusercomputers.com';
$subject = 'Hello';
$body = "From: $name\n E-Mail: $email\n Phone: $phone\n Message:\n $message";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} }
?>