Why doesn't this PHP script send me an email?

I run the example from chapter 1 in Head First PHP and MySQL. I placed the files on the Head Fist website in the apache / var / www folder and everything works. However, why does php mailto not work?

edit: btw, I'm working on Ubuntu 10.04

I added two debugging lines to the script:

$testmail = mail($to, $subject, $msg); echo 'WAS IT MAILED? <br />'.$testmail; 

Why is WAS IT MAILED? TRUE not printed WAS IT MAILED? TRUE WAS IT MAILED? TRUE ? Why is it not being mailed?

Edit: Is the fact that I am sending messages to my gmail address?

Here's the script:

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Aliens Abducted Me - Report an Abduction</title> </head> <body> <h2>Aliens Abducted Me - Report an Abduction</h2> <?php $name = $_POST['firstname'] . ' ' . $_POST['lastname']; $when_it_happened = $_POST['whenithappened']; $how_long = $_POST['howlong']; $how_many = $_POST['howmany']; $alien_description = $_POST['aliendescription']; $what_they_did = $_POST['whattheydid']; $fang_spotted = $_POST['fangspotted']; $email = $_POST['email']; $other = $_POST['other']; $to = ' antoniorueda18@gmail.com '; $subject = 'Aliens Abducted Me - Abduction Report'; $msg = "$name was abducted $when_it_happened and was gone for $how_long.\n" . "Number of aliens: $how_many\n" . "Alien description: $alien_description\n" . "What they did: $what_they_did\n" . "Fang spotted: $fang_spotted\n" . "Other comments: $other"; $testmail = mail($to, $subject, $msg); echo 'WAS IT MAILED? <br />'.$testmail; echo 'Thanks for submitting the form.<br />'; echo 'You were abducted ' . $when_it_happened; echo ' and were gone for ' . $how_long . '<br />'; echo 'Number of aliens: ' . $how_many . '<br />'; echo 'Describe them: ' . $alien_description . '<br />'; echo 'The aliens did this: ' . $what_they_did . '<br />'; echo 'Was Fang there? ' . $fang_spotted . '<br />'; echo 'Other comments: ' . $other . '<br />'; echo 'Your email address is ' . $email; ?> </body> </html> 

And here is the html form:

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Aliens Abducted Me - Report an Abduction</title> <link rel="stylesheet" type="text/css" href="style.css" /> </head> <body> <h2>Aliens Abducted Me - Report an Abduction</h2> <p>Share your story of alien abduction:</p> <form method="post" action="report.php"> <label for="firstname">First name:</label> <input type="text" id="firstname" name="firstname" /><br /> <label for="lastname">Last name:</label> <input type="text" id="lastname" name="lastname" /><br /> <label for="email">What is your email address?</label> <input type="text" id="email" name="email" /><br /> <label for="whenithappened">When did it happen?</label> <input type="text" id="whenithappened" name="whenithappened" /><br /> <label for="howlong">How long were you gone?</label> <input type="text" id="howlong" name="howlong" /><br /> <label for="howmany">How many did you see?</label> <input type="text" id="howmany" name="howmany" /><br /> <label for="aliendescription">Describe them:</label> <input type="text" id="aliendescription" name="aliendescription" size="32" /><br /> <label for="whattheydid">What did they do to you?</label> <input type="text" id="whattheydid" name="whattheydid" size="32" /><br /> <label for="fangspotted">Have you seen my dog Fang?</label> Yes <input id="fangspotted" name="fangspotted" type="radio" value="yes" /> No <input id="fangspotted" name="fangspotted" type="radio" value="no" /><br /> <img src="fang.jpg" width="100" height="175" alt="My abducted dog Fang." /><br /> <label for="other">Anything else you want to add?</label> <textarea id="other" name="other"></textarea><br /> <input type="submit" value="Report Abduction" name="submit" /> </form> </body> </html> 
+4
source share
5 answers

From PHP docs for mail() :

It is important to note that simply because the mail was accepted for delivery, this does NOT mean the mail is actually the destination.

Also: the mail() message could be received in the mail queue, but was bounced for various reasons. It is best to check the message logs - it depends on your operating system and the sendmail program.

When sending mail from a web server, it is very likely that the message is classified as spam - depending on the configuration of the receiving mail server, this can lead to the message being sent to the spam folder or deleted automatically.

+3
source

Since this is like starting the test / development web server

 <?php echo get_cfg_var('cfg_file_path'); 

it will print which php.ini was used by this php instance. Open this php.ini in a text editor and set the following values ​​(the directive should already be there, you only need to change their values)

 display_startup_errors = On display_errors = On error_reporting = E_ALL 

Then start the web server and try again. You will probably get some error / warning messages. Modify the original question and add these posts.

+2
source

The mail function may be disabled. Check your /var/log/mail.log if you are on UNIX.

+1
source

First of all, try writing PHP code at the top of the web page by checking the input type in POST.

Also, have you configured or checked the functionality of sending mail on your server? By default, it is not installed / enabled. Try sending simple mail first to check it, and if that doesn't work, contact your server administrator immediately or send a support ticket to your server.

Edit: - From your code, usually the "$ testmail" variable will always provide the value "TRUE" or "1" if the syntax of the "mail ()" function is correct. It will return only false if mail was not queued for delivery.

Another point: try using the following code: -

 mail($to, $subject, $message, $headers, " -femail.address@example.com "); 

where "$ headers" will contain the appropriate headers, and the fifth parameter may be your email address, previously overlaid with "-f".

 <?php $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n"; $headers .= "From: My site< noreply@my _site.com>" . "\r\n"; $headers .= "Reply-To: info@my _site.com" . "\r\n"; $headers .= "Return-Path: info@my _site.com" . "\r\n"; $headers .= "X-Mailer: PHP/" . phpversion() . "\r\n"; ?> 
+1
source

You may not have installed a mail server.

Here is a good guide. http://library.linode.com/email/exim/send-only-mta-ubuntu-9.10-karmic

0
source

All Articles