0

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 = "[email protected]"; // 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> 
6
  • Use error handlers to find out what the mail error is.. alternatively you can use an already built class like PHPMailer. Commented Mar 29, 2013 at 1:28
  • There are dozens of reasons a PHP mail() may not be received, such as your development environment doesn't have an SMTP server running, it went to spam (this is super common) your ISP blocks outbound messages, and on and on. Do you have an SMTP server? Can you read its logs? Commented Mar 29, 2013 at 1:28
  • Really, it is recommended to use something like PHPMailer instead of mail(). Commented Mar 29, 2013 at 1:29
  • How are you running the script? If you're running it on your own machine through your own Internet connection, be aware that some ISPs block outgoing emails. Commented Mar 29, 2013 at 1:29
  • Yes I knew it probably wouldn't work locally so its on a web server. So then I should just replace mail() with PHPmailer()? Other than that it looks ok though? Thanks for the quick replies. Commented Mar 29, 2013 at 1:33

4 Answers 4

2

Hmm... seems good. Try:

mail('[email protected]', '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.

Sign up to request clarification or add additional context in comments.

13 Comments

It looks good to me too, in fact I replicated this script in my own server, and the mail gets sent perfectly, just drops in my SPAM box
OK when changing it to mail('[email protected]', 'aSubject', 'aMessage'); I did receive it but with a @dreamhost.com as the sender
add "From: [email protected] \r\n" as a 4th argument (again, this is a string and not a variable). If you get the email, it means somethings wrong with the form submission :)
for some reason now I'm getting this error when I click submit Parse error: syntax error, unexpected T_STRING in /home/user/mywebsite.com/some-folder/mail.php on line 7
your code is perfect(just tested in gmail aswell). Must be something wrong with the web server your trying it on. http://www.w3schools.com/php/php_mail.asp is the simplest tutorial. Sorry i couldn't help =/
|
1

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('[email protected]', 'First Last');

$mail->AddReplyTo("[email protected]","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 = "[email protected]";
$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.

12 Comments

Ok thanks I'm trying this now. So after I've downloaded and installed PHPmailer, put this basic example code in my mail.php file, and filled in my info in the correct places, what would I do next? I'm not sure I see how it communicates with my form fields. Thanks
This is what I expect you do although it's contained in your download file. In the root folder of your downloaded file, you'll see class.smtp.php and class.phpmailer.php. These two files are required. In the page where you want to process your form, ensure that require_once('../class.phpmailer.php'); points to the actual location where it is. I suggest you the class.smtp.php and class.phpmailer.php and your script for processing your form all be in the same folder so you just require your class file as require_once('class.phpmailer.php');. Try it out.
I think I have everything set up correctly. In the mail() script I can see where $email = $_POST['email']; the $email variable is assigned a value of whatever was in the input field with a name of email but in the the PHPmailer script how do I put those form values into the mail.php? What do I add to the script to do this? I hope this makes sense. Sorry this is all new to me.
According to your question, the form above, is it a contact form so I know who is sending and who is receiving so I can configure for you as should work?
Yes it is a contact form. After studying the code for a while I am understanding it better but it is still not working for me. I keep getting a error that the body is empty.
|
0

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.

Comments

0

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 = "[email protected]";
            $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!!!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.