문제

Hi I have simple contact form for email on my site, and the form works including the success of a message sent,however, I am not receiving email to the designated webmail server. I am running the latest PHP. Do some web servers cache mail or is there some error in this code I am not seeing.

<?php

$EmailFrom = "email@mydomain.com";
$EmailTo = "email@mydomain.com";
$Subject = "Contacting Me";
$Name = Trim(stripslashes($_POST['Name'])); 
$Tel = Trim(stripslashes($_POST['Tel'])); 
$Email = Trim(stripslashes($_POST['Email'])); 
$Message = Trim(stripslashes($_POST['Message'])); 

// validation
$validationOK=true;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
  exit;
}

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page 
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=index\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>

<div id="contact-area">

            <form method="post" action="contactengine.php">
                <label for="Name">Name:</label>
                <input type="text" name="Name" id="Name" />

                <label for="Email">Email:</label>
                <input type="text" name="Email" id="Email" />

                <label for="Message">Thought:</label><br />
                <textarea name="Message" rows="20" cols="20" id="Message"></textarea>

                <input type="submit" name="submit" value="Transmit" class="submit-button" />
            </form>

            <div style="clear: both;"></div></div>
도움이 되었습니까?

해결책 2

Don't use print "<meta http-equiv=\"refresh\"... because during testing, it caused an endless loop while sending multiple copies of the same Email.

Use header('Location: ...'); instead, along with exit;.

Plus, all your <meta http-equiv=\"refresh\" have generated a parse error.

Working example (tested with Email successfully received):

<?php

$EmailFrom = "email@mydomain.com";
$EmailTo = "email@mydomain.com";
$Subject = "Contacting Me";
$Name = Trim(stripslashes($_POST['Name'])); 
$Tel = Trim(stripslashes($_POST['Tel'])); 
$Email = Trim(stripslashes($_POST['Email'])); 
$Message = Trim(stripslashes($_POST['Message'])); 

// validation
$validationOK=true;
if (!$validationOK) {

// echo "error 1";

header('Location: error.htm');

exit;
}

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page 
if ($success){

header('Location: http://www.example.com/');

exit;

}
else{

header('Location: error.htm');

exit;

}
?>

다른 팁

Sending mail is more complicated than just running the mail() command, you need to take a look at the php.ini settings for the email configuration and continue troubleshooting from there.

If you are running on a host that does not configure the mail() function to run, you can try using the following opensource mailer:

PHPMailer

What you can try is to use the examples provided and run from your local development stack (XAMPP, MAMP, WAMP) connected from your home, you should be able to receive mail if configured correctly.

Once it is OK, you can try it on the server, it should send as well, if not, check that the outgoing port for sending mail is not blocked.

Alternative to using PHPMailer, you can look for a simple PHP mailer and try those out as well.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top