4

I'm running the example from chapter 1 in Head First's PHP and MySQL. I placed the files on Head Fist's site on apache's /var/www folder and the thing runs. Yet, why isn't php's mailto working?

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 isn't printing WAS IT MAILED? TRUE? Why isn't it mailing?

Edit: Is the fact that I'm mailing to my gmail address relevant?

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's 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>
andandandand
  • 21,946
  • 60
  • 170
  • 271
  • 2
    Side note: `mail()` returns either `TRUE` or `FALSE`. In PHP, when you print a boolean as string you get '1' for TRUE and '' (empty string) for FALSE. – Álvaro González Aug 04 '10 at 06:27

5 Answers5

3

From the PHP Docs for mail():

It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.

Also: the mail() message could have been accepted into the mail queue, yet bounced due to various reasons. Your best bet is to check your mail logs - which will vary depending on your operating system and sendmail program.

When sending mail from a web server it's very likely that the message is being categorized as spam - depending on the configuration of the receiving mail server, this may result in the message being sent to the spam folder or being automatically deleted.

leepowers
  • 37,828
  • 23
  • 98
  • 129
  • 1
    While this is true, the question suggests that either mail() returned false or the script's execution didn't even reach the mail() or echo... statement. We'll see ;-) – VolkerK Aug 04 '10 at 06:26
  • mail() returned false here. How would you get around the mail being categorized as spam? – andandandand Aug 04 '10 at 15:18
  • I see, I misread `WAS IT MAILED? TRUE` as the script's output while scanning the question. – leepowers Aug 04 '10 at 18:06
  • Oh, according to this post's accepted answer gmail requires a special configuration: http://stackoverflow.com/questions/36079/php-mail-using-gmail – andandandand Aug 04 '10 at 21:29
2

Since this seems to be a test/development webserver run

<?php echo get_cfg_var('cfg_file_path');

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

display_startup_errors = On
display_errors = On
error_reporting = E_ALL

Then re-start the webserver and try again. You will probably get some error/warning messages. Edit your original question and add those messages.

VolkerK
  • 95,432
  • 20
  • 163
  • 226
  • I don't understand the function of , what's its purpose here? I found the php.ini file using ubuntu's search. – andandandand Aug 04 '10 at 21:16
  • When I tried to alter the file I found it was read only (I'm trying to alter the file that's found on /etc/php5/apache2) – andandandand Aug 04 '10 at 21:39
  • If this is your development/test server those values in the php.ini become quite handy. `display_errors=On` maybe is debatable. You could also keep an eye on the error.log instead of sending the messages to the browser. But `error_reporting=E_ALL` or `error_reporting=E_ALL|E_STRICT` is really useful to spot errors right away. – VolkerK Aug 04 '10 at 21:42
  • "I found it was read only" - for your "normal user" account maybe. Try `sudo nano -w /etc/php5/apache2/php.ini` (if that is the correct path to the php.ini) – VolkerK Aug 04 '10 at 21:47
  • Thanks for the edit on you php function, I ran it with echo and it printed: /etc/php5/apache2/php.ini, the same path I found with ubuntu's search. – andandandand Aug 04 '10 at 21:47
  • Yes, this is just a test/playground server. – andandandand Aug 04 '10 at 21:48
1

mail-function could be disabled. Check your /var/log/mail.log if you're under UNIX.

dst
  • 1,770
  • 13
  • 26
1

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

Also have you set up or checked the send mail functionality of your server? Sometimes it is not set / enabled by default. Try sending a simple mail first to check it, and if that does not work, contact the server administrator immediately or submit a support ticket to your server.

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

Another point is that try using the following code:-

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

where "$headers" will contain the proper headers, and the 5th parameter could be your email address pre-pended 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";
?>
Knowledge Craving
  • 7,955
  • 13
  • 49
  • 92
  • What if I don't have a site? I'm working with localhost, toying to learn php, this isn't a real server. – andandandand Aug 04 '10 at 15:02
  • I mean, would it still work in gmail? I guess they have a block of some sort to avoid this being used as an evil spam making machine. – andandandand Aug 04 '10 at 15:03
  • @dmindreader - By default, "sendmail" functionality of localhost is disabled. You will need to enable it to send mails to Gmail or any other server. – Knowledge Craving Aug 05 '10 at 04:32
  • @dmindreader - At first, it's best to send a test email to check whether or not "sendmail" is working. Then try to see whether Gmail is accepting your mails or not. – Knowledge Craving Aug 05 '10 at 04:34
0

Maybe you didn't install a mail server.

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

Box
  • 2,432
  • 1
  • 18
  • 20
  • I don't think one needs to install a dedicated or auxiliary extra mail server, it isn't mentioned in the documentation. – andandandand Aug 04 '10 at 16:16