简体   繁体   中英

Why won't this PHP mail script work?

I don't know PHP very well and am trying to get a very simple PHP script to send emails. When submit is clicked I get the Thank You message but no email.

<?php $name = $_POST['name'];
$email = $_POST['email'];
$web = $_POST['web'];
$message = $_POST['message'];
$formcontent="From: $name \n Website: $web \n Message: $message";
$recipient = "myemail@example.com"; // I do have my email here
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
    ?>

here is the form html:

<form action="mail.php" method="post" class="form">  

    <p class="name">  
      <label for="name">Name</label>
        <input type="text" name="name" id="name" />  

    </p>  

    <p class="email">  
      <label for="email">E-mail</label> 
        <input type="text" name="email" id="email" />  

    </p>  

    <p class="web">  
      <label for="web">Website</label> 
        <input type="text" name="web" id="web" />  

    </p>  

    <p class="text">  
      <label for="web">Comments</label> 
        <textarea name="message"></textarea>  
    </p>  

    <p class="submit">  
        <input type="submit" value="Send" />  
    </p>        
</form> 

Hmm... seems good. Try:

mail('youremail@hotmail.com', 'aSubject', 'aMessage');

all arguments NOT variables, but actual strings with single quotes .

Should appear in your normal/ junk mail in the next 5 mins.


If that doesn't work, it means the script isn't executing, so change mail.php to just:

echo 'hi';

to ensure the script path is correct. that will find out whats wrong, cheers.

I'll suggest you simple make use of PHPMAILER class for this and you'll have some rest for yourself.

https://code.google.com/a/apache-extras.org/p/phpmailer/

This would be of good help so you don't need to start bothering about editing much codes that will drain your time for other works.

Check the files that came in the examples folder for the SMTP examples and use either SMTP advanced or basic. With SMTP advanced, you'll be able to throw and catch your errors. That helps you know where you have errors until all errors are resolved. See sample code for the SMTP basic codes.

require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

$mail             = new PHPMailer();

$body             = file_get_contents('contents.html');
$body             = preg_replace('/[\]/','',$body);

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
                                           // 1 = errors and messages
                                           // 2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->Host       = "mail.yourdomain.com"; // sets the SMTP server
$mail->Port       = 26;                    // set the SMTP port for the GMAIL server
$mail->Username   = "yourname@yourdomain"; // SMTP account username
$mail->Password   = "yourpassword";        // SMTP account password

$mail->SetFrom('name@yourdomain.com', 'First Last');

$mail->AddReplyTo("name@yourdomain.com","First Last");

$mail->Subject    = "PHPMailer Test Subject via smtp, basic with authentication";

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);

$address = "whoto@otherdomain.com";
$mail->AddAddress($address, "John Doe");

$mail->AddAttachment("images/phpmailer.gif");      // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}

When you are done testing and want to now stop outputting the SMTP buffer messages, locate this line that says

$mail->SMTPDebug = 2;

and replace it with

$mail->SMTPDebug = false;
$mail->do_debug = 0;

Then you're good to go. Ask questions where necessary.

First of all, you should be aware that your code is subject to headers injection using POST. You should use filter_var() ( http://php.net/manual/fr/function.filter-var.php ) to ensure that $email value is harmless.

Then, there are a few reasons which can prevent PHP to send your mails.

The mail() function uses your server's MTA (such as sendmail, postfix...) : you need to install/configure it properly to send mails.

Your ISP can be blocking the 25 port, too (to prevent spam).

Alternatively, you can use php pear's Mail and Net_SMTP classes to send an email via SMTP. Theses classes handle encoding, attached files, and so on, in a convenient way.

I am not a back end developer so I do not know a lot about php, but this code worked for me so give this a go.

 <p><span>Name</span><input class="contact" type="text" name="your_name" value="" /></p> <p><span>Email Address</span><input class="contact" type="text" name="your_email" value="" /></p> <p><span>Message</span><textarea class="contact textarea" rows="8" cols="50" name="your_message"></textarea></p> <p style="padding-top: 15px"><span>&nbsp;</span><input class="submit" type="submit" name="contact_submitted" value="Send" /></p> <?php $your_name = $_POST['your_name']; $your_email = $_POST[your_email]; $your_message = $_POST['your_message']; $recipient = "youremailhere@domain.com"; $subject = "New Message About BikeExcel"; mail($recipient , $subject, $your_message, "From " . $your_email); echo "Your Message Has Been Sent"; ?> 

So instead of putting your email in the mail string, you make a variable that will define your email. This will also make your code look nice which will make you look good in your portfolio. I hope this helped!!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM