PHP 5 Mail issues.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ndjustin20
Forum Commoner
Posts: 31
Joined: Wed Sep 28, 2005 8:25 am

PHP 5 Mail issues.

Post by ndjustin20 »

Hello Everyone,

I'm having issues sending mail using a simple PHP mail script. The script generates no errors and from what I can tell should be sending the email. Here is the phpinfo link http://www.abesfamilyfire.com/phpinfo.php CentOS Linux and Apache

Here is the form

Code: Select all


<form  onsubmit="return checkFormData()" id="contactForm" action="contactProcess.php" method="get">
	<div style="background-color:#FFFF66">
   <label for="name">Name:</label>
   <input type="text" name="name" id="name" size="20" maxlength="30" /><br /><br />
   
   <label for="address">Address:</label>
   <input type="text" name="address" id="address" size="20" maxlength="70"  /><br /><br />
   
   <label for="city">City:</label>
   <input type="text" name="city" id="city" size="20" maxlength="70" /><br /><br />
   
   <label for="state">State:</label>
   <input type="text" name="state" id="state" size="20" maxlength="70" /><br /><br />
   
   <label for="zipCode">Zip Code:</label>
   <input type="text" name="zipCode" id="zipCode" size="20" maxlength="70" /><br /><br />
   
   <label for="email">Email:</label>
   <input type="text" name="email" id="email" size="20" maxlength="70" /><br /><br />

   <label for="message">Include A Message:</label>
   <textarea rows="10" cols="40" name="message" id="message" size="20" maxlength="500"></textarea><br /><br />
   
   <INPUT TYPE="image" SRC="SendYourMessage.jpg" BORDER="0" ALT="Send Your Message"><br /><br />
   
   </div>
   </form>






Here is the processing code of the script

Code: Select all


<?php

$Name = $_GET['name'];
$email = $_GET['email'];
$recipient = "ndjustin20@hotmail.com";
$mail_body = $_GET['name'] . "\r\n" . $_GET['address'] . "\r\n" . $_GET['city'] . "\r\n" . 
$_GET['state'] . "\r\n" . $_GET['zipCode'] . "\r\n" . "\r\n" . "\r\n" . $_GET['message'];
$subject = "Feedback from Abes Family Fire Website";


$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields

ini_set('sendmail_from', 'abe@abesfamilyfire.com'); //Suggested by "Some Guy"

mail($recipient, $subject, $mail_body); //mail command :)
?>

<?php $pageNum = 7 ?>

<?php include('messages.php'); ?>
<?php include('head.php'); ?>


<body class="thrColHybHdr">


<div id="container">
  <div id="header">
    <h1 align="center"><?php echo $headerMessage; ?></h1>
    <!-- end #header --></div>
  <?php include('sideBar1.php'); ?>
  <?php include('sideBar2.php'); ?>
  <?php include('main.php'); ?>
  <?php include('footer.php'); ?>

Any help is much appreciated.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: PHP 5 Mail issues.

Post by s.dot »

I know mail() must have a from parameter. Add $header as the last parameter in your mail() argument.
Is your error reporting on?

Code: Select all

ini_set('display_errors', 'On');
error_reporting(E_ALL);
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: PHP 5 Mail issues.

Post by califdon »

You are making your task much more difficult than it needs to be. First of all, there's no need to enclose each and every PHP line in <?php ... ?> tags. For example, instead of the final 4 lines above, just code it this way:

Code: Select all

<?php
include('sideBar1.php');
include('sideBar2.php');
include('main.php');
include('footer.php');
?>
Next, I would recommend using the POST method of sending the form data, rather than the GET method. There are many reasons for this, including security, appearance of the URL, etc. See http://www.tizag.com/phpT/postget.php

Then, since you are experiencing a problem and would like to see the error message, if any, you can do several things on a temporary basis, while you are debugging the script, and these should later be removed before going public with the final version:

1. In order to display error messages, insert the following PHP commands immediately after the first <?php line of your script:

Code: Select all

error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
2. Change your mail() function line to this:

Code: Select all

mail($recipient, $subject, $mail_body) or die("Error Sending Mail");
This will halt execution of your script and print the message if there is an error returned by the mail() function.
ndjustin20
Forum Commoner
Posts: 31
Joined: Wed Sep 28, 2005 8:25 am

Re: PHP 5 Mail issues.

Post by ndjustin20 »

Ok so here is the code. There are 0 errors reporting and I can see the values in the GET function. I have no idea why it's not sending at this point. Is there something in the ini file I need to change????

Code: Select all


$Name = $_GET['name'];
$email = $_GET['email'];
$recipient = "ndjustin20@hotmail.com";
$mail_body = $_GET['name'] . "\r\n" . $_GET['address'] . "\r\n" . $_GET['city'] . "\r\n" . 
$_GET['state'] . "\r\n" . $_GET['zipCode'] . "\r\n" . "\r\n" . "\r\n" . $_GET['message'];
$subject = "Feedback from Abes Family Fire Website";


$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields

ini_set('sendmail_from', 'abe@abesfamilyfire.com'); //Suggested by "Some Guy"

mail($recipient, $subject, $mail_body) or die("Error Sending Mail"); //mail command :)

print_r($_GET);

ini_set('display_errors', 'On');
 	error_reporting(E_ALL);

User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP 5 Mail issues.

Post by Celauran »

There's also the possibility that it's not a PHP error. Have you checked that sendmail is properly configured? Any clues in your sendmail logs?
ndjustin20
Forum Commoner
Posts: 31
Joined: Wed Sep 28, 2005 8:25 am

Re: PHP 5 Mail issues.

Post by ndjustin20 »

How do I check my sendmail log or ensure it is configured properly?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP 5 Mail issues.

Post by Celauran »

If it's not your machine, just disregard that.
ndjustin20
Forum Commoner
Posts: 31
Joined: Wed Sep 28, 2005 8:25 am

Re: PHP 5 Mail issues.

Post by ndjustin20 »

It's a dedicated server so I have access to all portions of it.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP 5 Mail issues.

Post by Celauran »

My CentOS machines have the mail log at /var/log/maillog.

Also, I tested your code on my test machine and received the email, so the problem definitely isn't your code.
Post Reply