on your last condition
if ($problem = TRUE) {print '<br />';}
you are not checking if problem
is true
but you are SETTING problem
to TRUE
change it to:
if ($problem == TRUE) {print '<br />';}
题
I am really new at all of this and need help... I have a forum I am trying to send, but for some reason it is not sending. The mailto function is not sending an email on the server, and it is also not refreshing to redirect.html. Here is my code:
<?php
$problem = FALSE;
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (empty($_POST['name'])) {
$problem = TRUE;
print '<p class="important">• What is your name?</p>';
}
if (empty($_POST['email'])) {
$problem =TRUE;
print '<p class="important">• What is your email?</p>';
}
if (empty($_POST['message'])) {
$problem =TRUE;
print '<p class="important">• What is your message?</p>';
}
if ($problem = TRUE) {print '<br />';}
}
?>
<form action="index.php" id="contact_me" method="post">
<table border="0" cellspacing="0" cellpadding="0" width="500">
<tr>
<td width="160">Your Name:<span class="important">*</span></td>
<td width="340"><input name="name" type="text" size="30" value="<?php if ($_SERVER['REQUEST_METHOD'] == 'POST') { if (isset($_POST['name'])) { print htmlspecialchars($_POST['name']); }}?>"/>
</td>
</tr>
<tr>
<td><p>Your Email:<span class="important">*</span></p>
</td>
<td><input name="email" type="text" size="30" placeholder="example@example.com" value="<?php if ($_SERVER['REQUEST_METHOD'] == 'POST') { if (isset($_POST['email'])) { print htmlspecialchars($_POST['email']); }}?>"/></td>
</tr>
<tr>
<td>Message:<span class="important">*</span></td>
<td><textarea name="message" rows="6" cols="40" placeholder="Your message here"><?php if($_SERVER['REQUEST_METHOD']=='POST'){if(isset($_POST['message'])){print htmlspecialchars($_POST['message']);}}?></textarea></td>
</tr>
</table><br />
<input type="submit" name="submit" id="submit" value="Submit" >
</form>
<?php
// Start the email function
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if ($problem == FALSE) { // holy cow they didn't mess up
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$message=$_REQUEST['message'];
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="Mathew Blair - Contact form";
mail("mathewblair@live.com", $subject, $message, $from);
Header("Location: redirect.html");
}
}
?>
Thank you for your help!
解决方案
on your last condition
if ($problem = TRUE) {print '<br />';}
you are not checking if problem
is true
but you are SETTING problem
to TRUE
change it to:
if ($problem == TRUE) {print '<br />';}
其他提示
You need to have a SMTP configured in order for the mail
to work.
The mail
function could also be malfunctioning because of a configuration error. You will have to edit your php.ini file.
The server that you are on will need to have a mail service such as sendmail or postfix installed.
On Ubuntu, that's
sudo apt-get install sendmail
or
sudo apt-get install postfix
You may have to change your mail configuration in your php.ini (http://php.net/manual/en/mail.configuration.php).
Also, for sending email, I found that it was better to use a full class instead of just the mail function. Such a class will make it easier to connect with a 3rd party mail gateway, such as gmail. So, you'll be able to send email from your web application via gmail's SMTP servers, which is much better for not having your sent emails detected as spam. Here are three good packages for that. I recommend swiftmailer.
https://github.com/Synchro/PHPMailer